【问题标题】:Cocos2dx CCRenderTexture - OpenGL ES2 not renderedCocos2dx CCRenderTexture - OpenGL ES2 未渲染
【发布时间】:2012-11-15 04:02:12
【问题描述】:

我正在尝试为 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());

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

【问题讨论】:

    标签: cocos2d-iphone opengl-es-2.0 cocos2d-x


    【解决方案1】:

    看起来我们必须使用 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);
    

    【讨论】:

    • 谢谢!请问您最终是如何找到解决方案的?
    【解决方案2】:

    我不太确定它是如何工作的,但如果你只是想要一些带有渐变颜色的东西,你可以尝试或检查 CCLayerGradient 的源代码, 这是链接http://www.cocos2d-x.org/reference/native-cpp/d9/d19/classcocos2d_1_1_c_c_layer_gradient.html

    【讨论】:

    • 谢谢,但我想了解为什么代码不起作用。因为教程有一些额外的东西要在 OpenGL 中绘制,如果这个初始步骤不起作用,我将无法继续前进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多