2012-01-28 17 views
5

Io uso SFML per creare la finestra.Impossibile eseguire il test di profondità per funzionare in OpenGL

In questo screenshot il cubo dovrebbe essere dietro la piramide ma non funziona. Screenshot

Ecco il codice minimo che ho usato:

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

Ho provato diverse funzioni di profondità, GL_LESS, GL_EQUAL, li ho provati tutti. Anche abilitando e disabilitando i test di profondità su luoghi diversi, nulla sembra funzionare.

Utilizzo Mac OS X 10.7 (Lion), non sono sicuro che sia importante, anche se non sembra che abbia problemi con questo tipo di cose prima dell'aggiornamento.

risposta

9

Il tuo codice sembra a posto. Sospetto che la tua finestra non abbia un buffer di profondità. Stai usando sf::RenderWindow, la cui documentation dice (sottolineatura mia):

involucro semplice per sf::Window che permette una facile il rendering 2D .

non so SFML, ma questo tutorial suggerisce per creare la tua finestra come questa:

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

È possibile impostare StencilBits e AntialiasingLevel-0 poiché questo esempio non ha bisogno di loro.

+2

Grazie mille! Ho esaminato il mio codice per giorni e ho dimenticato di aggiornare SFML a 2.0, che disabilita il buffer di profondità (apparentemente). Per chiunque altro si imbattesse in questo problema: WindowSettings non esiste più in SFML 2.0, invece usa ContextSettings. – RadicalRaid

0

Nell'ultima versione di SFML WindowSettings sostituito da ContextSettings. Le impostazioni di profondità possono essere configurate come.

//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); 
Problemi correlati