Problems rendering to a texture using FBO in OS X 10.7 but not Linux
我有一个测试程序,该程序以编程方式渲染纹理,然后渲染带有先前绘制的纹理蒙皮的简单四边形。该程序非常简单,一切都以2D形式完成。该程序在linux下运行良好,一切看起来都应该像这样:
但是,当我在运行10.7的Mac上运行该程序时,没有任何显示:
我完全不知道为什么该程序无法在我的Mac上运行。
这是程序的相关位。
创建纹理:
1 2 3 4 5 6 7 8 9 10 11 | void createTextureObject(){ cout<<"Creating a new texture of size:"<<textureSize<<endl; //glDeleteTextures(1, &textureId); glGenTextures(1, &textureId); glBindTexture(GL_TEXTURE_2D, textureId); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureSize, textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); isTextureValid = true; createFBObject(); } |
创建帧缓冲区对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | void createFBObject(){ cout<<"Creating a new frame buffer object"<<endl; glDeleteFramebuffers(1, &fboId); glGenFramebuffersEXT(1, &fboId); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId); glGenRenderbuffersEXT(1, &rboId); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, textureSize, textureSize); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboId); bool status = checkFramebufferStatus(); if(!status){ cout<<"FrameBufferObject not created! Quitting!"<<endl; exit(1); fboUsed = false; } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); } |
渲染到纹理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | void drawToTexture(){ if (!isTextureValid) createTextureObject(); glViewport(0, 0, textureSize, textureSize); glLoadIdentity(); // set the rendering destination to FBO glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId); // clear buffer glClearColor(0, 0, 0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw to the texture glColor3f(0.0, 1.0, 0.0); for (int i=1; i<=5; i++){ glBegin(GL_LINE_LOOP); glVertex2f(-1 * i/5.0f, -1 * i/5.0f); glVertex2f( 1 * i/5.0f, -1 * i/5.0f); glVertex2f( 1 * i/5.0f, 1 * i/5.0f); glVertex2f(-1 * i/5.0f, 1 * i/5.0f); glEnd(); } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); } |
渲染纹理四边形:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | void drawTexturedQuad(){ glViewport(50, 50, textureSize, textureSize); glLoadIdentity(); glClearColor(0.2, 0.2, 0.2, 0.2); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, textureId); int size = 1; int texS = 1; glBegin(GL_QUADS); glColor4f(1, 1, 1, 1); glTexCoord2f(texS, texS); glVertex3f(size, size,0); glTexCoord2f(0, texS); glVertex3f(-1 * size , size,0); glTexCoord2f(0, 0); glVertex3f(-1 * size, -1 * size,0); glTexCoord2f(texS, 0); glVertex3f(size, -1 * size,0); glEnd(); glBindTexture(GL_TEXTURE_2D, 0); } |
我正在使用GLUT,我的显示回叫很简单:
1 2 3 4 5 | void displayCB(){ drawToTexture(); drawTexturedQuad(); glutSwapBuffers(); } |
另外,我还有其他方法来检查OpenGL是否支持FrameBufferObjects,如果不是,则退出程序。此外,我还有其他逻辑可以在每次调整窗口大小时设置
1 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4 |
这可能是个问题。 GL规范没有确切说明应在何时生成Mipmap,当然,这会导致渲染到纹理的问题。 因此GL_EXT_framebuffer_object(以及核心版本)引入了glGenerateMipmapEXT,您可以在要生成mipmap的位置(通常在渲染到纹理之后)完全调用它。
因此,删除GL_GENERATE_MIPMAP内容,并在需要时手动使用glGenerateMipmap。 您可以在GL_EXT_framebuffer_object规范中阅读有关此问题的更多信息。
您确定缓冲区ID为0而不是1吗?
也许之前已经创建了另一个FBO。
同时检查纹理尺寸,尝试使其为2的幂