我正在尝试遵循 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);
它介于 CCRenderTexturebegin
和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 混合功能?任何帮助将非常感激。提前致谢。