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

Author Topic: Prepare OpenGL state for SFML drawing  (Read 5824 times)

0 Members and 1 Guest are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Prepare OpenGL state for SFML drawing
« on: January 21, 2013, 07:52:02 pm »
In my graphics application I use OpenGL calls, vertex buffers and shaders for rendering a scene. Onto that scene I want to draw text using SFML's graphics capabilities. (I use the last SFML 2.0 snapshot and SFML's internaly used GLEW.)

Since SFML internally uses OpenGL for drawing, I need to store the OpenGL state before, reset it for SFML, and restore it afterwards. I know that there is a function called "RenderTarget::pushGLStates()" which first stores the current OpenGL state and resets it to the SFML defaults. But I figured out that this function isn't enough.

Using just the function mentioned above there is no text displayed even if I comment out the draw calls for the scene. (By the way, font loading is successful.)

// draw scene using OpenGL
// ...

// store the state
window.pushGLStates();

// draw text using SFML
Text text("Hello World", font, 15);
window.draw(text);

// restore the state
window.popGLStates();

// display and swap buffers
// ...

Still not drawing scene objects, added the following calls for resetting the OpenGL state, the text is displayed.

// draw scene using OpenGL
// ...

// store the state
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
window.pushGLStates();

// draw text using SFML
Text text("Hello World", font, 15);
window.draw(text);

// restore the state
window.popGLStates();

// display and swap buffers
// ...

But when I enable the scene drawing again, no text is displayed. What of the OpenGL state do I have to reset by myself for displaying text using SFML? Do I have to care about shader uniforms I set for rendering the scene?
« Last Edit: January 21, 2013, 08:11:08 pm by sharethis »

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: Prepare OpenGL state for SFML drawing
« Reply #1 on: January 21, 2013, 11:22:02 pm »
When you push(), the state doesn't change, it's just saved on the stack. So you need to resetGLStates() after you push to get SFML to set the correct internal opengl state it needs. (I think)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Prepare OpenGL state for SFML drawing
« Reply #2 on: January 22, 2013, 08:41:35 am »
No, resetGLStates is called by pushGLStates, asa explained in the doc ;)

This function is not perfect yet, so you have to reset some states manually. Especially unbind buffers and shaders.
Laurent Gomila - SFML developer

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Prepare OpenGL state for SFML drawing
« Reply #3 on: January 22, 2013, 03:15:59 pm »
You have to reset some states manually. Especially unbind buffers and shaders.

Thanks, I got it working by disabling the vertex arrays ("glDisableVertexAttribArray").

But there is one thing I would like to fix. When I resize the window, the text gets skewed. What could be the reason for that?
« Last Edit: January 22, 2013, 07:25:18 pm by sharethis »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Prepare OpenGL state for SFML drawing
« Reply #4 on: January 22, 2013, 08:33:27 pm »
You have to adapt the view (sf::View) to the new size of the window.
Laurent Gomila - SFML developer

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: Prepare OpenGL state for SFML drawing
« Reply #5 on: January 22, 2013, 10:15:21 pm »
No, resetGLStates is called by pushGLStates, asa explained in the doc ;)

Oh right, my bad. I was thinking it worked like openGL.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Prepare OpenGL state for SFML drawing
« Reply #6 on: January 23, 2013, 07:47:04 pm »
Thanks Laurent, it works like a charm.

I didn't even know about "sf::View" since I never used SFML's drawing capabilities before.

tbop

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Prepare OpenGL state for SFML drawing
« Reply #7 on: June 13, 2013, 05:28:23 pm »
Hi,


I've tried to follow the example but it hasn't been very successful so far.

Briefly, I'm in the same situation where I need to combine at some part of my code real openGL calls with some utility functions of RenderTexture such as polygons, circle everything to get more straightforward source code.

Here's the rendering routine that gets call all the time  but only the triangle will get drawn.

BOOL renderSC()
{  
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

        glPushMatrix();

        glColor3f(0, 1, 1);
        glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
                glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
                glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
                glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
                glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
                glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
                glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
        glEnd();

        glPopMatrix();

        sf::RenderTexture rt;
        rt.create (h, w);
        sf::CircleShape shape(100.f);
        shape.setFillColor (sf::Color::Green);
        rt.pushGLStates ();
        rt.draw (shape);
        rt.popGLStates ();

        glFlush();

        return 0;
}
 

What am I doing wrong here according to you folks?

Cheers!
« Last Edit: June 13, 2013, 05:44:12 pm by tbop »