#### Archived

This topic is now archived and is closed to further replies.

# transform coordinates between 2 matrices

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

## Recommended Posts

i''m trying to ''convert'' coordinates from one matrix to another. i''m setting the camera-matrix & drawing a point. then i try to get the ''real'' coordinates of that point (relative to the identity matrix). i don''t really know, how OpenGL''s modelview-matrix works (thought, i did so, but my problem seems to prove something different ) here''s my code : Camera->Set (); // sets the camera (gluLookAt() nxVertex3f _v1, _v3; _v1.x = 4.0f; // set point''s coordinates _v1.y = 5.0f; _v1.z = -5.0f; nxMatrix Matrix; // get the modelview-matrix glGetFloatv (GL_MODELVIEW_MATRIX, (float*)&Matrix); //calc _v1 (untransformed) -> _v3 (transformed) _v3.x = Matrix.X[0]*_v1.x + Matrix.X[1]*_v1.x + Matrix.X[2]*_v1.x + Matrix.P[0]; _v3.y = Matrix.Y[0]*_v1.y + Matrix.Y[1]*_v1.y + Matrix.Y[2]*_v1.y + Matrix.P[1]; _v3.z = Matrix.Z[0]*_v1.z + Matrix.Z[1]*_v1.z + Matrix.Z[2]*_v1.z + Matrix.P[2]; glPushMatrix (); //draw first dot glColor3f (1.0f, 1.0f, 1.0f); glBegin (GL_POINTS); glVertex3f (_v1.x, _v1.y, _v1.z); glEnd(); // draw second dot glLoadIdentity(); glColor3f (1.0f, 0.0f, 0.0f); glBegin (GL_POINTS); glVertex3f (_v3.x, _v3.y, _v3.z); glEnd(); glPopMatrix(); ... i want to have both points at the same position, one of them drawn ''relative'' and the other one (second) ''absolute''. what''s wrong with my code (the points don''t stay @ the same position, when the camera moves...) is it my transformation code ? thx

##### Share on other sites
have you tried rearanging the matrix positions?

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

to
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16)

otherwise it should work as far as i know (dont take me serious as my matrix knowledge is rather (extremely) bad )

##### Share on other sites
yes, i''ve tried that... doesn''t work.

##### Share on other sites
Try using x''=f(x,y,z) instead of x''=f(x,x,x). The matrix returned is the transpose of the standard matrix, but your multiplication is correct for that matrix if you use the right components of your vector.

##### Share on other sites
LilBudyWizer : yes, you''re right... i''ve just tried this, before reading here... and it worked.

the correct code is :

_v3.x = Matrix.X[0]*_v1.x + Matrix.Y[0]*_v1.y + Matrix.Z[0]*_v1.z + Matrix.P[0];
_v3.y = Matrix.X[1]*_v1.x + Matrix.Y[1]*_v1.y + Matrix.Z[1]*_v1.z + Matrix.P[1];
_v3.z = Matrix.X[2]*_v1.x + Matrix.Y[2]*_v1.y + Matrix.Z[2]*_v1.z + Matrix.P[2];

strange... i''ve done it the other way before... didn''t see any errors... maybe i should have a look at these routines once more

thx 4 help !

##### Share on other sites
well... now it gets a little more tricky... what about the other way round ?
i have absolute coordinates and want to transform them into a matrix. the resulting point shall be the same (in absolute space). is that possible ? (i''ve tried to do the calculation by hand & decided i''m too lazy to go on... just takes me too long )

does anyone know how this works ?

##### Share on other sites
I''m not sure exactly what you mean. If you mean can you find the inverse of a matrix then yes as long as the determinant is not zero. Rotation, scaling and translation matrices are all invertible and the product of any set of matrices with non-zero determinants has a non-zero determinant. The easiest way to invert a transform is to factor it into a rotation, scaling and translation matrices. The inverse of a rotation is the transpose. The inverse of a translation by (tx,ty,tz) is a translation by (-tx,-ty,-tz). The inverse of a scaling by (sx,sy,sz) is a scaling by (1/sx,1/sy,1/sz).

The easiest way to factor the matrix is M=R*S*T. The last row of M is (tx,ty,tz,1) so you replace that with (0,0,0,1) and build a translation matrix for (-tx,-ty,-tz). That gives you M*T^-1=R*S*T*T^-1=R*S. Now sx, sy and sz is the magnitude of the vectors forming the first three columns resepectively. So build a scaling matrix for (1/sx,1/sy,1/sz) and you get M*T^-1*S^-1=R*S*S^-1=R. Take the transpose of what is left and you get M*T^-1*S^-1*R^T=M*T^-1*S^-1*R^-1=I where I is the identity matrix. Since M*M^-1=I then M*(T^-1*S^-1*R^-1)=M*M^-1=I or M^-1=T^-1*S^-1*R^-1. The inverse of the model view matrix will transform coordinates from camera relative coordinates to model relative coordinates.

If you mean building a matrix from a point before and after transformation you can''t do that. You have to have four points and not just any four points. One point is the location of the origin of one coordinate system relative to the other. The vectors from that point to the other three have to be orthogonal to one another. It seems safe to assume that is not what you meant though.

##### Share on other sites
what i meant was :
i have a vertex in absolute coordinates (-> relative to the identity-matrix) and i have the matrix of an object, which may be translated and rotated. i want to have the coordinates relative to the object''s matrix, so that the absolute vertex is still the same.

example: (pseudo-code)

vertex = (10,10,10);
drawvertex(vertex);

m = [any matrix (rel. to identity)]
vertex = (10,10,10);
multmatrix(m);
v2 = vertexToMatrix(vertex);
drawvertex (v2);

and both shall draw the same vertices. the ''vertexToMatrix()'' function is what i need. i''ve tested this by multiplication of the vertex by the inverse matrix of ''m'', but it didn''t work.

hope, that''s explained good enough (me no speak english too good)...

##### Share on other sites
It should work. (v*m^-1)*m=v*(m^-1*m)=v*I=v. So first would be to check that you have the inverse by checking that m*m^-1=I where I is the identity matrix. I would do the multiplication both using OpenGL and without. Someone said that OpenGL doesn''t actually use the transposes but instead just stores it as a transpose which implies it does matrix multiplication as a row times a row instead of a row times a column. If without OpenGL you get the identity matrix, but with you do not then try the inverse of the transpose of the matrix. I believe the transpose of the inverse is the inverse of the transpose, but I can''t swear to it right now.

1. 1
2. 2
3. 3
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×