OpenGL深度问题

时间:2011-06-16 22:25:15

标签: xcode macos opengl glfw

我在OpenGL中渲染深度有问题

以下代码是发生问题的代码的简单示例。它在同一个地方渲染出2个梯形,其中一个旋转。但旋转的一个总是显示在顶部,即使旋转时应该在第一个梯形后面。

我想我通过初始化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 个答案:

答案 0 :(得分:5)

  

if(!glfwOpenWindow(640,480,0,0,0,0,0,0,GLFW_WINDOW))

为什么你通过所有这些零?那些参数很重要。 GLFW reference documentation (pdf)表明这些参数描述了颜色,深度和模板所需的位数。倒数第二个零是深度位的数量。你要求帧缓冲器有0位深度,这就是你得到的。

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

更合理的命令是:

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

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

答案 1 :(得分:2)

如果您在彼此之上绘制多边形,则保证您绘制它们的顺序将是它们呈现的顺序。它可以在一个驱动器上单向工作,在另一个驱动器上工作。与来自不同供应商的驱动程序相同。

您必须在glVertex命令中添加微小的深度差异,或者使用glPolygonOffset()。或者在第一个多边形之后执行glFlush(),这是非常低效的。

通过使用深度缓冲区,你告诉显卡它可以按照它选择的顺序渲染三角形,因为深度缓冲区将整理出前面绘制的内容。这就是为什么在某些游戏中,当它们映射到相同的深度值时,你会看到相互闪烁的多边形(特别是距离内的东西)。

答案 2 :(得分:0)

glGetIntegerv(GL_DEPTH_BITS,..)在OpenGL 3中已弃用

改为使用:

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;
}
相关问题