2012-01-28 14 views
5

utilizo SFML para crear la ventana.no puede obtener pruebas de profundidad para trabajar en OpenGL

En esta pantalla el cubo debe estar detrás de la pirámide, pero simplemente no funciona. Screenshot

Aquí es el código mínimo utilicé:

#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); 
} 

He intentado diferentes funciones de profundidad, GL_LESS, GL_EQUAL, he probado todos. También habilita y deshabilita las pruebas de profundidad en diferentes lugares, nada parece funcionar.

estoy ejecutando Mac OS X 10.7 (Lion), no está seguro de si eso es importante, aunque no parecen tener ningún problema con este tipo de cosas antes de actualizar.

Respuesta

9

Tu código se ve bien. Sospecho que tu ventana simplemente no tiene un buffer de profundidad. Estás usando sf::RenderWindow, cuya documentation dice (el énfasis es mío):

envoltura simple para sf::Window que permite un fácil 2D prestación.

no sé SFML, pero esto sugiere tutorial para crear su ventana como esta:

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); 

También podemos establecer StencilBits y AntialiasingLevel a 0, ya que este ejemplo no los necesita.

+2

Muchas gracias! Estuve revisando mi código durante días y olvidé que actualicé SFML a 2.0, lo que desactiva el buffer de profundidad (aparentemente). Para cualquier otra persona que se encuentre con este problema: WindowSettings ya no existe en SFML 2.0, use ContextSettings en su lugar. – RadicalRaid

0

En última versión del SFML WindowSettings reemplazado por ContextSettings. Los ajustes de profundidad se pueden configurar como.

//Configuring SFML window 
sf::ContextSettings window_settings; 
window_settings.depthBits   = 24; // Request a 24-bit depth buffer 
window_settings.stencilBits  = 8; // Request a 8 bits stencil buffer 
window_settings.antialiasingLevel = 2; // Request 2 levels of antialiasing 

// Opening SFML window 
sf::Window window(sf::VideoMode(800, 600), "Title", sf::Style::Resize | sf::Style::Close, window_settings); 
glewExperimental = GL_TRUE; 

// Initializing glew and openGL 
glewInit(); 
glViewport(0, 0, 800, 600); 

// Enabling Depth 
glEnable(GL_DEPTH_TEST); 
Cuestiones relacionadas