Hi, I’m using cocos2dx v3.5.
I have no experience with shaders, I have seen this:
Pixel perfect collision
and take the shaders from there.
SolidColorShader.fsh
#ifdef GL_ES
precision lowp float;
#endif
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform int u_color_red;
uniform int u_color_blue;
void main()
{
vec4 color = texture2D(u_texture, v_texCoord);
gl_FragColor = vec4(u_color_red, 0, u_color_blue, color.a);
}
SolidVertexShader.vsh
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
in helloworld.cpp
Sprite *sprite=Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(512,384));
this->addChild(sprite);
CCGLProgram *shaderProgram_ = new CCGLProgram();
sprite->setShaderProgram(shaderProgram_);
shaderProgram_->initWithVertexShaderFilename("SolidVertexShader.vsh", "SolidColorShader.fsh");
shaderProgram_->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
shaderProgram_->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
shaderProgram_->link();
shaderProgram_->updateUniforms();
sprite->getShaderProgram()->use();
after added sprite->setShaderProgram(shaderProgram_);
the sprite disappear, I tryed with different location and seems that the sprite has different coordinates than before…
I tryed with a prebuilt shader but when I apply it, the position of the image is different once applied…
Sprite *sprite=Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(512,384));
this->addChild(sprite);
// sprite->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
Changing
gl_Position = CC_PMatrix * a_position;
//gl_Position = CC_MVPMatrix * a_position
Seem not solve the problem…
Can you help me to understand a basic use of this shaders?
thanks