Can't get depth testing to work in OpenGL

前端 未结 2 1225
南笙
南笙 2020-12-06 18:12

I use SFML to create the window.

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

相关标签:
2条回答
  • 2020-12-06 18:43

    In latest version of SFML WindowSettings replaced by ContextSettings. Depth settings can be configured as.

    //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);
    
    0 讨论(0)
  • 2020-12-06 18:44

    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.

    0 讨论(0)
提交回复
热议问题