2012-05-28 220 views
0

我一直在嘗試將一個茶壺渲染爲一個FBO,然後使用隨後的紋理作爲紋理貼圖。似乎沒有出現,所以我想知道我做錯了什麼。下面是主迴路:OpenGL FBO不渲染紋理(C++)

//switch to fbo 
glBindFramebuffer(GL_FRAMEBUFFER, fbo); 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glLoadIdentity(); 
gluLookAt(0.0,0.0,5.0, 
      0.0,0.0,-1.0, 
      0.0f,1.0f,0.0f); 

glLightfv(GL_LIGHT0, GL_POSITION, lpos); 
glRotatef(a,0,1,1); 
glutSolidTeapot(1); 

    //switch to main 

glBindFramebuffer(GL_FRAMEBUFFER, 0); 
glLoadIdentity(); 
gluLookAt(0.0,0.0,5.0, 
      0.0,0.0,-1.0, 
      0.0f,1.0f,0.0f); 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glBindTexture(GL_TEXTURE_2D, img); 


    //draw cube 

glColor4f(1, 1, 1, 1); 
    glBegin(GL_TRIANGLES); 
    // front faces 
    glNormal3f(0,0,1); 
    // face v0-v1-v2 
    glTexCoord2f(1,1); glVertex3f(1,1,1); 
    glTexCoord2f(0,1); glVertex3f(-1,1,1); 
    glTexCoord2f(0,0); glVertex3f(-1,-1,1); 
    ...draws cube 

我已經使用glFramebufferStatus嘗試,它已返回「成功」

glGenTextures(1, &img); 
glBindTexture(GL_TEXTURE_2D, img); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 

glGenFramebuffers(1,&fbo); 
glBindFramebuffer(GL_FRAMEBUFFER,fbo); 
glGenRenderbuffers(1, &depthbuffer);  
glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer); 
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 512, 512); 
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); 

glGenTextures(1, &img); 
glBindTexture(GL_TEXTURE_2D, img); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, img, 0); 

    glBindFramebuffer(GL_FRAMEBUFFER, fbo); 


GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 

if (status==GL_FRAMEBUFFER_COMPLETE) { 
    printf("success\n"); 
} 

但是,我得到的是沒有紋理映射的立方體。

回答

0

glEnable (GL_TEXTURE_2D)怎麼樣?

+0

是的,我已啓用GL_TEXTURE_2D在初始化 – user1422573

1

有兩種可能的錯誤在這裏:

  1. 渲染到FBO失敗。要檢查是否屬於這種情況,請使用不同的背景顏色(例如粉紅色)清除FBO,並查看紋理是否受到影響(例如,通過將其讀回CPU)。您還應該確保您的轉換矩陣和視口(glViewport)是正確的。

  2. 紋理映射失敗。確保你已經爲紋理映射設置了一切,並首先使用靜態紋理測試紋理映射。

+0

良好的故障排除步驟。我認爲我會首先測試紋理映射,因爲知道這些工作使得檢查紋理內容更直接。 –

+0

謝謝,明天我會試試這些步驟。在1的情況下,爲什麼如果FBO是「完整的」,則將FBO渲染爲失敗? – user1422573

+0

如前所述,視口和轉換矩陣可能不適合您的FBO分辨率。我並不認爲FBO破裂,而是你的渲染方式有問題。 – reima