Can't get depth testing to work in OpenGL

这一生的挚爱 提交于 2019-11-29 10:44:37

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!