# Matrix transformations

Given a modelview and projection matrix and a viewport(cx, cy), what are the operations OGL performes in order to get a 2D point on the screen
(something like gluProject code)?
I know that the vertices are multiplied with modelview then with projection matrix, scaled …etc but I want a concrete example…
It will be very usefull also a link where I can find detailed information about functions like gluPerspective, gluLookAt, glOrtho,…etc(to what matrix they must be applied and what is the effect - both modelview and projection are modified)

From MESA (glu/project.c):

``````
/*
* Transform a point (column vector) by a 4x4 matrix.  I.e.  out = m * in
* Input:  m - the 4x4 matrix
*         in - the 4x1 vector
* Output:  out - the resulting 4x1 vector.
*/
static void
transform_point(GLdouble out, const GLdouble m, const GLdouble in)
{
#define M(row,col)  m[col*4+row]
out =
M(0, 0) * in + M(0, 1) * in + M(0, 2) * in + M(0, 3) * in;
out =
M(1, 0) * in + M(1, 1) * in + M(1, 2) * in + M(1, 3) * in;
out =
M(2, 0) * in + M(2, 1) * in + M(2, 2) * in + M(2, 3) * in;
out =
M(3, 0) * in + M(3, 1) * in + M(3, 2) * in + M(3, 3) * in;
#undef M
}

/* projection du point (objx,objy,obz) sur l'ecran (winx,winy,winz) */
GLint GLAPIENTRY
gluProject(GLdouble objx, GLdouble objy, GLdouble objz,
const GLdouble model, const GLdouble proj,
const GLint viewport,
GLdouble * winx, GLdouble * winy, GLdouble * winz)
{
/* matrice de transformation */
GLdouble in, out;

/* initilise la matrice et le vecteur a transformer */
in = objx;
in = objy;
in = objz;
in = 1.0;
transform_point(out, model, in);
transform_point(in, proj, out);

/* d'ou le resultat normalise entre -1 et 1 */
if (in == 0.0)
return GL_FALSE;

in /= in;
in /= in;
in /= in;

/* en coordonnees ecran */
*winx = viewport + (1 + in) * viewport / 2;
*winy = viewport + (1 + in) * viewport / 2;
/* entre 0 et 1 suivant z */
*winz = (1 + in) / 2;
return GL_TRUE;
}
``````

Take a look

Cool. Thank you.