Advertisement

Matrix

Started by January 24, 2002 11:48 AM
1 comment, last by Oktan 23 years, 1 month ago
I have a matrix from OpenGL (4x4 table of floats) and a point coordinates (x, y, z). I want to pass the point through the matrix - calculate for it world coordinates after transformations. What and how should I do it?
Say the OpenGL Matrix is M , the vertex before transformation is A and the vertex after transformation is B . The transformation is :

B = M x A

it is the result of a product between a Matrix (4x4) and a vector (4x1) :
  | Bx |   | M11 M12 M13 M14 |   | Ax || By |   | M21 M22 M23 M24 |   | Ay || Bz | = | M31 M32 M33 M34 | x | Az || Bw |   | M41 M42 M43 M44 |   | Aw |  


Be careful of OpenGL matrix. Its rows and columns are transposed to the "standard" matrix representation, eg in OpenGL the 4 firsts elements represents the 1st column, not the 1st row.

The 4th vector coordinate (W) is the homogeneous coordinate.
I'll write later how to use it.

  void transformCoordinates(GLfloat *dest, GLfloat *matrix, GLfloat *src){ dest[0] = matrix[0]*src[0]+matrix[4]*src[1]+matrix[8]*src[2]+matrix[12]*src[3]; dest[1] = matrix[1]*src[0]+matrix[5]*src[1]+matrix[9]*src[2]+matrix[13]*src[3]; dest[2] = matrix[2]*src[0]+matrix[6]*src[1]+matrix[10]*src[2]+matrix[14]*src[3]; dest[3] = matrix[3]*src[0]+matrix[7]*src[1]+matrix[11]*src[2]+matrix[15]*src[3];}...GLfloat matrix[16];GLfloat local_vertex[4] = { ... }; /* set your local coordinates */GLfloat world_vertex[4];glGetFloatv(GL_MODELVIEW_MATRIX, matrix);transformCoordinates(world_vertex, matrix, local_vertex);  

In this source example, A is world_vertex, B is local_vertex and M is matrix.

Usage of W coordinate
Any vertex V=[X Y Z W] is equivalent to a 3D vertex [X/W Y/W Z/W].
If the W coordinate is null, the vertex is infinite.

Because your original local vertex is in 3D, its the W coordinate is 1 :
local_vertex = {x y z 1};

When you compute the world coordinates, just divide each component by W to get its 3D coordinate.

Edited by - vincoof on January 24, 2002 1:24:53 PM
Advertisement
Thank you a lot

This topic is closed to new replies.

Advertisement