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);
glLoadIdentity();
// 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);
glLoadIdentity();
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).
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... \$\endgroup\$