Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

sfml - Can't get depth testing to work in OpenGL

I use SFML to create the window.

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...