3

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

This

...

And if I don't enable it Dratted Cube

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

2
  • 1
    If you're beginning to learn OpenGL, you should not learn immediate mode. It's slow, cumbersome and deprecated from recent versions of OpenGL, never was available in OpenGL-ES. I recommend Nicol Bolas' tutorials, they're the best currently out there arcsynthesis.org/gltut
    – datenwolf
    Sep 18, 2011 at 10:31
  • Wow, thank you very much, that is super helpful. Of course now I'm reading instead of programming, so curse you, curse you datenwolf the platypus. Curse you. Sep 20, 2011 at 12:57

1 Answer 1

3

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.

1
  • 1
    Just set it to 1.0. Don't listen to the world claim that you need to set it to 0.000001.
    – TheBuzzSaw
    Sep 22, 2011 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.