Sign in to follow this  
Arek the Absolute

Distance from Modelview

Recommended Posts

I'm currently trying to write some code that will need to do special processing when an object is going to be rendered beyond a certain distance from the camera. My current idea is to read in the modelview matrix using glGetFloatv, and then calculate the distance from the camera from the values within the matrix. Unfortunately, I can't find much of an explanation for how the values within the modelview matrix are laid out, and therefore I'm not really sure where to start in extracting the total distance from the camera. My question, therefore, is given the modelview matrix, how can one calculate the distance from the camera? Additionally, I'm interested to hear if this is a bad way of doing things. It seems to me that it would be pointlessly redundant if I were to keep an additional stack to keep track of the various translations and rotations, so this seemed the best alternative. If my solution is faulty somehow, though, I'd love to hear why, and what's a better way.

Share this post


Link to post
Share on other sites
The answer to your first question is as follows: multiply a homogeneous (4-d) vector representing the position of the object in question by the modelview matrix; the result gives you the position of the object in the local space of the camera, from which you can derive the distance (either the Euclidean distance to the camera position, or the distance to the view plane, whichever you prefer).

In answer to your second question (that is, is there a better way to do it), you can avoid the OpenGL state query altogether by keeping track of the camera transform yourself. (I can't really be more specific than that, since I'm not sure how you're setting up your camera transform in the first place.)

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