1
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Dec 27, 2012 at 4:19
  • \$\begingroup\$ @Max why are you calling 'glDepthRange(-1, 1000)', ay reason for this? As ktodisco pointed out, you are also not setting a projection matrix. \$\endgroup\$ Dec 27, 2012 at 5:32
  • \$\begingroup\$ @ktodisco See edits. Thanks for the quick response :) \$\endgroup\$ Dec 27, 2012 at 6:02
  • \$\begingroup\$ 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). \$\endgroup\$ Dec 27, 2012 at 7:21
  • \$\begingroup\$ @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... \$\endgroup\$ Dec 27, 2012 at 16:36

1 Answer 1

0
\$\begingroup\$

It appears that the issue was a combination of setting the perspective matrix correctly, ensuring a proper coordinate system, and getting the right number of depth bits/ensuring the depth buffer was properly enabled. It looks like sf::RenderWindow is intended for 2D rendering, so I should be using sf::Window instead as well. Still having issues with gluLookAt(), but that's a different problem.

Update: I believe a big part of the problem was coming from 'glEnable(GL_CULL_FACE)' and then proceeding to use the wrong winding... whoops.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .