Hello,
I have tried to follow this tutorial “http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x” But I got trouble at “Adding a Gradient to the Texture” chapter. I have used this code to add gradient to my texture but that doesn’t work. I have started the game but I didn’t saw any gradient in it. I don’t know what the issue is but I think it is glDrawArrays but It can possibly be that all functions I use are decrepated because I am using cocos2d-x 3.0 final. This is my Spritewithcolor function.
Sprite* HelloWorld::spriteWithColor(Color4F bgColor, float textureWidth, float textureHeight)
{
// 1: Create new CCRenderTexture
RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight);
// 2: Call CCRenderTexture:begin
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
// 3: Draw into the texture
float gradientAlpha = 1;
Point vertices[4];
Color4F colors[4];
int nVertices = 0;
vertices[nVertices] = Point(0, 0);
colors[nVertices++] = Color4F(0, 0, 0, 0);
vertices[nVertices] = Point(textureWidth, 0);
colors[nVertices++] = Color4F(0, 0, 0, 0);
vertices[nVertices] = Point(0, textureHeight);
colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);
vertices[nVertices] = Point(textureWidth, textureHeight);
colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);
// Set the shader program for OpenGL
this->setShaderProgram(ShaderCache::getInstance()->getProgram(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
Sprite* noise = Sprite::create("Noise.png");
BlendFunc myBlend;
myBlend.src = GL_DST_COLOR;
myBlend.dst = GL_ZERO;
noise->setBlendFunc(myBlend);
noise->setPosition(Point(textureWidth/2, textureHeight/2));
noise->visit();
// 4: Call CCRenderTexture:end
rt->end();
// 5: Create a new Sprite from the texture
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
Can someone please help me because I am a bit new to texturing.
Thanks for reading.