Reputation: 955
I am making a path based game using cocos2d 2.0 which will randomly generate a path (curvature) for the player. This path has been created using box2d's b2EdgeShape. I want to place a sprite below that path, the route I have taken is this:
The sprite to be shown below the path will repeat continuously whose height will always be greater than the height of the path (Parallax background)
A white sprite will fit perfectly below the path using CCRenderTexture (my real question)
A fragment shader will compare the alpha values of the white sprite with the sprite I have to show and multiply both the values. For each pixel where the sprite has to be shown the alpha value after multiplication will be 1 and hopefully I could achieve the effect I am looking for.
How do I make that white sprite to fit perfectly below the path created using CCRenderTexture. I tried to follow the Tiny Wings tutorial on RayWenderlich but that tutorial is based on OpenGL ES 1.1 and has some fixed pipeline functions that I can not port to OpenGL 2.0 (I have very little knowledge of OpenGL to begin with.) I would rather use cocos2d 2.0 which has OpenGL 2.0. I have worked on the fragment shader, using a static white sprite and this approach can work if I can constantly get that white color underneath the path. Can somebody please point me in the right direction? Any help would be appreciated :)
Upvotes: 1
Views: 416
Reputation: 166
If the white sprite is just an alpha mask, you don't need to do that. Follow's Ray's example and I think all you have to change is:
In your -init:
// Built-in to Cocos2D v2.0
[self setShaderProgram: [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture]];
... Generate _hillVertices and _hillTexCoords as Ray does ...
In your -draw:
// Built-in to Cocos2D v2.0
[[self shaderProgram] use];
[[self shaderProgram] setUniformForModelViewProjectionMatrix];
// vertex data (glVertexPointer is depreciated)
glEnableVertexAttribArray ( kCCVertexAttrib_Position );
glVertexAttribPointer ( kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, _hillVertices );
// texture data (glTexCoordPointer is depreciated)
glEnableVertexAttribArray ( kCCVertexAttrib_TexCoords );
glVertexAttribPointer ( kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, _hillTexCoords );
glDrawArrays ( GL_TRIANGLE_STRIP, 0, (GLsizei)_nHillVertices );
I have a working implementation that is similar (I used vertices and color coordinates, no texture) so I haven't actually compiled the code above. Cocos2d v2.0 has built-in shaders for using vertices with textures, vertices with colors, vertices with textures and colors, etc. Just pick the appropriate kCCShader_* key when you setShaderProgram and provide the right data.
Upvotes: 1