Sign in to follow this  
waZim

Object Orientation from Modelview Matrix

Recommended Posts

hi All, I have objects in my scene and i want to calcuate its world coordinates and Forward,Up and Right Vectors, what i am doing right now is when i am going to Just draw the Object i call the following code and then i draw the object..
static float		ProjectionMatrix[16];
static float		ModelViewMatrix[16];
static float		CombinedMatrix;
glGetFloatv(GL_PROJECTION_MATRIX,ProjectionMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX,ModelViewMatrix);

glPushMatrix();
glLoadMatrixf(ModelViewMatrix);
glMultMatrixf(ProjectionMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX,CombinedMatrix);
glPopMatrix();

//CombinedMatrix.Inverse();
// Set is used to setup the values of the Vector Vector::Set(x,y,z)
m_ForwardVector->Set(CombinedMatrix[8],	CombinedMatrix[9],	CombinedMatrix[10]);
m_UpVector->Set		(CombinedMatrix[4],	CombinedMatrix[5],	CombinedMatrix[6]);
m_RightVector->Set	(CombinedMatrix[0],	CombinedMatrix[1],	CombinedMatrix[2]);
m_Position->Set		(CombinedMatrix[12],	CombinedMatrix[13],CombinedMatrix[14]);


From this what i understand is that, if i am drawing this object with the current transformation already applied, this should also give me the information regarding my Objects Orientation (Forward,Right,Up), One thing i don't understand is if it would be the Inverse of CombinedMatrix? Or i have to pick one Vector and calculate the Others by doing some CrossProducts and such? And i do see something i do get the Forward,Right and Up Vectors with this. but when i move the camera those Forward,Up and Right vectors for each object also moves. the seem to be camera aligned, or in other words it gives me the camera Orientation but in Objects Position. Since the Position is calculated Properly for each Object. Any explaination of these concepts will be greatly appriciated.

Share this post


Link to post
Share on other sites
What do you mean by world coordinates, and forward etc vectors?

If for example, you have an airplane - whose up vector is _defined_ to be u = [0,0,1,0], then in eye coordinates (as seen by the camera), its coordinates are M*u. If you have the position of the airplane ob = [x,y,z,1] <= the one in w coordinate is crucial, then the objects position in eye coordinates (as seen by the camera) are M*ob, where M is the modelview matrix.

The projection matrix is not needed, because that only converts eye coordinates (as seen by the camera) into clip coordinates (where the screen is centered on x = 0, y = 0, with the camera defined to be at z = 0).

I don't understand the point in doing this though. Usually you choose the objects position and orientation (through angles), not find it.

Share this post


Link to post
Share on other sites
Quote:
Original post by gpu_fem
What do you mean by world coordinates, and forward etc vectors?


World coordinates means Word coordinates, i don't know how else can i explain it.
And forward vector means the vector in the direction of the the model front. or the direction the model is front-facing. And yes i found out last week that Projection matrix is not needed.

Quote:

I don't understand the point in doing this though. Usually you choose the objects position and orientation (through angles), not find it.


Well. let me explain, and may be i am completely wrong of what i am currently doing. so in that case please correct me.

What i have is i am reading a Scenegraph from collada. so the Authoring softwares gives me a Scenegraph in which objects are connected in some ways to each other and their Local transformation is given through matrices.

Now when i have to render that scenegraph i can just load the Parent matrix and the when i am traversing its children i keep on multiplying their Local Transformation matrices with it. My scene is rendered properly, every thing is properly connected to each other and the children follow the parents and so on.. but i don't know how to get their World coordinates in a Usable form. (vectors) since i can't directly work with matrices.

I have to atleast get them when i am traversing the Scenegraph the first time.

And now although not related to this poast, i have further concerns,

Lets say i save the Coordinates and Orientation of an object in my scene so when i am drawing that object in a scenegraph how am i going to calculate matrices from that.. would it not disturb the scenegraph? and then those coordinates will be in world coordinates while i need it in Local relative to parent. so how do i do that.

Share this post


Link to post
Share on other sites
I think I understand.

If you have a local vector v = [1,0,0] in object coordinates (forward, e.g.), and M the model view matrix, then the location in eye coordinates is Mv, like you have.

If you have a two-level transformation M1*M2*v, where M1 is the parent transformation and M2 the object transformation. Just ignore M1 and compute your coordinates via M2*v. I would do this by getting the parent matrix when you load the scene and invert it. Then when you want to compute the coordinates of some object, grab the modelview matrix from GL and pre-multiply it by the inverse of the parent matrix, and then multiply by v.

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