【问题标题】:OpenGL depth test problemOpenGL深度测试问题
【发布时间】:2011-07-08 20:48:12
【问题描述】:

我在 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();
} 

【问题讨论】:

  • 感谢您提出这个问题。对于我的一生,我无法弄清楚为什么 GL_DEPTH_TEST 不起作用。在我覆盖 defaultPixelFormat 并设置深度大小后,它开始工作了。

标签: objective-c opengl depth nsopenglview


【解决方案1】:
gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);

方式太小了近剪裁平面。您的近剪辑值越接近 0,您在更远的值上获得的精度就越低。将您的近端剪辑推回至少 1.0,如果不是更远的话。一般来说,你应该尽可能地把它往后推。

哦,您应该使用 24 位深度缓冲区,而不是 16 位。

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2018-07-21
    相关资源
    最近更新 更多