OpenGL depth test problem
I've got a problem with OpenGL on mac, and I think the problem is the Depth test.
So, to my problem: Rather than explaining, I made two screenshots: My scene from far: http://c0848462.cdn.cloudfiles.rackspacecloud.com/dd2267e27ad7d0206526b208cf2ea6910bcd00b4fa.jpg And from near: http://c0848462.cdn.cloudfiles.rackspacecloud.com/dd2267e27a561b5f02344dca57508dddce21d2315f.jpg
If I do not draw the green floor, everything looks (kinda) fine. But still, like this it looks just aweful.
Here are the three codeblocks I use to set up Opengl:
+ (NSOpenGLPixelFormat*) defaultPixelFormat
{
NSOpenGLPixelFormatAttribute attributes [] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16,
(NSOpenGLPixelFormatAttribute)nil
};
return [[[NSOpenGLPixelFormat al开发者_开发知识库loc] initWithAttributes:attributes] autorelease];
}
- (void) prepareOpenGL
{
NSLog(@"Preparing OpenGL");
glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
glEnable(GL_TEXTURE_2D);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
- (void)reshape
{
NSLog(@"Reshaping view");
glViewport( 0, 0, (GLsizei)[self bounds].size.width, (GLsizei)[self bounds].size.height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);
That's way too small of a near clip plane. The closer your near clip value is to 0, the less precision you get on values that are farther away. Push your near clip back to at least 1.0, if not farther. In general, you should push it back as far away as you can live with.
Oh, and you should be using a 24-bit depth buffer, not 16-bit.
精彩评论