Jump to content
  • Advertisement
Sign in to follow this  
Arek the Absolute

Distance from Modelview

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!