Skip to content

Commit

Permalink
Merge pull request cocos2d#6270 from andyque/feature4783
Browse files Browse the repository at this point in the history
closed cocos2d#4783, add outline shader files
  • Loading branch information
James Chen committed Apr 25, 2014
2 parents db26148 + a75fcfb commit 437d6f2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 5 deletions.
78 changes: 73 additions & 5 deletions tests/cpp-tests/Classes/ShaderTest/ShaderTest2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace ShaderTest2
CL(EdgeDetectionSpriteTest),
CL(BloomSpriteTest),
CL(CelShadingSpriteTest),
CL(LensFlareSpriteTest)
CL(LensFlareSpriteTest),
CL(OutlineShadingSpriteTest)
};

static unsigned int TEST_CASE_COUNT = sizeof(ShaderTest2::createFunctions) / sizeof(ShaderTest2::createFunctions[0]);
Expand Down Expand Up @@ -120,14 +121,15 @@ class ShaderSprite : public Sprite
virtual void setCustomUniforms() = 0;
protected:
std::string _fragSourceFile;

std::string _vertSourceFile;
protected:
CustomCommand _renderCommand;
void onDraw(const kmMat4 &transform, bool transformUpdated);

};

ShaderSprite::ShaderSprite()
:_vertSourceFile("")
{
}

Expand All @@ -148,10 +150,19 @@ void ShaderSprite::setBackgroundNotification()

void ShaderSprite::initShader()
{
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();
auto fileUtiles = FileUtils::getInstance();
auto fragmentFilePath = fileUtiles->fullPathForFilename(_fragSourceFile);
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
std::string vertSource;
if (_vertSourceFile.empty()) {
vertSource = ccPositionTextureColor_vert;
}else{
std::string vertexFilePath = fileUtiles->fullPathForFilename(_vertSourceFile);
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}

auto program = new GLProgram();
program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
program->initWithByteArrays(vertSource.c_str(), fragSource.c_str());
setShaderProgram(program);
program->release();

Expand Down Expand Up @@ -632,3 +643,60 @@ LensFlareSpriteTest::LensFlareSpriteTest()
addChild(sprite);
}
}


class OutlineSprite : public ShaderSprite, public ShaderSpriteCreator<OutlineSprite>
{
public:
CREATE_FUNC(OutlineSprite);
OutlineSprite();

private:
GLuint _outlineColorUniformLocation;
GLuint _thresdholdUniformLocation;
GLuint _radiusUniformLocation;
protected:
virtual void buildCustomUniforms();
virtual void setCustomUniforms();
};


OutlineSprite::OutlineSprite()
{
_fragSourceFile = "Shaders/example_outline.fsh";
_vertSourceFile = "Shaders/example_outline.vsh";
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}

void OutlineSprite::buildCustomUniforms()
{
auto program = getShaderProgram();
_outlineColorUniformLocation = program->getUniformLocation("u_outlineColor");
_thresdholdUniformLocation = program->getUniformLocation("u_threshold");
_radiusUniformLocation = program->getUniformLocation("u_radius");
}

void OutlineSprite::setCustomUniforms()
{
GLfloat color[3] = {1.0, 0.2, 0.3};
GLfloat radius = 0.01;
GLfloat threshold = 1.75;

getShaderProgram()->setUniformLocationWith3fv(_outlineColorUniformLocation, color, 1);
getShaderProgram()->setUniformLocationWith1f(_radiusUniformLocation, radius);
getShaderProgram()->setUniformLocationWith1f(_thresdholdUniformLocation, threshold);
}


OutlineShadingSpriteTest::OutlineShadingSpriteTest()
{
if (ShaderTestDemo2::init()) {
auto s = Director::getInstance()->getWinSize();
OutlineSprite* sprite = OutlineSprite::createSprite("Images/grossini_dance_10.png");
sprite->setPosition(Point(s.width * 0.75, s.height/2));
auto sprite2 = Sprite::create("Images/grossini_dance_10.png");
sprite2->setPosition(Point(s.width * 0.25, s.height/2));
addChild(sprite);
addChild(sprite2);
}
}
8 changes: 8 additions & 0 deletions tests/cpp-tests/Classes/ShaderTest/ShaderTest2.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ class CelShadingSpriteTest : public ShaderTestDemo2
virtual std::string subtitle() const {return "CelShadingSpriteTest";}
};

class OutlineShadingSpriteTest : public ShaderTestDemo2
{
public:
CREATE_FUNC(OutlineShadingSpriteTest);
OutlineShadingSpriteTest();
virtual std::string subtitle() const {return "OutlineShadingSpriteTest";}
};

#endif
38 changes: 38 additions & 0 deletions tests/cpp-tests/Resources/Shaders/example_outline.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Created by guanghui on 4/8/14.
http://www.idevgames.com/forums/thread-3010.html
*/

varying vec2 v_texCoord;
varying vec4 v_fragmentColor;

uniform sampler2D CC_Texture0;

uniform vec3 u_outlineColor;
uniform float u_threshold;
uniform float u_radius;

void main()
{
float radius = u_radius;
vec4 accum = vec4(0.0);
vec4 normal = vec4(0.0);

normal = texture2D(CC_Texture0, vec2(v_texCoord.x, v_texCoord.y));

accum += texture2D(CC_Texture0, vec2(v_texCoord.x - radius, v_texCoord.y - radius));
accum += texture2D(CC_Texture0, vec2(v_texCoord.x + radius, v_texCoord.y - radius));
accum += texture2D(CC_Texture0, vec2(v_texCoord.x + radius, v_texCoord.y + radius));
accum += texture2D(CC_Texture0, vec2(v_texCoord.x - radius, v_texCoord.y + radius));

accum *= u_threshold;

accum.r = u_outlineColor.x;
accum.g = u_outlineColor.y;
accum.b = u_outlineColor.z;

normal = (accum * (1.0 - normal.a)) + (normal * normal.a);

gl_FragColor = v_fragmentColor * normal;
}

20 changes: 20 additions & 0 deletions tests/cpp-tests/Resources/Shaders/example_outline.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Created by guanghui on 4/8/14.
*/


attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;

varying vec2 v_texCoord;

varying vec4 v_fragmentColor;

void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}

0 comments on commit 437d6f2

Please sign in to comment.