Cocos2dx CCRenderTexture - OpenGL ES2未渲染

3
我正在尝试在Cocos2dx 2、OpenGL ES2中向纹理添加渐变。 但OpenGL内容无法被渲染。
这段代码基于以下教程链接: http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture
CCRenderTexture *rt = CCRenderTexture::create((int)textureSize, (int)textureSize);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

float gradientAlpha = 0.5;
CCPoint vertices[4];
ccColor4F colors[4];
int nVertices = 0;

vertices[nVertices] = CCPointMake(0, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = CCPointMake(textureSize, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = CCPointMake(0, textureSize);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);
vertices[nVertices] = CCPointMake(textureSize, textureSize);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);

// Set the shader program for OpenGL
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
ccGLBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

CCSprite* noise = CCSprite::create("Noise.png");
ccBlendFunc blendFunc;
blendFunc.src = GL_DST_COLOR;
blendFunc.dst = GL_ZERO;
noise->setBlendFunc(blendFunc);
noise->setPosition(ccp(textureSize / 2, textureSize / 2));
noise->visit();

rt->end();
return CCSprite::createWithTexture(rt->getSprite()->getTexture());

如果我做错了什么,请告诉我。谢谢。

2个回答

4

看起来我们需要使用ccVertex2F而不是CCPoint来表示顶点。

此外,混合函数在绘制渐变时并不是必需的。

以下是更新后的C++代码:

float gradientAlpha = 0.5;
ccVertex2F vertices[4];
ccColor4F colors[4];
int nVertices = 0;

vertices[nVertices] = vertex2(0, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = vertex2(textureSize, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = vertex2(0, textureSize);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);
vertices[nVertices] = vertex2(textureSize, textureSize);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);

// Set the shader program for OpenGL
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(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);

glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

谢谢!我可以问一下你最终是怎么找到解决方案的吗? - Zaky German

1

谢谢,但我想了解为什么代码无法工作。因为教程中还有一些要在OpenGL中绘制的额外内容,如果这个初始步骤不能正常运行,我就无法继续进行了。 - Paramesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接