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

Author Topic: making custom save/restore OpenGL states  (Read 3474 times)

0 Members and 2 Guests are viewing this topic.

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
making custom save/restore OpenGL states
« on: August 29, 2011, 04:52:31 pm »
So I've been working through the tutorial and it suggested not to use the built in save/restore GL states because they are slow. I got it working fine with those functions, but I don't want to rely on them because they are slow.

These are the opengl settings that I change when I want to draw something with opengl

Code: [Select]

        glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, 800, 600, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);


my question is how I can undo these changes so I can also draw things with sfml. How am I supposed to know what the starting values of these were?
[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
making custom save/restore OpenGL states
« Reply #1 on: August 29, 2011, 05:04:04 pm »
In fact you should use Save/RestoreGLStates as long as it doesn't actually hurt your performances. Don't start doing complicated things if it's not worth it.
Laurent Gomila - SFML developer

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
making custom save/restore OpenGL states
« Reply #2 on: August 30, 2011, 01:18:38 am »
I'm getting real close! Here are my custom load/save sates!

Code: [Select]

void Game::OnOpenGL(sf::RenderWindow* a)
{
        // Save a copy of the projection matrix so that we can restore it
        // when it's time to do 3D rendering again.
        glMatrixMode( GL_PROJECTION );
        glPushMatrix();
        glLoadIdentity();

        // Set up the orthographic projection
        glOrtho (0, 800, 600, 0, 0, 1);
        glMatrixMode( GL_MODELVIEW );
        glPushMatrix();
        glLoadIdentity();

        // Make sure depth testing and lighting are disabled for 2D rendering until
        // we are finished rendering in 2D
        glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
        glDisable( GL_DEPTH_TEST );
        glDisable( GL_LIGHTING );
}


Code: [Select]

void Game::OffOpenGL(sf::RenderWindow* a)
{
//a->SaveGLStates();
glPopAttrib();
        glMatrixMode( GL_PROJECTION );
        glPopMatrix();
        glMatrixMode( GL_MODELVIEW );
        glPopMatrix();

}


That works fine except when I try to draw text (after switching to sfml drawing) instead of getting letters I just get squares. The weird thing is that if I use sfml to draw a sprite and then try to draw text it works fine. What could that be?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
making custom save/restore OpenGL states
« Reply #3 on: August 30, 2011, 07:49:40 am »
It's the alpha-blending mode (glEnable(GL_BLEND), glBlendFunc).
Laurent Gomila - SFML developer

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
making custom save/restore OpenGL states
« Reply #4 on: August 30, 2011, 02:39:07 pm »
But I never touched those functions!

I did touch glColor4f which I set back to
Code: [Select]

glColor4f(255, 255,255, 1.0f);

before sfml drawing.


Also sfml correctly draws transparent sprites after the switch. It appears to be just the text. Weird!

h4tt3n

  • Newbie
  • *
  • Posts: 7
    • View Profile
making custom save/restore OpenGL states
« Reply #5 on: August 30, 2011, 04:57:54 pm »
I'm struggling with the same problem. How exactly do you combine opengl and text/strings without using the preserveopenglstates command?

Cheers,
Mike

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
making custom save/restore OpenGL states
« Reply #6 on: August 30, 2011, 08:57:55 pm »
In fact it's more complicated than the documentation suggests, because you have to know which states SFML needs for rendering.

You can look at the source code of sf::Renderer and sf::RenderTarget.
Laurent Gomila - SFML developer