OpenGL深度问题

4

我在使用OpenGL渲染深度方面遇到了问题。

以下代码是出现问题的简单示例。它在同一位置呈现了两个梯形,其中一个在旋转。但是旋转的那个始终显示在顶部,即使在旋转时应该在第一个梯形后面。

我猜我在初始化OpenGL时弄错了什么。在搜索stackoverflow时,我找到了一篇帖子,有人建议执行以下代码并查看输出。

int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
printf("%i bits depth", depth);

输出结果是0位深度,我猜这不太好 :(

我正在Mac上使用xCode和glfw库进行开发。

#include <GL/glfw.h>
#include <stdlib.h>

int main( void )
{
   int running = GL_TRUE;

   if( !glfwInit() )
   {
      exit( EXIT_FAILURE );
   }

   // Open an OpenGL window
   if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW ))
   {
      glfwTerminate();
      exit( EXIT_FAILURE );
   }

   glEnable(GL_DEPTH_TEST);

   float angle = 0;

   // Main loop
   while( running )
   {

      double elapsedTime = glfwGetTime();
      glfwSetTime(0);
      angle += 90 * elapsedTime;

      // OpenGL rendering goes here...
      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      glPushMatrix(); //Save the transformations performed thus far
      glColor3f(1.0f, 1.0, 0.0);
      glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid
      //glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis
      glBegin(GL_QUADS);

      //Trapezoid
      glVertex3f(-0.7f, -0.5f, 0.0f);
      glVertex3f(0.7f, -0.5f, 0.0f);
      glVertex3f(0.4f, 0.5f, 0.0f);
      glVertex3f(-0.4f, 0.5f, 0.0f);

      glEnd();

      glPopMatrix();

      glPushMatrix(); //Save the transformations performed thus far
      glColor3f(1.0f, 0.0, 0.0);
      glTranslatef(0.0f, 0.0f, 0.0f); //Move to the center of the trapezoid
      glRotatef(angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis
      glBegin(GL_QUADS);

      //Trapezoid
      glVertex3f(-0.7f, -0.5f, 0.0f);
      glVertex3f(0.7f, -0.5f, 0.0f);
      glVertex3f(0.4f, 0.5f, 0.0f);
      glVertex3f(-0.4f, 0.5f, 0.0f);

      glEnd();

      glPopMatrix(); //Undo the move to the center of the trapezoid

      glfwSwapBuffers();

      // Check if ESC key was pressed or window was closed
      running = !glfwGetKey( GLFW_KEY_ESC ) &&
      glfwGetWindowParam( GLFW_OPENED );
   }

   // Close window and terminate GLFW
   glfwTerminate();
   // Exit program
   exit( EXIT_SUCCESS );
}
3个回答

5

如果( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW ))

为什么要传递所有这些零?这些参数非常重要。 GLFW 参考文档 (pdf) 显示这些参数描述了您想要的颜色、深度和模板的位数。倒数第二个零是深度位数。您请求帧缓冲区的深度位数为0,因此您得到了0位深度。

您不能责怪 API 做您所要求的事情 ;)

一个更合理的命令是:

if( !glfwOpenWindow( 640,480, 8, 8, 8, 8, 24, 8, GLFW_WINDOW ))

这为RGBA提供了8位,深度提供了24位,模板缓冲区提供了8位。

2
如果您在多边形上绘制时,不能保证绘制顺序就是渲染顺序。在一个驱动程序上可能是一种方式,在另一个驱动程序上可能是另一种方式,不同供应商的驱动程序也是如此。您必须在glVertex命令中添加微小的深度差异或使用glPolygonOffset()进行调整。或者在第一个多边形之后执行glFlush(),但这非常低效。通过使用深度缓冲区,您告诉显卡可以按任意顺序渲染三角形,因为深度缓冲区将对前面绘制的内容进行排序。这就是为什么在某些游戏中,当它们映射到相同的深度值时,您会看到多边形互相闪烁(特别是远处的物体)。

如果您想使用深度缓冲区,请启用深度缓冲区 :)。 - Chris Mennie

0

glGetIntegerv( GL_DEPTH_BITS, ..) is deprecated in OpenGL 3

use instead :

int GetDepthBits()
{
  HDC hdc = wglGetCurrentDC();
  int iPixelFormat = GetPixelFormat( hdc);
  int iAttr[] = {WGL_DEPTH_BITS_ARB};
  int iDepthBits;
  wglGetPixelFormatAttribivARB( hdc, iPixelFormat, 0, 1, iAttr, & iDepthBits);
  return iDepthBits;
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接