Luís Jesus
Luís Jesus

Reputation: 1678

Object outside camera OpenGL

I'm using a Perspective View with gluPerspective and gluLookAt and I have the x,y world coordinates of an object. How can I determine if the object is outside the viewable area? Note that the camera is always moving around.

Upvotes: 3

Views: 881

Answers (2)

Jon Cage
Jon Cage

Reputation: 37500

When you say they leave the camera do you mean they're off the sides because you're looking at a fixed distance? ..or do you mean they just vanish.

If it's the first case (off the sides of the shot) you should be able to calculate how far up from the x,y plane you need to move the camera based on your current perspective settings.

If it's the latter case, you probably just need to increase the maximum Z depth to stop them being culled.

[Edit 1] In the gluPerspective call you set up the viewing angle and the aspect ratio. You should know the distance to your plane and the distance from your center point to the cars so you've got all the information you need. All that's left is a little trigonometry to work out what's viewable and what isn't.

[Edit 2] I found another very useful tutorial which describes various ways to do the frustum culling you want to do.

Upvotes: 0

datenwolf
datenwolf

Reputation: 162289

First thing first: OpenGL doesn't have a camera. It just transforms vertices around. And gluLookAt just applies a transformation on the objects that's inverse to a movement of a thought camera.

The solution had already been given in the first comment by Robert Massaioli: In OpenGL a perspective transformation is described in terms of a frustum (gluPerpective just calculates frustum parameters and passes these to glFrustum). So by determining frustum culling of your objects, i.e. if they are culled, you determine if they're still visible.

Upvotes: 5

Related Questions