无法在 OpenGL 中进行深度测试 [英] Can't get depth testing to work in OpenGL

查看:41
本文介绍了无法在 OpenGL 中进行深度测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 说(强调我的):

<块引用>

sf::Window 的简单包装器,允许轻松的 2D 渲染.

我不知道 SFML,但是这个 教程 建议创建您的窗口像这样:

sf::WindowSettings 设置;Settings.DepthBits = 24;//请求一个 24 位深度的缓冲区Settings.StencilBits = 8;//请求一个 8 位的模板缓冲区Settings.AntialiasingLevel = 2;//请求 2 级抗锯齿sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);

您可以将 StencilBitsAntialiasingLevel 设置为 0,因为本示例不需要它们.

I use SFML to create the window.

In this screenshot the cube should be behind the pyramid but it just doesn't work.

Here is the minimal code I used:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

void resize();
void drawScene();
void initGL();

float rtri = 0;
float rquad = 0;
float z = -10.0f;
int main (int argc, const char * argv[])
{
    // Create the main window
    sf::RenderWindow *window = new sf::RenderWindow();
    window->Create( sf::VideoMode( 800, 600, 32 ), "Collision Detection", sf::Style::Close );

    sf::Event event;

    bool run = true;
    initGL();
    resize();
    while( run ) {
        window->PollEvent( event );
        if( event.Type == sf::Event::Closed ) {
            run = false;
        }

        drawScene();
        window->Display();
//        z+= 0.001f;
    }

    return EXIT_SUCCESS;
}

void resize() {
    glViewport(0,0, 800,600);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,800/600,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity();                   // Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,-6.0f);      // Move Left 1.5 Units And Into The Screen 6.0
    glRotatef(rtri,0.0f,1.0f,0.0f);     // Rotate The Triangle On The Y axis ( NEW )
    glBegin(GL_TRIANGLES);  
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f(0.0f,1.0f,0.0f);  
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f( 1.0f,-1.0f, 1.0f);  
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f( 1.0f,-1.0f, 1.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f( 1.0f,-1.0f, -1.0f); 
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f( 1.0f,-1.0f, -1.0f); 
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f(-1.0f,-1.0f, -1.0f); 
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f(-1.0f,-1.0f,-1.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f(-1.0f,-1.0f, 1.0f);  
    glEnd();                        

    glLoadIdentity();               // Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,z);      // Move Right 1.5 Units And Into The Screen 7.0
    glRotatef(rquad,1.0f,1.0f,z);   // Rotate The Quad On The X axis ( NEW )
    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);      
    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,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(0.0f,0.0f,1.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,1.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);
    glEnd();                                            // Done Drawing The Quad

    rtri+=0.2f;                                         // Increase The Rotation Variable For The Triangle ( NEW )
    rquad-=0.15f;
    z-=0.01;
}

void initGL() {
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable( GL_CULL_FACE );

    /* Position the camera */
    glTranslatef(0, 0, -5);
}

I've tried different depth functions, GL_LESS, GL_EQUAL, I've tried them all. Also enabling and disabling depth testing on different places, nothing seems to work.

I'm running Mac OS X 10.7 ( Lion ), not sure if that is important, though I didn't seem to have any trouble with these kind of things before upgrading.

解决方案

Your code looks okay. I suspect that your window simply does not have a depth buffer. You're using sf::RenderWindow, whose documentation says (emphasis mine):

Simple wrapper for sf::Window that allows easy 2D rendering.

I don't know SFML, but this tutorial suggests to create your window like this:

sf::WindowSettings Settings;
Settings.DepthBits         = 24; // Request a 24 bits depth buffer
Settings.StencilBits       = 8;  // Request a 8 bits stencil buffer
Settings.AntialiasingLevel = 2;  // Request 2 levels of antialiasing
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);

You could set StencilBits and AntialiasingLevel to 0 since this example doesn't need them.

这篇关于无法在 OpenGL 中进行深度测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆