![](/img/trans.png)
[英]OpenGL 1 pixel high texture rendering difference Windows/Linux vs OS X
[英]Problems rendering to a texture using FBO in OS X 10.7 but not Linux
我有一個測試程序,它以編程方式渲染紋理,然后渲染一個簡單的四邊形,該四邊形使用之前繪制的紋理進行蒙皮。 該程序非常簡單,一切都在 2D 中完成。 該程序在 linux 下運行良好,一切看起來都應該如此:
但是,當我在運行 10.7 的 Mac 上運行該程序時,什么也沒有顯示:
我完全不知道為什么這個程序不能在我的 Mac 上運行。
這是程序的相關部分。
創建紋理:
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();
}
創建幀緩沖區 Object:
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);
}
渲染到紋理:
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);
}
渲染帶紋理的四邊形:
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,我的顯示回調很簡單:
void displayCB(){
drawToTexture();
drawTexturedQuad();
glutSwapBuffers();
}
此外,我還有其他方法檢查 OpenGL 是否支持 FrameBufferObjects,如果不支持則程序退出。 此外,只要調整 window 的大小時,我還有其他邏輯會設置textureSize
。
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4
這可能是個問題。 GL規范並沒有明確說明應該在什么時候生成mipmaps,當然,這會產生渲染到紋理的問題。 因此 GL_EXT_framebuffer_object(以及核心版本)引入了 glGenerateMipmapEXT,您可以在您想要生成 mipmap 的地方調用它(通常在渲染到紋理之后)。
因此,刪除 GL_GENERATE_MIPMAP 內容並在需要時手動使用 glGenerateMipmap。 您可以在GL_EXT_framebuffer_object 規范中閱讀有關此問題的更多信息。
您確定您的緩沖區 ID 是 0 而不是 1 嗎? 也許之前已經創建了另一個 FBO。
同時檢查紋理尺寸,嘗試使它們成為 2 的冪
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.