Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can't draw sf::Sprites over modern OpenGL context  (Read 2000 times)

0 Members and 1 Guest are viewing this topic.

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Can't draw sf::Sprites over modern OpenGL context
« on: November 08, 2012, 07:55:02 pm »
Hi, I'm experiencing an issue with OpenGL and SFML. I'm using SFML 2.0 and OpenGL 3.2.

Basically, I'd like to draw an in game GUI that is drawn over the OpenGL context, when OpenGL is not being drawn, my GUI shows fine. However, as soon as I start drawing something (with glDrawElements, the GUI vanishes. I do draw the GUI after I draw my objects, and I keep UI drawing inside window.pushGLStates() and window.popGLStates();. Here is my game loop:

    while (window.isOpen())
    {
        window.setActive();
        sf::Event event;

        while (window.pollEvent(event)) // Event loop
        {
            switch (event.type)
            {
                // Will send most input to the InputHandler to be processed there after the event has ended
                case sf::Event::MouseButtonPressed:
                    inputHandler.mouseClicked();
                    break;

                case sf::Event::KeyPressed:
                    inputHandler.keyPressed(event.key.code);
                    break;

                case sf::Event::Closed:
                    closeGame();
                    break;

                case sf::Event::GainedFocus:
                    inputHandler.gainedFocus();
                    break;

                case sf::Event::LostFocus:
                    inputHandler.lostFocus();
                    break;

                case sf::Event::Resized:
                    inputHandler.windowWasResized = true;
                    break;

                case sf::Event::MouseMoved:
                    inputHandler.mouseMoved = true;
                    break;

                default:
                    break;
            }
        }

        if (!closing)
            inputHandler.onFrame();

        if (renderingGL && !closing)
        {
            currentCamera->onTick(); // Refresh camera
            renderer->render(); // This draws the OpenGL stuff with glDrawElements!
        }

        window.pushGLStates();
        fontRenderer.drawString("Hello World!", 50, 50, 30, 0, 0, 0, &window); // This draws a nice "Hello World" in the corner when OpenGL is not being drawn to. Otherwise, it dissapears.
        window.popGLStates();


        window.display();
    }
 

I suspect my issue might be because I'm defining the matrices myself and passing to my shaders instead of using glPush/PopMatrix.

If you need more code, just ask.
Thanks,

Jishaxe
« Last Edit: November 08, 2012, 07:59:11 pm by Laurent »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't draw sf::Sprites over modern OpenGL context
« Reply #1 on: November 08, 2012, 11:45:13 pm »
Laurent answered this on the french forum a few weeks ago ( http://fr.sfml-dev.org/forums/index.php?topic=9314.msg63202#msg63202 ) :
Add glBindBuffer(GL_ARRAY_BUFFER, 0); before your pushGLStates or your drawString.

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Can't draw sf::Sprites over modern OpenGL context
« Reply #2 on: November 09, 2012, 06:11:36 pm »
Thank you! I have to unbind all my buffers

        glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind all the buffers
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

And it works fine! Thanks, G :)