Sign in to follow this  
Jotaf

Peeking into the current matrix in the stack

Recommended Posts

Hi! I recently hit a road block in a project I'm developing. It's probably just a matter of knowing the right function name, so sorry in advance if it's a trivial question. I have a "draw an object" function that I want to generalize as much as possible. So, before calling it, you're supposed to push a matrix and apply any translations, rotations, etc. The object will be drawn according to this automatically, that's the whole idea of matrices. The problem is that I have a vector relative to the world (the camera vector) and I need to compare it with a vector *inside the object* -- one that is transformed by all the matrix operations. So, obviously, inside the function that draws the object, I need to apply the "current matrix" in the stack to the camera vector that is relative to the world. But I have no idea of how to do this. I've searched the web and stuff, but no luck. Just peeking into the stack would be enough I guess, I know how to multiply a matrix by a vector. Can you help? Thanks! [Edited by - Jotaf on September 8, 2005 10:38:29 AM]

Share this post


Link to post
Share on other sites
There are two solutions:
1- transform the camera vector (lets call it cam_vec) from world to object space
2- transform the vector *inside the object* (lets call it obj_vec) from object to world space

If you have multiple obj_vecs to compare to, solution 1 will be most efficient.

Anyway, both require you to have the current 'model' matrix (a matrix that transforms from model-space to world-space). Unfortunately, opengl combines model and view matrices int the modelview matrix (which converts directly from model to camera space), which makes it tricky to acquire this model matrix automatically. This means that you should keep track of the model matrix yourself. Alternatively, instead of transforming from world space, you could transform from camera-space. Assuming the latter, the problem becomes:

1- transform the camera vector (lets call it cam_vec2) from camera to object space

The solution to that is simple, just call
 
float matrix[16];
void glGetFloatv(GL_MODELVIEW_MATRIX, matrix)

and matrix will be filled with the required values. Then multiply the cam_vec2 with the transposed of that matrix and it will be in object space.

Tom

Note: cam_vec2 is different from cam_vec1, as it is defined in camera space rather than world space. Its values should actually be simpler though (if for example it's the viewing direction of the camera, its value is always the same: (0,0,-1)).

EDIT: by the way, I assumed that with 'vector' you really meant vector. If you need to do this with a vertex in camera space instead, you'll need to use the inverse matrix rather than the transposed matrix in the final step...

[Edited by - dimebolt on September 8, 2005 11:45:33 AM]

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