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

Author Topic: Push/Pop GL state  (Read 5931 times)

0 Members and 1 Guest are viewing this topic.

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Push/Pop GL state
« on: July 30, 2009, 09:59:27 pm »
Currently, one can tell a render target to preserve OpenGL state, but this means the complete render state is pushed/popped for each and every drawable. Most of the time when combining OpenGL rendering and SFML rendering, we have a large section of GL code (3D rendering) and a large section of SFML code (GUI overlays etc). Therefore it would be far more efficient to push GL state once before using SFML and popping it once afterward (instead of for each individual sprite, font etc.).

Currently I solved this by making my own (see below), but adding these to the API might be useful.

Code: [Select]

// taken from RenderTarget::Draw and RenderTarget::SetRenderStates
void _preSFML()
{
glMatrixMode(GL_MODELVIEW); glPushMatrix();
glMatrixMode(GL_PROJECTION); glPushMatrix();
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT   | GL_ENABLE_BIT  |
            GL_TEXTURE_BIT      | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);

glDisable(GL_ALPHA_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
}

void _postSFML()
{
glMatrixMode(GL_PROJECTION); glPopMatrix();
glMatrixMode(GL_MODELVIEW);  glPopMatrix();
glPopAttrib();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Push/Pop GL state
« Reply #1 on: July 30, 2009, 11:43:53 pm »
It's far more efficient to keep this on your side, as you know exactly what to save/restore, and when. The SFML's solution is a global one provided for convenience (and people that are not used to OpenGL).
Laurent Gomila - SFML developer

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Push/Pop GL state
« Reply #2 on: July 30, 2009, 11:51:51 pm »
Quote from: "Laurent"
It's far more efficient to keep this on your side, as you know exactly what to save/restore, and when. The SFML's solution is a global one provided for convenience (and people that are not used to OpenGL).


In this case I decided to take the simple path and save everything SFML would save (Saving once is always more effective though). I just thought there might be other people who'd like to have a simple way of pushing&popping gl state once to render their SFML objects.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Push/Pop GL state
« Reply #3 on: February 03, 2011, 10:22:33 am »
My program crashes when I call pushattrib(GL_LIGHTING_BIT). What's wrong?

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Push/Pop GL state
« Reply #4 on: February 03, 2011, 10:32:50 am »
Quote from: "tntexplosivesltd"
My program crashes when I call pushattrib(GL_LIGHTING_BIT). What's wrong?


it shouldn't crash... driver error? but you don't really need it, as SFML doesn't use lighting. and the enable state of lighting is already covered by GL_ENABLE_BIT

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Push/Pop GL state
« Reply #5 on: February 03, 2011, 10:41:22 am »
Do I need to call pushattrib each frame? What about popattrib? Before the polygon stuff?

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
Push/Pop GL state
« Reply #6 on: February 03, 2011, 10:51:20 am »
of course you need to call both push and pop every frame. example:

drawOpenGL()
pushAttrib/_preSFML()
drawSFML()
popAttrib/_postSFML()
(drawOpenGL() could also happen here, just never between push+pop)

if you don't do this, your OpenGL state stack will overflow. also if you don't pop the attributes from the stack, what is the point in pushing them? ;)

 

anything