Sunday, April 8, 2018

matrix - Translating local to global coordinates in ARToolkit / OpenGL


Ok, so I've tried reading over the other questions and something's just not clicking for me. I am working on an augmented reality application using ARToolkit, however it should just be straight OpenGL matrix math.


So, one of my fiducial markers is considered to be my global coordinate base. So I have an x,y,z that I pull out of the ARToolkit transform matrix (this would be something like trans[0][3], trans[1][3], trans[2][3]). I am feeding that into a glTranslated() call in order to move my objects around, relative to the origin.



Now, when I use another marker, those are recognized with their own local coordinates. I need to transform them into the coordinates of the original marker, and that is where I'm having issues. I can pull out the x,y,z coordinates easy enough, but taking that relative to the origin is my issue.


So my understanding was that I could somehow create my global coordinates, take the inverse, multiply those by the new coordinates, and that would give me the coordinates of the new object relative to the origin. I think my issue is in figuring out how to add those in properly to a matrix, since I'm dealing with x,y,z coords separately.


Any help would be appreciated...I'm doing this all in C as well, so no nifty STL functionality for me.



Answer



A small correction first.


x, y and z translation in a 4x4 OpenGL matrix is not


trans[0][3], trans[1][3], trans[2][3]

but


trans[3][0], trans[3][1], trans[3][2]


since OpenGL matrices are in column-major form, the translation being in the last column.


This code-snippet demonstrates it:


    glLoadIdentity();
glTranslatef(1.0, 2.0, 3.0);
float m[4][4];
glGetFloatv(GL_MODELVIEW_MATRIX, (float*)m);
printf("%3.1f, %3.1f, %3.1f\n", m[3][0], m[3][1], m[3][2]);

The output is: 1.0, 2.0, 3.0





Now here is the answer to your question. Let


  mA be the transformation matrix of marker A
mB be the transformation matrix of marker B
mA^-1 be the inverse matrix of mA

Now to get the matrix that transforms mB-space into mA-space -> m = mA^-1 * mB


m[3][0] and m[3][1] and m[3][2] will be the position of marker B in the local space of marker A


Btw. you dont need to pull out x, y and z to pass it to glTranslated, just upload the entire matrix using glMultMatrixd, in order to not only apply the position but also the correct orientation.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...