Hi!
Did go from V2 to V3 but now i got an annoying error/issue in openGL? Used to get gradient but now i got plain color in V2 but after i’ve updated the code it doesn’t work as it should…
This is the code:
[code]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 = 0.7;
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->setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
CC_NODE_DRAW_SETUP();
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_COLOR | GL::VERTEX_ATTRIB_FLAG_POSITION);
glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
Sprite* noise = Sprite::create("Noise.png");
noise->setScaleY(1536/512);
noise->setScaleX(2048/512);
BlendFunc myBlend;
myBlend.src = GL_DST_COLOR;
myBlend.dst = GL_ZERO;
noise->setBlendFunc(myBlend);
noise->setPosition(Vec2(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());
}
[/code]
So, this works in V2 ofc without the updating of the code…But i know it should give the same…But in V3 it just gives a plain color, no gradient??
THEN i searched on google for some solutions and i found one without any comments on but it was from one of cocos2d-x’s developers so that should work too, here’s the code from him:
[code]Sprite* HelloWorld::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
CustomCommand _customCommand;
// 3: Draw into the texture
//Caution!!! if you want to use your own opengl draw call ,you must wrap them with a custom command
_customCommand.init(rt->getGlobalZOrder());
_customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw,this);
auto renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_customCommand);
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
void HelloWorld::onDraw()
{
this->setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
this->getGLProgram()->use();
this->getGLProgram()->setUniformsForBuiltins();
Size winSize = Director::getInstance()->getVisibleSize();
float gradientAlpha = 0.7;
Vec2 vertices[4];
Color4F colors[4];
int nVertices = 0;
vertices[nVertices] = Vec2(0, 0);
colors[nVertices++] = (Color4F){0, 0, 0, 0 };
vertices[nVertices] = Vec2(winSize.width, 0);
colors[nVertices++] = (Color4F){0, 0, 0, 0};
vertices[nVertices] = Vec2(0, winSize.height);
colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = Vec2(winSize.width, winSize.height);
colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_COLOR | GL::VERTEX_ATTRIB_FLAG_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
}
[/code]
But that gives me a plain color too!! This is so annoying and that’s one of the reasons i won’t change to V3
but everyone says i should…
Please help, Thanks!!
Much appreciated…
@slackmoehrle, @owen, @pandamicro…I think the second code snippet was from owen, so maybe you know that i shall do? I just tagged you guys because i know u are in with all this!
Thanks again!