I'm applying a shader to my BatchNode with cocos2d 2.0.
I updated the ccV3F_C4B_T2F struct to hold a new uniform, so it now looks like this:
typedef struct _ccV3F_C4B_T2F {
ccVertex3F vertices;
ccColor4B colors;
ccTex2F texCoords;
ccVertex2F NewCoords;
} ccV3F_C4B_T2F;
I added a new property value to plist of my CCspritebatchnode, which is the value that i want to pass to the shader. So when the batchnode gets the offset ,color rect, etc., of each frame. It will also get the value that I need passed through. that worked.
I then took the values from the Batchnodes plist and modified 'updatetransform' to recieve the values. so it looks something like this:
quad_.bl.NewCoords = (ccVertex2F) { newCoords.x, newCoords.y };
quad_.br.NewCoords = (ccVertex2F) { newCoords.x, newCoords.y };
quad_.tl.NewCoords = (ccVertex2F) { newCoords.x, newCoords.y };
quad_.tr.NewCoords = (ccVertex2F) { newCoords.x, newCoords.y };
I subclassed CCSpriteBatchNode and applied the shader:
vertex shader:
\n\
attribute vec4 a_position; \n\
attribute vec2 a_texCoord; \n\
attribute vec4 a_color; \n\
attribute vec2 a_newcoords; \n\
\n\
varying lowp vec4 v_fragmentColor; \n\
varying mediump vec2 v_texCoord; \n\
varying mediump vec2 v_newcoords; \n\
\n\
void main() \n\
{ \n\
gl_Position = CC_MVPMatrix * a_position; \n\
v_fragmentColor = a_color; \n\
v_texCoord = a_texCoord; \n\
v_newcoords = a_newcoords; \n\
} \n\
";
fragment shader:
#ifdef GL_ES
precision highp float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
varying vec2 v_newcoords;
uniform sampler2D u_texture;
void main()
{
vec2 texCoord = v_texCoord;
vec4 texColor = texture2D(u_texture, texCoord);
vec2 MoveBy = vec2(v_newcoords.x, v_newcoords.y);
vec4 Color1 = texture2D(u_texture, vec2(v_texCoord.x - MoveBy.x , v_texCoord.y));
vec4 finalColor = vec4(texColor.r, texColor.g, Color1.b, texColor.a);
gl_FragColor = finalColor;
}
The vertex shader gets the 'attribute vec2 a_newcoords;' correctly, but I can't access the 'varying v_newcoords' from the fragment shader.
Does someone know what I might be doing wrong??