【问题标题】:OpenGL depth problemOpenGL深度问题
【发布时间】:2011-09-16 18:49:51
【问题描述】:

我在 OpenGL 中的渲染深度有问题

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

我想我在初始化 OpenGL 时搞砸了。另外,在通过 stackoverflow 搜索时,我发现有人建议执行以下代码并查看输出的帖子。

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

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

我在 Mac 上使用 glfw 库在 xCode 中开发

#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 );
}

【问题讨论】:

    标签: xcode macos opengl glfw


    【解决方案1】:

    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 位。

    【讨论】:

      【解决方案2】:

      如果您在彼此之上绘制多边形,则保证您绘制它们的顺序将是它们的渲染顺序。它可能在一个驱动程序上以一种方式工作,而在下一个驱动程序上则以另一种方式工作。来自不同供应商的驱动程序也是如此。

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

      通过使用深度缓冲区,您告诉显卡它可以按照它选择的任何顺序渲染三角形,因为深度缓冲区会整理前面绘制的内容。这就是为什么在某些游戏中,当它们映射到相同的深度值时,您会看到多边形相互闪烁(尤其是远处的事物)。

      【讨论】:

      • 如果您想使用深度缓冲区,请启用深度缓冲区:)。
      【解决方案3】:

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多