FBO和纹理

时间:2019-02-25 03:52:44

标签: opengl glsl fbo

我在FBO上附加了两个纹理。第一个纹理将用于显示深度图。第二个纹理将以正常方式显示对象。

如果我这样做,它将很好地工作并向我显示深度图。

GLuint atach0 = GL_DEPTH_ATTACHMENT;
glBindFramebuffer(GL_FRAMEBUFFER,fboBuffer);
  glDrawBuffers(1,&atach0);
  glClear(GL_DEPTH_BUFFER_BIT);
  glViewport(0.0,0.0,640,480);
  LProjection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,-500.0f,500.0f);
  LView = glm::lookAt(glm::vec3(0.0f,15.0f,0.000001f),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
  LViewProjection = LProjection * LView;
  glEnable(GL_DEPTH_TEST);

  ...CUBE3D

glBindFramebuffer(GL_FRAMEBUFFER,0);

glUniform1i(uniforTEX,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,GL_DEPTH_MAP);

  ...DRAW-DEPTH-MAP!

glBindTexture(GL_TEXTURE_2D,0);

但是,如果我这样做,它只会显示白色屏幕,并且不再显示深度图。

GLuint atach0 = GL_DEPTH_ATTACHMENT;
glBindFramebuffer(GL_FRAMEBUFFER,fboBuffer);
  glDrawBuffers(1,&atach0);
  glClear(GL_DEPTH_BUFFER_BIT);
  glViewport(auxrecX,auxrecY,auxrcAn,auxrcAl);
  LProjection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,-500.0f,500.0f);
  LView = glm::lookAt(glm::vec3(0.0f,15.0f,0.000001f),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
  LViewProjection = LProjection * LView;
  glEnable(GL_DEPTH_TEST);

  ...CUBE3D

glBindFramebuffer(GL_FRAMEBUFFER,0);

GLuint atach1 = GL_COLOR_ATTACHMENT0;
glBindFramebuffer(GL_FRAMEBUFFER,fboBuffer);
  glDrawBuffers(1,&atach1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glViewport(0,0,640,480);
  Projection = glm::perspective(45.0f,1.333333f,0.01f,1000.0f);
  View = glm::lookAt(glm::vec3(0.0f,0.0f,3.0),glm::vec3(0.0f,0.0f,-15.0f),glm::vec3(0.0f,1.0f,0.0f));
  ViewProjection = Projection * View;
  glEnable(GL_DEPTH_TEST);

  ...CUBE3D

glBindFramebuffer(GL_FRAMEBUFFER,0);   

glUniform1i(uniforTEX,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,GL_DEPTH_MAP);

  ...DO NOT DRAW NOTHING

glBindTexture(GL_TEXTURE_2D,0);

我需要按顺序进行。因为第一次“通过”是为了在阴影图中使用深度图。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

GL_DEPTH_ATTACHMENTglDrawBuffers无效。 glDrawBuffers个缓冲区,片段着色器数据的输出将写入其中。

这样做的时候

GLuint atach0 = GL_DEPTH_ATTACHMENT;
glBindFramebuffer(GL_FRAMEBUFFER,fboBuffer);

然后您将收到一个GL_INVALID_ENUM错误。

如果您不想使用深度图,请禁用使用glDepthMask的深度测试。深度缓冲区附件不能打开和关闭,也不能像颜色缓冲区那样使用。

进一步

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

在任何情况下都会清除深度缓冲区。

我认为存在一个基本的误解,即必须为阴影映射设置帧缓冲区。您仅需要一个用于阴影贴图的帧缓冲区。该帧缓冲区必须仅具有深度缓冲区。

您必须这样做:

GLuint fboShadow;      // framebuffer with depth buffer only (shadow map)
GLuint toShadowDepth;  // texture which is the depth buffer of `fboShadow`
glBindFramebuffer(GL_FRAMEBUFFER, fboShadow);
GLuint atach0 = GL_NONE;
glDrawBuffers(1, &atach0);
glClear(GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glViewport(auxrecX, auxrecY, auxrcAn, auxrcAl);

// draw the shadow map to the depth buffer only

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 640, 480);

glUniform1i(uniforTEX, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, toShadowDepth);

// draw the geometry to the default framebuffer by using the depth map