深度缓纹理

//
  // Create A Texture Object Storing Color
  glGenTextures( 1, &m_uDepthTexId ) ;
  glBindTexture( GL_TEXTURE_2D, m_uDepthTexId ) ;
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_nImageWidth, m_nImageHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  glBindTexture( GL_TEXTURE_2D, 0 ) ;

  //
  // Create A Frame-buffer Object,
  glGenFramebuffersEXT( 1, &m_uDepthFboId ) ;
  glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_uDepthFboId ) ;
  //
  // Attach A Texture Depth Attachment Point
  glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_uDepthTexId, 0 );  // For Depth
  glDrawBuffer( GL_NONE ); // No Color Buffer
  glReadBuffer( GL_NONE );

  //
  // Restore System Frame-buffer
  glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ) ;
  glDrawBuffer( GL_BACK );
  glReadBuffer( GL_BACK );

 

glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_uDepthFboId ) ;

绘制内容到深度缓冲

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind

 

glBindTexture( GL_TEXTURE_2D, m_uColorTexId ) ;

将纹理映射到其他物体

 

注:如果看不到纹理效果,有可能是设置的视点离物体太大或者太小,使得生成的深度数据集中太大或者太小

你可能感兴趣的:(深度缓纹理)