1

我正在尝试创建 OpenGL ES 1.1 纹理并填充渲染内容。

准备纹理:

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

GLuint texFrambuffer;
glGenFramebuffers(1, &texFrambuffer);
glBindFramebuffer(GL_FRAMEBUFFER, texFrambuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
if(status != GL_FRAMEBUFFER_COMPLETE) {
    NSLog(@"failed to make complete framebuffer object %x", status);
    // this message doesn't appear, thus, framebuffer is complete
}

glViewport(0, 0, 32, 32);

glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

GLfloat vertices[] = {
    10.0f, 10.0f, 0.0f,
    20.0f, 40.0f, 0.0f,
    10.0f, 30.0f, 0.0f
};
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &texFrambuffer);

将纹理渲染到场景中:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GLshort texCoord[] = {0, 0, 0, 1, 1, 0, 1, 1};
GLfloat vertices2[] = {0.0, 0.0, 0.0, 31.0f, 31.0f, 0.0f, 31.0f, 31.0f};
glVertexPointer(2, GL_FLOAT, 0, vertices2);
glTexCoordPointer(2, GL_SHORT, 0, texCoord);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisable(GL_TEXTURE_2D);

完整的源代码可在 Bitbucket 获得:https ://bitbucket.org/amjaliks/gl2/src/66ee4932e212/GL2/GL2View.m

还有其他内容正确呈现。问题仅在于此纹理 - 它呈现为白色矩形。

我研究了很多例子,但仍然无法弄清楚我做错了什么。什么不见​​了?


更新 1:我注意到,如果我在绑定时设置glClearColor()并调用,这些更改将保存到我的纹理中。因此,有一些问题和/或相关的事情。glClear()texFrambufferglDrawArrays()

我需要专门为我的设置矩阵模式和投影texFrambuffer吗?


更新 2:我也尝试使用glCopyTexImage2D将内容从“主”帧缓冲区复制到纹理。

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glViewport(0, 0, 32, 32);

glClearColor(1.0f, 0.75f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
GLfloat vertices[] = {
    0.0f, 0.0f, 0.0f,
    20.0f, 32.0f, 0.0f,
    10.0f, 30.0f, 0.0f
};
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

glBindTexture(GL_TEXTURE_2D, texture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 32, 32, 0);

Bitbucket 提供完整源代码:https ://bitbucket.org/amjaliks/gl2/src/b3339661650e/GL2/GL2View.m

结果是一样的。仅填充矩形。看起来我的矩阵肯定有问题。

4

1 回答 1

0

你的问题是矩阵。默认情况下,投影矩阵是恒等式。因此为视口提供 (-1,-1)-(+1,+1) 坐标。很可能您需要正交投影到 (0,0)-(32,32) 正方形。

于 2012-03-06T09:24:49.583 回答