OpenGL深度Spaz攻击

时间:2011-09-18 00:03:18

标签: c++ opengl sdl

我今天开始学习OpenGL,这简直太棒了。但是我不能为了我的生活而根据深度绘制对象,而不是绘制顺序,所以我希望有人可以告诉我我做错了什么。

这是我用来创建多维数据集的非常简单的代码:

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

 //Simple translation

 glBegin(GL_QUADS);     
  glColor3f(0.0f,1.0f,0.0f);    
  glVertex3f( 1.0f, 1.0f,-1.0f);    
  glVertex3f(-1.0f, 1.0f,-1.0f);    
  glVertex3f(-1.0f, 1.0f, 1.0f);    
  glVertex3f( 1.0f, 1.0f, 1.0f);    
  glColor3f(1.0f,0.5f,0.0f);    
  glVertex3f( 1.0f,-1.0f, 1.0f);    
  glVertex3f(-1.0f,-1.0f, 1.0f);    
  glVertex3f(-1.0f,-1.0f,-1.0f);    
  glVertex3f( 1.0f,-1.0f,-1.0f);    
  glColor3f(1.0f,0.0f,0.0f);    
  //You get the point, continue with all sides 
 glEnd();           // End Drawing The Cube

SDL_GL_SwapBuffers();

以下是设置代码:

if (SDL_Init(SDL_INIT_EVERYTHING)<0)
    return -1;

SDL_GL_SetAttribute(SDL_GL_RED_SIZE,        8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,      8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,       8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,      8);

SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,      16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,        32);

SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,    8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,    8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    8);

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2);


SDL_Surface* screen = SDL_SetVideoMode(screen_x,screen_y,32,SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL);

if (screen == NULL)
    return -2;

//glEnable(GL_DEPTH_TEST); //<-If this is uncommented look at figure 1
glDepthFunc(GL_LESS);

glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);

glViewport(0, 0, screen_x, screen_y);

glMatrixMode(GL_PROJECTION); //projection with ortho, model otherwise
glLoadIdentity();

gluPerspective(60.0,1.0,0.0,10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

好吧,当我

glEnable(GL_DEPTH_TEST);

This

...

如果我启用它 Dratted Cube

那么我应该做什么?我肯定错过了什么?如何解决我的深度问题?

1 个答案:

答案 0 :(得分:3)

我找到了答案!

http://www.opengl.org/resources/faq/technical/depthbuffer.htm

这里是我找到答案的地方,我的近帧正好是0,它需要稍微远离0才能使深度缓冲区的精度生效。 (如果我读得正确的话) 一切都很好。