Question

I'm using OpenGL wigh LWJGL in Java, but that's not important here. I'm not asking for code, but for a hint on how to do this. Language independent.

I have some region (a rectangle for simplicity), and, let's say, a big tiled map which I want to show in this area. The area is not the whole screen, I want to render something around it.

enter image description here

I know about a few approaches, but all are either huge pain or unsuitable.

  • Render the whole tiled map and everything else, including background and the frame, on top - leaving the window. Yes, works, but it'd be pain.
  • Render only visible tiles and only the visible portions of the border tiles. Again, doable but hard, and ie. when I use external font drawing library, I can't just tell it "Hey, stop at this line, there's my border." Not very good approach, I'd say.
  • Some OpenGL magic which I'm not aware of.

Guide me.

Was it helpful?

Solution

When your area is guaranteed to be an axis-aligned rectangle, you can just use glViewport and/or glScissor (the latter together with glEnable(GL_SCISSOR_TEST)) to prevent OpenGL from rendering outside that rectangle.

In case of modifying the viewport, the image resulting just is scaled to fit the viewport rectangle. Using the scissor test, the area is just "cut out", so not scaled with respect to the viewport setting. But the differences do not really even matter -you can get to the same result via both paths by just adjusting your transformations accordingly. Just note that, if you need to call glClear when you render your "tiled map", the clear operation will not be limited by the currently set viewport, but the scissor test will allow you to even limit the clearing.

If you're area can not be described as an axis-aligned rectangle, I'll recommend having a look at the stencil buffer. The algorithm is simple:

  1. Clear stencil buffer to 0.
  2. Just render the shape you want your tiled map to appear _only into the stencil buffer.
  3. When rendering your tiled map, enable stencil test and set it up so that it will discard fragments for pixels the stencil buffer is 0.

Steps 1 and 2 do only have to be done once (as long as your area is not changing, or your windo size). Have a look at the glStencilFunc and glStencilOp functions for details of how to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top