8

我使用SFML创建窗口。

在这个屏幕截图中,立方体应该在金字塔的后面,但它不起作用。 截屏

这是我使用的最小代码:

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

我试过不同的深度函数,GL_LESS,GL_EQUAL,我都试过了。同样在不同的地方启用和禁用深度测试,似乎没有任何效果。

我正在运行 Mac OS X 10.7 ( Lion ),不确定这是否重要,尽管在升级之前我似乎对这些事情没有任何问题。

4

2 回答 2

14

你的代码看起来不错。我怀疑您的窗口根本没有深度缓冲区。您正在使用sf::RenderWindow,其文档说(强调我的):

简单的包装器sf::Window允许简单的2D渲染。

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

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

您可以将StencilBitsand设置AntialiasingLevel为 0,因为此示例不需要它们。

于 2012-01-28T14:17:34.463 回答
2

在最新版本的 SFML 中WindowSettingsContextSettings. 深度设置可以配置为。

//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);
于 2016-06-16T11:43:28.400 回答