【发布时间】:2012-08-09 09:34:13
【问题描述】:
我正在尝试学习 Ray Wenderlich 的 iOS 动态纹理教程
http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture
但使用 Cocos2D 2.0 和 OpenGL ES 2.0 而不是 1.1。本教程首先在屏幕上绘制一个彩色正方形,并对其应用阴影渐变,但我无法将渐变渲染到彩色正方形。本教程的这一部分是将 OpenGL ES 代码发送到 CCRenderTexture 的地方,所以我认为我的 OpenGL ES 2.0 代码设置错误(我对 OpenGL / OpenGL ES 的经验很少)。 OpenGL ES 1.1 代码是
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
float gradientAlpha = 0.7;
CGPoint vertices[4];
ccColor4F colors[4];
int nVertices = 0;
vertices[nVertices] = CGPointMake(0, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0 };
vertices[nVertices] = CGPointMake(textureSize, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = CGPointMake(0, textureSize);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = CGPointMake(textureSize, textureSize);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_FLOAT, 0, colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
它介于 CCRenderTexture begin 和 end 方法之间(完整代码可以在上面的链接中找到)。我的 Cocos2D 2.0 / OpenGL ES 2.0 尝试是
float gradientAlpha = 0.7;
CGPoint vertices[4];
ccColor4F colors[4];
int nVertices = 0;
vertices[nVertices] = CGPointMake(0, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0 };
vertices[nVertices] = CGPointMake(textureSize, 0);
colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
vertices[nVertices] = CGPointMake(0, textureSize);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
vertices[nVertices] = CGPointMake(textureSize, textureSize);
colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
// Setup OpenGl ES shader programs
CCGLProgram *positionColourProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];
[rt setShaderProgram:positionColourProgram];
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);
rt 是 CCRenderTexture 对象。控制台中没有错误,但屏幕上的图像是没有渐变的纯色正方形。我是否需要使用 OpenGL 混合功能?任何帮助将非常感激。提前致谢。
【问题讨论】:
-
我也有同样的问题。你有运气吗?
-
不,害怕。我一直在学习更多 Open GL ES 2.0 并且在您感兴趣后又进行了一次尝试,但仍然没有进展
[rt set shaderProgram:positionColourProgram]应该是[[[rt sprite] texture] setShaderProgram:positionColourProgram]因为它是用于创建新 Sprite 的 CCRenderTexture 的 Sprite 纹理。 CCRenderTexture 的beginWithClear:和end方法应该是进一步研究的地方,但我一直在使用 iOS 5+ 的 GLKit,所以没有帧缓冲区等方面的经验,所以现在无法理解这些方法。
标签: ios cocos2d-iphone opengl-es-2.0