# Distance from Modelview

This topic is 4150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633734
• Total Posts
3013587
×