opengl transformed 3d vertice to 2d screen space

I’m using opengl to do the math, i’m still learning the basics, until then i really need
to get some work done
what i need is to convert a mesh after
it being transformed by opengl, after i grad the projection matrix and the modelview matrix how can i use them to convert a vertice to 2d screen space?


OK, let’s try this:
let v(x,y,z) be the vertex in world space, modelview be your world/model view matrix and projection be your projection matrix. What you need to do is:

matrix t = projection * modelview;

vertex s = t.transform(v);

where transfrom() method is defined as:
(actually pasted from my code)
inline vec3_t
matrix_t::transform(const vec3_t& v) const
float w = v.xwx + v.ywy + v.zwz + ww;
return vec3_t(

now “s” is your vertex in normalized coords;
apply the following:

width - vieport (window/screen) width
height - vieport height

screen_x = (s.x+1)(width/2)
screen_y = (s.y+1)

there you go.
Hope this helps. Took it from glViewport() MSDN help page, check it out for the complete

hope i didn’t make any mistakes…

i did everything as you told me,
that worked, except when i used rotations, it was weird, i was assuming the matrix as:

x y z w
x xx xy xz xw
y yx yy yz yw
z zx zy zz zw
w wx wy wz ww

is this correct?
some rotations were wrong, i dunno why :
thanks for the help BTW

I think the matrix is the other way arround, because you multiply the columns with the vertex:

[x y z w]*

|xx yx zx wx|
|xy yy zy wy|
|xz yz zz wz|
|xw yw zw ww|

Then you divide the resulting x,y,z with the resulting w.

[This message has been edited by softland_gh (edited 12-15-2000).]

You can use glProject this way to get the 2D screen coordinate of a point in world space.

GLdouble winx, winy, winz;
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);

gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);

// where objx, objy, and objz are the world coordinates
winx and winy are the 2d screen coord.
and Bonus, if winz>1 then you are facing away from that point,
and if winz<=1 then you are facing towards.

Good luck, lemme know if it works for you

hey, using gluProject works perfectly,
but i will also try doing it like beavis
sugested, it looks less cpu intensive,
thanx alot for the help guys!

I’m sorry fozi, I wasn’t clear as to the matrix fields. They are defined as follows:

    struct {
        float xx,xy,xz,xw;
        float yx,yy,yz,yw;
        float zx,zy,zz,zw;
        float wx,wy,wz,ww;

hope this clarifies things.

yeah it did, thanx alot beavis!