开发者

OpenGL Depth Spaz Attack

I've begun learning OpenGL today, and it's just plain fantastic. However I cannot for the life of me make objects draw according to depth, instead of drawing order, so I hope someone can tell 开发者_如何学运维me what I'm doing wrong.

Here's the extremely simple code I'm using to create a cube:

 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();

Here's the set up code:

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();

Well, when I

glEnable(GL_DEPTH_TEST);

OpenGL Depth Spaz Attack

...

And if I don't enable it

OpenGL Depth Spaz Attack

So what am I supposed to do? I must be missing ... something? How do I fix my depth issue?


I found the answer!

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

Here's where I found the answer, My near frame was at exactly 0, it needs to be slightly away from 0 in order for the depth buffer's precision to take effect. (If I read that correctly) All is well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜