Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL GL_DEPTH_TEST not working

I just ported a .obj loader to objective-C and so far, it works, I can get my vertices and normals and stuff. Every normal is good, pointing in the right direction, all my faces are in CCW winding, but I have some issues with the depth test.

float rotX = 0;
float rotY = 0;
objModel* o = [[objModel alloc] initWithPath:@"/model.obj"]

glClearColor(0,0,0,0);

glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glCullFace(GL_BACK); 
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();   
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0, -1, 0);
glRotatef(90,0,0,1);
glRotatef(90,0,1,0);
glRotatef(rotX,0,0,-1);
glRotatef(rotY,0,1,0);

[o drawObjWithArrays]

glFlush();

I have 2 different ways of drawing my object, one uses glBegin() / glEnd(), the other uses vertex and normal arrays with a call to glDrawArrays(). Both of them result in the same problem : faces that should be hidden by faces in front of them are displayed, because the depth test isn't working. The faces are drawn in the order they come in the .obj file.

You'll find an image here : http://img524.imageshack.us/img524/994/image2jgq.png

I quite new to OpenGL and objective-C, so I guess my problem comes from a setting that I forgot. Here they are :

-(id) initWithFrame:(NSRect) frame {
NSLog(@"INIT GL VIEW\n");

GLuint attributes[] = {
    NSOpenGLPFANoRecovery,
    NSOpenGLPFAWindow,
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAColorSize, 24,
    NSOpenGLPFAAlphaSize, 8,
    NSOpenGLPFADepthSize, 24,
    NSOpenGLPFAStencilSize, 8,
    NSOpenGLPFAAccumSize, 0,
    0
};

NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:(NSOpenGLPixelFormatAttribute*) attributes];
if (!fmt)
    NSLog(@"No OpenGL pixel format");

GLfloat   mat_ambient[]     = {0.0, 0.0, 1.0, 1.0};
GLfloat   mat_flash[]       = {0.0, 0.0, 1.0, 1.0};
GLfloat   mat_flash_shiny[] = {50.0};
GLfloat   light_position[]  = {100.0,-200.0,-200.0,0.0};
GLfloat   ambi[]            = {0.1, 0.1, 0.1, 0.1};
GLfloat   lightZeroColor[]  = {0.9, 0.9, 0.9, 0.1};

/* set the material */
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);

glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);  
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);

return self = [super initWithFrame:frame pixelFormat:[fmt autorelease]]; 
}

Anyone can help me ? I've tried everyone glDepthFunc, glCullFace, glFrontFace combination possible, nothing works...

Thanks ^^

like image 574
melka Avatar asked Jun 06 '09 20:06

melka


People also ask

How does OpenGL depth buffer work?

The depth buffer is automatically created by the windowing system and stores its depth values as 16 , 24 or 32 bit floats. In most systems you'll see a depth buffer with a precision of 24 bits. When depth testing is enabled, OpenGL tests the depth value of a fragment against the content of the depth buffer.

What is depthtest?

Depth Testing is a testing technique in which feature of a product is tested in full detail. Each of the feature is tested exhaustively during the integration phase and the defects are logged, are captured across all parameters, functional and non functional.


3 Answers

what do you get when you add this to your draw method?

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

A few more things to try:

  • make sure initWithFrame is being called
  • if you're using an NSOpenGLView subclass from IB, set the depth buffer in IB, and move all your openGL initialization into - (void)prepareOpenGL
like image 127
cobbal Avatar answered Nov 01 '22 18:11

cobbal


In case someone wonders how to set this without IB:

NSOpenGLPixelFormatAttribute attrs[] = {
    // NSOpenGLPFADoubleBuffer,
    NSOpenGLPFADepthSize, 32,
    0
};
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:frame pixelFormat:format];
like image 25
Anna B Avatar answered Nov 01 '22 19:11

Anna B


Make sure you call glDepthFunc() to set the depth buffer comparison function. Most applications use GL_LEQUAL or GL_LESS for the depth function. Also make sure you call glClearDepth() to set the value that the depth buffer gets cleared to; you should probably use a parameter of 1.0 for this to clear to maximum depth.

like image 39
Adam Rosenfield Avatar answered Nov 01 '22 18:11

Adam Rosenfield