# 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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627654
• Total Posts
2978447
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33