屏幕外渲染的纹理在屏幕上渲染空矩形



我正在尝试创建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:我注意到,如果在绑定texFrambuffer时设置glClearColor()并调用glClear(),这些更改将保存到我的纹理中。因此,glDrawArrays()和/或相关事物存在问题。

我需要为我的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

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

您的问题是矩阵。默认情况下,投影矩阵为单位。从而给出视口的(-1,-1)-(+1,+1)坐标。很可能你需要正交投影到(0,0)-(32,32)平方。

相关内容

  • 没有找到相关文章

最新更新