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

Author Topic: Tips and hints on how to correctly use glScissor with SFML?  (Read 6365 times)

0 Members and 1 Guest are viewing this topic.

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Tips and hints on how to correctly use glScissor with SFML?
« on: February 15, 2013, 03:42:48 am »
Hi!

Since SFML doesn't (yet) have functionality for clipped drawing,
and thankfully my requirements are very low (ie. only rectangle clipping areas),
I intend to use glScissor for that. However, I'm a complete newb in terms of openGL,
so besides calling
Code: [Select]
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, w, h);
before every call to window.draw(...) there isn't much I'd know about.
I feel like calling the glEnable every time I draw seems unnecessary (as I will always use it),
but I don't know where the right place would be to call it once.
Also, do I need to call window.setActive() so openGL knows which context to apply
the scissor test to?
And how should I do all this when I'm using render textures in between normal draws
to the screen? Do I need to "refresh" the scissor box for every render target when I switch them?
Ie. -> set scissor box -> draw to window -> draw to other render target
-> scissor box gone, need to set again ?
(Sorry if this sounds confusing, I'm just not very knowledgeable as to how context and state
like this are managed under the hood in openGL)

A minimal example of using glScissor, maybe with an alternating clip box based on some conditional
would be greatly appreciated :D

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #1 on: February 15, 2013, 05:48:25 am »
sfml 2.0 has it, :) its worth switching, I don't know if 1.6 has sub rectangling, however

Sfml 2.0, C++:
sf::Rect SubRectangle(x1,y1,x2,y2);
sf::Sprite.SetSubRect(SubRectangle);
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #2 on: February 15, 2013, 05:59:57 am »
sfml 2.0 has it, :) its worth switching, I don't know if 1.6 has sub rectangling, however

Sfml 2.0, C++:
sf::Rect SubRectangle(x1,y1,x2,y2);
sf::Sprite.SetSubRect(SubRectangle);

Sorry, should have stated it, but I have always been working with 2.0 (never even looked at 1.6)
Your solution is not what I'm looking for =/ I need per render target clipping

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #3 on: February 15, 2013, 08:06:46 am »
Don't be afraid to call glEnable every time you need it. And yes, when you switch the active render-target, you need to set it again because each target has its own OpenGL context with its own states. But it sounds strange to apply the same clipping rectangle to multiple render-targets.
Laurent Gomila - SFML developer

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #4 on: February 15, 2013, 09:35:06 am »
And yes, when you switch the active render-target, you need to set it again because each target has its own OpenGL context with its own states.
Ok, just so I understand you correctly, each render target has its own context
which does not keep the scissor state between context switches. Correct?

But it sounds strange to apply the same clipping rectangle to multiple render-targets.
That wasn't my intention, sorry for being unclear ^^"

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #5 on: February 15, 2013, 09:50:01 am »
Quote
Ok, just so I understand you correctly, each render target has its own context
which does not keep the scissor state between context switches. Correct?
Yes. As I said, each OpenGL context has its own states. If you define and activate the scissor on a particular context, it will be activated and defined only on this context.
Laurent Gomila - SFML developer

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #6 on: February 15, 2013, 10:01:15 am »
Quote
Ok, just so I understand you correctly, each render target has its own context
which does not keep the scissor state between context switches. Correct?
Yes. As I said, each OpenGL context has its own states. If you define and activate the scissor on a particular context, it will be activated and defined only on this context.
Wowow, your wording is still confusing me a little  ;D
So, to illustrate what I understood from your sentence:
 -> Create two contexts (render targets)
 -> Activate context one
 -> Set scissor box A
 -> Do some drawing
 -> Activate context two
 -> Do some drawing
 -> Activate context one
 -> Context one's scissor box from before is now gone due to context switch,
     therefore needs to be set to box A again.

So I would basically have to keep track of each clipping area for each render target
(because openGL doesn't do it for me), and apply it after each setActive() call to restore it.
Correct?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #7 on: February 15, 2013, 11:13:21 am »
No... I said that each context has its states, not that these states were reseted every time you switched the contexts. Once a state is set in a context, it stays there until it is set again with another value.
Laurent Gomila - SFML developer

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #8 on: February 15, 2013, 11:20:21 am »
No... I said that each context has its states, not that these states were reseted every time you switched the contexts. Once a state is set in a context, it stays there until it is set again with another value.

Oh ok. I was just confused because I said
Quote
Ok, just so I understand you correctly, each render target has its own context
which does not keep the scissor state between context switches. Correct?
And you said yes.. turns out it's actually a no because the contexts
do in fact retain their individual scissor states between switches. (They don't lose it on switch)
So once I specified a clipping box for a context, I don't have to worry about
having to set it again, unless I want to change it.
Conclusively, this means I don't have to call glEnable over and over again,
just once for each context.

Thanks for the clarifications^^
« Last Edit: February 15, 2013, 11:22:17 am by Ancurio »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Tips and hints on how to correctly use glScissor with SFML?
« Reply #9 on: February 15, 2013, 11:24:06 am »
You're right, my previous answer was really confusing. Sorry ;D
Laurent Gomila - SFML developer

 

anything