我正在尝试创建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()
时调用texFrambuffer
,则这些更改会保存到我的纹理中。因此,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
结果是一样的。只填充矩形。看起来我的矩阵肯定存在问题。
答案 0 :(得分:0)
你的问题在于矩阵。默认情况下,投影矩阵是标识。因此为视口提供(-1,-1) - (+ 1,+ 1)坐标。很可能你需要正交投影到(0,0) - (32,32)平方。