0

I currently have 16 tiles, with individual images that make up 1 big map. I pan by transforming right at the beginning before any actual drawing with this:

GL.Translate(G_.Pan(0), G_.Pan(1), 0)

Then I zoom by doing this:

GL.Ortho(-G_.Size * 1.5 ^ G_.ZoomFactor, G_.Size * 1.5 ^ G_.ZoomFactor, G_.Size * 1.5 ^ G_.ZoomFactor, -G_.Size * 1.5 ^ G_.ZoomFactor, -1, 1)

G_.Size is a constant that only varies on startup depending on parameters, zoom factor ranges from -1 to -13

What I want to be able to do is check if 1 of the 16 tiles is within the visible area, so then I stop them drawing when they are not on screen. I had found some quite complex methods for doing it, but it was 3D and seemed like a lot of work for something that should be simple. I would of thought it would of been something like just checking if a point is within the bounds of visible area, but I have no idea on how to get the visible area.

  • 2
    The technique you were referring to was probably frustum culling, and while an orthogonal projection does not produce the traditional model of a frustum (pyramid shape with the tip cut off), the basic concept still applies. You can obtain 4 clipping planes from your model view and projection matrices and test if your 2D point is inside all of them. You don't need the near/far plane since this is 2D. – Andon M. Coleman Dec 16 '13 at 04:10
  • 1
    And your final statement: "I have no idea on how to get the visible area" is why I suggest you borrow from the concept of frustum culling. You have a ModelViewProjection matrix in all cases, and it is trivial to extract the top/left/bottom/right planes from it without **any** knowledge of your values of `G_.Size` or what it means to XOR a floating-point number (**1.5**) by `G_.ZoomFactor`. – Andon M. Coleman Dec 16 '13 at 04:26
  • Extracting the planes, is not trivial for me, Im still new to OpenGL, how do I extract the planes? – The Empire Strikes Back Dec 16 '13 at 07:11
  • @TheEmpireStrikesBack That's up to you to find out, we're not here to do your research or teach you things! – vallentin Dec 16 '13 at 07:57
  • I googled it lots before posting that comment and I barely found anything. Especially since he said it was trivial it makes it sound like it just a parameter but nothing leads me to that. – The Empire Strikes Back Dec 16 '13 at 08:44
  • @AndonM.Coleman In VB `^` is the exponentiation operator, so that expression isn't crazy. – GuyRT Dec 16 '13 at 09:55
  • @GuyRT: Yeah, you are right. If it had not been the middle of the night when I wrote those comments I probably would have noticed "VB.NET" in the browser title / tags. I just saw parenthesis and automatically assumed a C-like language :) – Andon M. Coleman Dec 16 '13 at 16:29

1 Answers1

3

Andon M Coleman already suggested you to implement projection volume culling (a generalized form of frustum culling). This is however outside the scope of OpenGL. You must understand that OpenGL is not a "magical" scene graph that does scene management and the likes. It's mere drawing API; what it does is putting shaded, textured points, lines or triangles on the screen and that's it. The rest is up to you, or the libraries you choose to implement it.

In the case of projection volume culling you're testing if a given piece of geometry intersects with the volume defined by the planes that form the borders of the volume. Your projection matrix defines such planes, specifically it transform the view space vertex position volume into the range [-1;1]×[-1;1]×[0;1] of perspective divided clip space. So by inverting the projection matrix and unprojection the corners of the [-1;1]×[-1;1]×[0;1] cube through that you determine the limiting planes of the projection volume in view space.

You then use that information to intersect your quads with the volume to see if they cross it, i.e. are in any way visible.

datenwolf
  • 159,371
  • 13
  • 185
  • 298