0

我一直在尝试将茶壶渲染到 FBO,然后将后续纹理用作纹理贴图。似乎什么都没有出现,所以我想知道我做错了什么。下面是主循环:

//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");
}

然而,我得到的只是没有纹理映射的立方体。

4

2 回答 2

1

这里有两个可能的错误:

  1. 渲染到 FBO 失败。要检查是否是这种情况,请使用不同的背景颜色(例如粉红色)清除 FBO,并查看纹理是否受到影响(例如,通过将其读回 CPU)。您还应该确保您的转换矩阵和视口 ( glViewport) 是正确的。

  2. 纹理映射失败。确保您已经为纹理映射设置了所有内容,并首先使用静态纹理测试纹理映射。

于 2012-05-28T23:55:05.327 回答
0

怎么样glEnable ( GL_TEXTURE_2D )

于 2012-05-28T23:49:45.610 回答