polygon coords -> pixel coords

Hi guys

How do i convert a polygon coordinate (0,10,-10) , to pixel coordinates on the screen ? I have heard something about the projection matrix…, but, can’t figure it out

thanks

Bruno

GLint gluProject(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble modelMatrix[16],const GLdouble projMatrix[16], const GLint viewport[4], GLdouble winx, GLdouble *winy, GLdouble *winz);

As i understand i can use this gluProject, and feed this function with my poly coordinates.
In wich variable are the modelMatrix,projMatrix and viewport stored?
Are they OGL reserved variables??
thanks

modelMatrix, projMatrix and viewport is three variables you create yourself. Then you get the two matrices and viewport from OpenGL.

GLfloat modelMatrix[16], projMatrix[16];
GLint viewport[4];
GLfloat objx, objy, objz;
GLfloat winx, winy, winz;
glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);

This should do it. Window coordinates is placed winx and winy. But remember that OpenGL’s Y-axis is going from bottom of the window and upwards, while Windows Y-axis is going from top to bottom.

Hey, thanks a lot Bob., but i still have one problem…

First, i had to define the matrix’s glDouble, the compiler keep complaining, so i just changed the declaration of the variables.

Well, this is my problem…, i did this:

GLdouble modelMatrix[16], projMatrix[16];
GLint viewport[4];
GLfloat objx, objy, objz;
GLdouble winx, winy, winz;

then,

objx=0;
objy=0;
objz=-10;

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

and i got,
winx=400;
winy=300;
winz=0.9;

The next vertex of my polygon is,

objx=6;
objy=0;
objz=-10;

so, once again running glProject

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

and now i got:

winx=834;
winy=300;
winz=0.9;

This is completely off screen (800x600), and my polygon is right in the middle of the screen…, what am i doing wrong???

One other thing, a poly coordinate of y=-8, i get an winy of -239…,is this whay you mean by win/ogl inverted y-axis ??

thanks

Bruno

Well, I don’t know how you setup your matrices, but I think it seems like it works OK. If you think it’s wrong or don’t understand the whole thing about the polygon inside the screen, but projected coordinates outside, post some code and maybe we can find some reasons.

What I mean by different window coordinates is that in Windows, the coordinate (0,0) is the top left corner, but in OpenGL, it’s the lower left corner. You convert the point with: oglYValue = windowHeight - windowsYValue

Hi…, thanks, i read your e-mail

Ok, here it goes my code then:

typedef struct poly
{
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
float x4,y4,z4;
int tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4;
}datas;

datas polys[2];

main()
{
GLdouble modelMatrix[16], projMatrix[16];
GLint viewport[4];
GLfloat objx, objy, objz;
GLdouble winx, winy, winz;
int i=0;
float zz=-10;

polys[i].x1=0;
polys[i].y1 =0;
polys[i].z1 = zz;

polys[i].x2 = 6;
polys[i].y2 = 0;
polys[i].z2 = zz;

polys[i].x3=6;
polys[i].y3=-8;
polys[i].z3=zz;

polys[i].x4=0;
polys[i].y4=-8;
polys[i].z4=zz;

glGetDoublev(GL_MODELVIEW_MATRIX, ModelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

objx=polys[i].x1;
objy=polys[i].y1;
objz=polys[i].z1;
gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);

// Here i get x=400,y=300;

polys[i].tx1=winx;
polys[i].ty1=abs(winy-600);

objx=polys[i].x2;
objy=polys[i].y2;
objz=polys[i].z2;

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

// here i get x=838,y=300;

polys[i].tx2=winx;
polys[i].ty2=abs(winy-600);

If you want the complete code, i can sent it to you, just say

thanks

Bruno

Man, my post didn’t show up…, well, going to post it again…

Ok, thanks bob, i read your e-mail.
Here it goes some code:

typedef struct poly
{
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
float x4,y4,z4;
int tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4;
}datas;

datas polys[2];

main()
{
int i=0;
float zz=-10;

polys[i].x1=0;
polys[i].y1 =0;
polys[i].z1 = zz;

polys[i].x2 = 6;
polys[i].y2 = 0;
polys[i].z2 = zz;

polys[i].x3=6;
polys[i].y3=-8;
polys[i].z3=zz;

polys[i].x4=0;
polys[i].y4=-8;
polys[i].z4=zz;

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

objx=polys[i].x1;
objy=polys[i].y1;
objz=polys[i].z1;
gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);

polys[i].tx1=winx;
polys[i].ty1=abs(winy-600);

///// Here, i get x=400, y= 300;

objx=polys[i].x2;
objy=polys[i].y2;
objz=polys[i].z2;

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

polys[i].tx2=winx;
polys[i].ty2=abs(winy-600);

///// Here i get, x=838, y=300…

thanks

Bruno

Man, my post didn’t show up…, well, going to post it again…

Ok, thanks bob, i read your e-mail.
Here it goes some code:

typedef struct poly
{
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
float x4,y4,z4;
int tx1,ty1,tx2,ty2,tx3,ty3,tx4,ty4;
}datas;

datas polys[2];

main()
{
int i=0;
float zz=-10;

polys[i].x1=0;
polys[i].y1 =0;
polys[i].z1 = zz;

polys[i].x2 = 6;
polys[i].y2 = 0;
polys[i].z2 = zz;

polys[i].x3=6;
polys[i].y3=-8;
polys[i].z3=zz;

polys[i].x4=0;
polys[i].y4=-8;
polys[i].z4=zz;

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

objx=polys[i].x1;
objy=polys[i].y1;
objz=polys[i].z1;
gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, &winx, &winy, &winz);

polys[i].tx1=winx;
polys[i].ty1=abs(winy-600);

///// Here, i get x=400, y= 300;

objx=polys[i].x2;
objy=polys[i].y2;
objz=polys[i].z2;

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

polys[i].tx2=winx;
polys[i].ty2=abs(winy-600);

///// Here i get, x=838, y=300…

thanks

Bruno

Why x == 838 ? it beyonds the window width.