OpenGL深度问题 [英] OpenGL depth problem

查看:165
本文介绍了OpenGL深度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenGL中渲染深度有问题

I'm having problem with rendering depth in OpenGL

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

Following code is a simple example of code in which problem occurs. It renders 2 trapezoids in same place, and one of them rotates. But the rotating one is always displayed on top, even though it should go behind the first trapezoid while rotating.

我想我初始化OpenGL搞砸了.同样,在搜索stackoverflow时,我发现了一个帖子,有人建议执行以下代码并查看输出.

I guess I messed up something with initializing OpenGL. Also while searching through stackoverflow i found a post where someone suggested to execute following code and see the output.

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

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

well the output is, 0 bits depth, which isn't good I guess :(

我正在Mac上使用带有glfw库的xCode开发

I'm developing on a Mac, in xCode with glfw library

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

推荐答案

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

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

为什么您要传递所有这些零?这些参数很重要. GLFW参考文档(pdf)显示,这些参数描述了您需要多少位的颜色,深度和模版.倒数第二个是深度位数.您要求帧缓冲区的深度为0位,这就是您所得到的.

Why do you pass all of those zeros? Those parameters are kind of important. The GLFW reference documentation (pdf) shows that those parameters describe how many bits you want for the colors, depth, and stencil. The next-to-last zero is the number of depth bits. You asked for 0 bits of depth for the framebuffer, so that's what you got.

您不能责怪API来完成您的要求;)

You can't blame the API for doing what you asked ;)

更合理的命令是:

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

这将为您提供8位的RGBA,24位的深度和8位的模板.

This gives you 8 bits each for RGBA, 24 for depth, and 8-bits of stencil.

这篇关于OpenGL深度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆