OpenGL深度测试问题

Ivo*_*ius 2 opengl objective-c depth nsopenglview

我在Mac上遇到了OpenGL的问题,我认为问题是深度测试.

所以,我的问题:从很远的我的场景:,而不是解释,我做了两个截图http://c0848462.cdn.cloudfiles.rackspacecloud.com/dd2267e27ad7d0206526b208cf2ea6910bcd00b4fa.jpg 而从近:HTTP://c0848462.cdn.cloudfiles .rackspacecloud.com/dd2267e27a561b5f02344dca57508dddce21d2315f.jpg

如果我不画绿色地板,一切看起来都很好.但是,像这样,它看起来只是令人敬畏.

以下是我用来设置Opengl的三个代码块:

+ (NSOpenGLPixelFormat*) defaultPixelFormat
{
    NSOpenGLPixelFormatAttribute attributes [] = {
        NSOpenGLPFAWindow,
        NSOpenGLPFADoubleBuffer, 
        NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, 
        (NSOpenGLPixelFormatAttribute)nil
    };
    return [[[NSOpenGLPixelFormat alloc] 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();
} 
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 5

gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);
Run Code Online (Sandbox Code Playgroud)

这是这样太小近裁平面.您的近剪辑值越接近0,您获得的距离越远的值的精度就越低.将近夹子推回至少1.0,如果不是更远的话.一般来说,你应该尽可能远地将它推回去.

哦,你应该使用24位深度缓冲,而不是16位.