# OpenGL Depth Buffer/Coordinate issue with SFML

For some reason, my coordinate system in OpenGL is getting all messed up with depth. When I put something more than 1 unit away from zero (in terms of the Z coordinate), I can't see the object. Furthermore, it seems as if the GL coordinate (-1, -1, 0) is in the lower left corner no matter what I do (and (1, 1, 0) is upper right). I've used OpenGL in Java and in C++ with GLUT before and am now trying out SFML, and haven't had these issues before, so maybe someone can tell me what I'm doing wrong?

A minimal example as best I can do:

#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

#include <iostream>

int main()
{
sf::ContextSettings settings;
sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL Test", sf::Style::Default, sf::ContextSettings(24));

settings = window.getSettings();

std::cout << "depth bits:" << settings.depthBits << std::endl;
std::cout << "stencil bits:" << settings.stencilBits << std::endl;
std::cout << "antialiasing level:" << settings.antialiasingLevel << std::endl;
std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    // Black Background
glDepthRange(-1, 1000);

// run the main loop
bool running = true;
while (running)
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Comment this line to fix it!!?
glTranslatef(-1.5f,0.0f,-6.0f);

glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
glEnd();                            // Finished Drawing The Triangle

window.display();
}

return 0;
}


Update: The glDepthRange() call was to make sure that wasn't the source of the problem. Also I added the following code to the initialization, and I haven't seen any change in behavior:

glMatrixMode(GL_PROJECTION);
glViewport(0, 0, 800, 600);

glMatrixMode(GL_MODELVIEW);
...


Also, I'm using the 2.0RC version of SFML, and my OpenGL version is 4.3 (running on an ATI card).

• I've haven't read too far into your question, but the coordinates you're describing sound like normalized device coordinates, and skimming over your code, it looks like this would happen because you haven't set the model-view-projection matrix. – kevintodisco Dec 27 '12 at 4:19
• @Max why are you calling 'glDepthRange(-1, 1000)', ay reason for this? As ktodisco pointed out, you are also not setting a projection matrix. – Stephan van den Heuvel Dec 27 '12 at 5:32
• @ktodisco See edits. Thanks for the quick response :) – Max Feldkamp Dec 27 '12 at 6:02
• Hmm, close, but I don't think glViewport is what you need. Try using gluPerspective. Also be warned that OpenGL's immediate mode is deprecated. With newer versions you're expected to manually implement the view and projection matrix calculations (commonly in a shader). – kevintodisco Dec 27 '12 at 7:21
• @ktdisco Putting in gluPerspective(50, 800/600, -1, 1000); gets me to a completely black screen. I was aware immediate mode is deprecated, but I expected it to at least work considering it's worked elsewhere. I'll try seeing if this code works in GLUT, and I'll see if implementing a shader fixes things... – Max Feldkamp Dec 27 '12 at 16:36