Sign in to follow this  
DeGauss

OpenGL Determine if textured quad has left the viewport

Recommended Posts

Hi, I've been gradually working my way through various OpenGL tutorials, and I'm currently playing around with NeHe's ninth tutorial. Basically, I've been doing each lesson and generally extending beyond the original scope of each tutorial to get a better feel of what's going on. In the case of the ninth tutorial, instead of a vortex/swirl of twinkling "sprites", my aim is to have a rising motes effect (ie. x number of sprites travel slowly up the screen at differing speeds depending on their z axis, and also weave side to side - If you have ever played Darwinia, they use a similar effect on one of their bootloader intros). Whilst I have modified the code enough to know and understand how to make the "motes" travel up the screen and so on, I'm trying to use the same trick from the original intent of the tutorial where if the mote leaves the viewport, its position is reset to below the bottom edge of the viewport. This obviously will save me having to keep generating a brand new mote each time one leaves the top of the screen. The problem being, whilst the original tutorial simply checks to see if the sprite has reached a position of 0,0 and recycles the sprite back to a distance of 5.0 units away, I cannot use this same check. After reading through the OpenGL manual a bit and basically not having much luck except for mentions of using a frustrum (which I'm hesitant to dive into using something like that since I'm trying to learn step by step) and also mentions that there are by default clipping planes attached to a viewport for each direction, I cannot figure out how to programatically detect whether or not a mote has simply moved out of the top of the viewport. I admit that I have a fairly crappy algorithm that seems to vaguely work half the time (Image Height divided by absolute value of the quad's z position equals where I should begin and end clipping - so 128 / abs(-16) = 8, leading to me placing the "start" position at -8 and "end" position at +8), but it feels far too primitive and clumsy, and I figure there *must* be a fairly simple way to detect the position of the quad. If you've read this far, I thank you already. If you can point me in the right direction, my thanks will increase to offers of cake/loose women/personal sexual favours, etc :)

Share this post


Link to post
Share on other sites
Frustum
First of all I'd like to give a short definition of the view frustum. Basically it's a volume defined by 6 clipping planes which itself defines which region of 3D space is visible to the camera (note that the viewport simply defines where the camera's output is placed on the screen).

So if you'd like to know if an object has left the visible area you'd have to check whether the object is inside that volume or not.

In case of your quads you could check each of the four vertices and if one of them is inside the volume the quad is visible. Otherwise the quad is likely to be invisible except for the cases where the quad crosses the volume but none of the corners is visible.

Projection
There's jet another approach that you could use in case of a quad.

Try to project the vertices of the quad to the screen using the projection matrix you've provided to OpenGL (either you still have it or read it back from OpenGL).

Then transform each vertex by that matrix and read out the resulting position. X and Y are the position of the vertex in the viewport relative to its center with -1 and +1 being the edges, Z is the distance to the screen plane. So you check whether x or y are < -1 or > +1 or if z is < 0 (behind camera) or > 1 (to far from screen).

Keep in mind that here you could also run into cases where the quad spans the whole screen and each corner beeing invisible. But if you're using screen-aligned quads (always facing the screen) you could solve this with testing the z-coordinate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this