PDA

View Full Version : Converting object to screen coordinates

rookiebeotch
08-06-2011, 11:03 AM
I am attempting to convert from my object coordiantes to screen coordinates.

Below is my current process. I am not getting the answers i am expecting. I seem to get clip coordinates that area much larger than 1. like -60.0 sometimes. mostly always bigger than 1/-1.

I beleive the process goes like:

ObjVector * ModelView Matrix * Projection Matrix * Perspective Divide = screen coordinates in +/-1 form. then i adjust per resolution.

the following code does that minus the perspective divide, i think :)

float modelview[16], proj[16];
float inV[16];//THIS HOLDS MY OBJECT COORDINATES

glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&amp;modelview);

glMatrixMode(GL_PROJECTION);
glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&amp;proj);

glMatrixMode(GL_MODELVIEW);

//copy matrix by pushing
glPushMatrix();

//MULTIPLY MODELVIEW MATRX WITH PROJECTION MATRIX
glMultMatrixf((GLfloat*)&amp;proj);

//SET THE VECTOR CONTAINING MY OBJECT COORDINATES, REST
//IS FILLED WITH ZEROS. i KNOW I CAN LEAVE IT AS A 4X1 VEC.
inV[0] = x;
inV[1] = y;
inV[2] = z;
inV[3] = 1.0;
for(int i=4;i<16;i++)inV[i]=0;//FILL IN ZEROS

//MULTIPLY OBJECT VECOTR/MATRIX. SHOULD GIVE ME CLIP COORDINATES?

glMultMatrixf((GLfloat*)&amp;inV);
//SAVE THE RESULT TO A MATRIX
glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)matrix);

//restore model view matrix by popping
glPopMatrix();

I think my "matrix" float[16] should hold my clipping coordinates?? matrix[0] and matrix[1] are supposed to be between +/-1.0 and should hold the x,y after i do a perspective divide?

Isnt the perspective divide (w) a 1 since i set my 4th parameter(inV[3]) of my object vector to a 1?

I apprciate any help
thanks

BionicBytes
08-06-2011, 01:22 PM
Your matrix multiplies are the wrong way round.
You should start with the projection matrix and then multiply by the modelview matrix.

Here's my implementation:

function ScreenSpaceProjectPoint(var point: Tvertex3f; var viewProjMatrix: Tmatrix; screenWidth,screenheight: integer): Tvertex2i;
var
Point4D: TVertex4f;
sw_div2,sh_div2: glfloat;
begin
point4D := viewProjMatrix.Vector4Transform (point);
point4D.DividebyW;
sw_div2 := screenWidth / 2;
sh_div2 := screenheight / 2;
result.x := round ((point4D.x * sw_div2) + sw_div2);
result.y := round ((-point4D.y * sh_div2) + sh_div2);

//flip y-coord for OpenGL
result.y := (screenheight - 1) - result.y;
end;