cocos2dx - Custom Fragment Shader and CCRenderTexture

Posted by saiy2k on Game Development See other posts from Game Development or by saiy2k
Published on 2013-06-13T16:08:16Z Indexed on 2013/11/05 4:15 UTC
Read the original article Hit count: 711

Filed under:
|

I have a CCRenderTexture that is filled with a sprite when the scene is loaded, as follows,

canvas = CCRenderTexture::create(this->getContentSize().width, this->getContentSize().height);
canvas->setPosition(data->position);
canvas->beginWithClear(0.0, 0.0, 0.0, 0);
this->visit();
canvas->end();

The above code is written within a class, which derives from CCSprite (Hence this).

Then, in another function applyShader(), I create a sprite named splat, from the texture of CCRenderTexture *canvas. Thus splat will contain the whole texture of canvas.

Now I apply a custom fragment shader to the splat by calling the function splat->renderShader(), which will modify some small portion of the whole texture.

Then I draw the modified texture back to the CCRenderTexture *canvas.

Hence, applyShader() will * take a texture from CCRenderTexture, * create a sprite based on it, * apply a fragment shader to it * and draw the modified texture back to CCRenderTexture.

This applyShader() will be called repetitively and its code is as follows:

splat = Splat::createWithTexture(art->canvas->getSprite()->getTexture());
splat->renderShader();
art->canvas->begin();
splat->visit();
art->canvas->end();

My shader code is (nothing fancy)

precision mediump float;

varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform sampler2D u_colorRampTexture;
uniform float params[5];

void main() {
    gl_FragColor = texture2D(u_texture, v_texCoord);
    return;
}

So, with the above code I expect the original sprite this to get rendered over and over again without any visual changes. But on each call to applyShader(), the texture is getting stretched a little and the stretched image is getting rendered. After some 10 calls, the image gets so distorted.

Can someone please tell me where I am going wrong?

Thanks :-)

PS: All code shown here is partial, not complete code.

Edit: Adding Screens Render Texture Problem

Update: The problem has nothing to do with shaders it seems. It happens even when I dont call renderShader(). The actual lines of code is:

splat = Splat::createWithTexture(art->canvas->getSprite()->getTexture());
splat->setPosition( ccp( art->getContentSize().width * 0.5, art->getContentSize().height * 0.5 ) );
splat->setFlipY(true);
art->canvas->begin();
splat->visit();
art->canvas->end();

© Game Development or respective owner

Related posts about cocos2d-x

Related posts about fragment-shader