cocos2dx - Custom Fragment Shader and CCRenderTexture

cocos2dx - Custom Fragment Shader and CCRenderTexture
0.0 0


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~~>beginWithClear(0.0, 0.0, 0.0, 0);

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~~>getTexture);

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);

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 :slight_smile:


Initial state of the Image:

After some 50 calls to applyShader(), it mutates like this: