【问题标题】:Using OpenGL ES 2.0 inside CCRenderTexture在 CCRenderTexture 中使用 OpenGL ES 2.0
【发布时间】: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 beginend 方法之间(完整代码可以在上面的链接中找到)。我的 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


【解决方案1】:

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

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

我已在以下位置发布了我的修复: http://www.wfu.edu/~ylwong/download/cocos2d-2.0-texture/ HelloWorldLayer.mm 是包含所有更改的最终文件,因此您不必输入它们。pdf 文件会标记更改,以防您想查看更改内容。

基本上,除了替换 OpenGLES 2.0 中不支持的语句之外,我还必须添加代码来设置顶点和片段的着色器。另外,在顶点数组中,我必须使用范围-1到1,而不是使用范围0到textureSize,这意味着在设置顶点数组时,纹理宽度现在为2,0变为-1,textureSize变为 1。

要为该教程设置着色器,我们可以使用 Cocos2d 附带的着色器或编写自定义但简单的着色器。我已经包含了两种方法可供选择。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2012-02-17
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多