Merge pull request #6270 from andyque/feature4783

closed #4783, add outline shader files
This commit is contained in:
James Chen 2014-04-25 13:50:44 +08:00
commit 06d9187783
3 changed files with 101 additions and 5 deletions

View File

@ -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]);
@ -120,7 +121,7 @@ protected:
virtual void setCustomUniforms() = 0;
protected:
std::string _fragSourceFile;
std::string _vertSourceFile;
protected:
CustomCommand _renderCommand;
void onDraw(const kmMat4 &transform, bool transformUpdated);
@ -128,6 +129,7 @@ protected:
};
ShaderSprite::ShaderSprite()
:_vertSourceFile("")
{
}
@ -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();
@ -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);
}
}

View File

@ -94,4 +94,12 @@ public:
virtual std::string subtitle() const {return "CelShadingSpriteTest";}
};
class OutlineShadingSpriteTest : public ShaderTestDemo2
{
public:
CREATE_FUNC(OutlineShadingSpriteTest);
OutlineShadingSpriteTest();
virtual std::string subtitle() const {return "OutlineShadingSpriteTest";}
};
#endif

View File

@ -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;
}