1

我正在尝试遵循 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);

它介于 CCRenderTexturebeginend方法之间(完整的代码可以在上面的链接中找到)。我的 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);

rtCCRenderTexture 对象在哪里。控制台中没有错误,但屏幕上的图像是没有渐变的纯色正方形。我是否需要使用 OpenGL 混合功能?任何帮助将非常感激。提前致谢。

4

1 回答 1

2

我已经弄清楚了使它\工作所需的更改,并在教程的论坛http://www.raywenderlich.com/forums//viewtopic.php?f=20&t=512&start=40上发表了我的评论 希望不是太你迟到了。

为了节省您浏览论坛查找我的帖子的时间,这是我在那里发布的内容:

I have posted my fix at: http://www.wfu.edu/~ylwong/download/cocos2d-2.0-texture/ The HelloWorldLayer.mm is the final file incorporated all the changes so you do not have to type them in. The pdf file marks up the changes in case you want to see what the changes are.

Basically, in addition to replacing the statements that are not supported in OpenGLES 2.0, I have to add code to set up the shaders for vertex and fragment. Also, instead of using the range 0 to textureSize in the vertex arrays, I have to use the range -1 to 1, which means that in setting up the vertex arrays, the texture width is now 2, 0 becomes -1, and textureSize becomes 1.

To set up the shaders for that tutorial, we can use the ones that come with Cocos2d or write custom but simple shaders. I have included both methods to choose from.

Hope this helps!

于 2012-08-09T01:34:13.810 回答