Click to See Complete Forum and Search --> : From object coords to screen coords...help!


misterAction
December 12th, 2006, 11:59 AM
Hi,
Im trying to write my own routines to draw objects on screen. I have functions for changing coordinates, applying perspective projection, and to go from device coords to screen coords, but nothings coming up.


// partial listing of project

void ChangeCoord(Mat4x4 matIn, pt4d R, int D, pt4d N, pt4d U) // camera position
{

cout <<"---- ChangeCoord called ----" << endl;

// translate origin to view reference plane
Mat4x4 T;
IdentMatrix4D(T);

T[0][3] = - (R.x - D * N.x);
T[1][3] = - (R.y - D * N.y);
T[2][3] = - (R.z - D * N.z);

// rotate about the x-axis
float V = sqrt((N.y * N.y) + (N.z + N.z));

cout << "V = " << V << endl;

Mat4x4 Qx;
IdentMatrix4D(Qx);

Qx[1][1] = - (N.z / V);
Qx[1][2] = N.y / V;
Qx[2][1] = - (N.y / V);
Qx[2][2] = - (N.z / V);

// rotate about the y-axis
Mat4x4 Qy;
IdentMatrix4D(Qy);

Qy[0][0] = V;
Qy[0][2] = N.x;
Qy[2][0] = -N.x;
Qy[2][2] = V;

// get new coordinates of new y-axis
Mat4x4 QyTemp;
int row, col;

for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
QyTemp[row][col] = Qy[row][col];
}
}

MatMply(QyTemp, Qx);

pt4d UV;

UV.x = QyTemp[0][0] * U.x + QyTemp[0][1] * U.y + QyTemp[0][2] * U.z;
UV.y = QyTemp[1][0] * U.x + QyTemp[1][1] * U.y + QyTemp[1][2] * U.z;
UV.z = QyTemp[2][0] * U.x + QyTemp[2][1] * U.y + QyTemp[2][2] * U.z;

int RUP = sqrt((UV.x * UV.x) + (UV.y * UV.y));

// z rotation
Mat4x4 Qz;
IdentMatrix4D(Qz);
Qz[0][0] = UV.y / RUP;
Qz[0][1] = -(UV.x / RUP);
Qz[1][0] = UV.x / RUP;
Qz[1][1] = UV.y / RUP;

MatMply(Qz, Qy);
MatMply(Qz, Qx);
MatMply(Qz, T);

for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
matIn[row][col] = Qz[row][col];
}
}

printMat4x4(matIn);

}

void ProjectionView(Mat4x4 matIn, pt4d P, pt4d R, int D, pt4d N, pt4d U)
{


Mat4x4 CCM;
ChangeCoord(CCM, R, D, N, U);

pt4d C;
C.x = CCM[0][0] * P.x + CCM[0][1] * P.y + CCM[0][2] * P.z + CCM[0][3] * P.w;
C.y = CCM[1][0] * P.x + CCM[1][1] * P.y + CCM[1][2] * P.z + CCM[1][3] * P.w;
C.z = CCM[2][0] * P.x + CCM[2][1] * P.y + CCM[2][2] * P.z + CCM[2][3] * P.w;
C.w = CCM[3][0] * P.x + CCM[3][1] * P.y + CCM[3][2] * P.z + CCM[3][3] * P.w;

Mat4x4 PM;
IdentMatrix4D(PM);

PM[0][0] = -C.z;
PM[0][2] = C.x;
PM[1][1] = -C.z;
PM[1][2] = C.y;
PM[2][2] = -C.w;
PM[3][2] = C.w;

if (C.w == 0)
PM[3][3] = -1;
else
PM[3][3] = -C.z;

MatMply(PM, CCM);

int row, col;

for(row = 0; row < 3; row++)
{
for(col = 0; col < 3; col++)
{
matIn[row][col] = PM[row][col];
}
}

cout << "------------ end ProjectionView -----------" << endl;


}

void WorldToScreen(float vl, float vr, float vb, float vt, float wl, float wr, float wb, float wt, PT3D &p1, int maxX, int maxY)
{

//convert 2d to ndc
Mat3x3 ndc;
IdentMatrix3D(ndc);

ndc[0][0] = (vr - vl) / (wr - wl);
ndc[0][2] = ( -(wl * (vr - vl)) / (wr - wl) ) + vl;
ndc[1][1] = (vt - vb) / (wt - wb);
ndc[1][2] = ( -(wb * (vt - vb)) / (wt - wb) ) + vb;

// begin multiplication
PT3D out;
out.x = (ndc[0][0] * p1.x) + (ndc[0][1] * p1.y) + (ndc[0][2] * p1.z);
out.y = (ndc[1][0] * p1.x) + (ndc[1][1] * p1.y) + (ndc[1][2] * p1.z);
out.z = 1;




// convert from ndc to screen
Mat3x3 screen;
IdentMatrix3D(screen);

screen[0][0] = maxX;
screen[1][1] = -4/3 * maxY;
screen[1][2] = maxY;

p1.x = screen[0][0] * out.x + screen[0][1] * out.y + screen[0][2] * out.z;
p1.y = screen[1][0] * out.x + screen[1][1] * out.y + screen[1][2] * out.z;
p1.z = 1;




}

void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);



float points[192] = {

1.40000, 0.00000, 2.40000,
1.21126, -0.71141, 2.40000,
0.71141, -1.21126, 2.40000,
0.00000, -1.40000, 2.40000,
1.38426, 0.00000, 2.48750,
1.19764, -0.70341, 2.48750,
0.70341, -1.19764, 2.48750,
0.00000, -1.38426, 2.48750,
1.43241, 0.00000, 2.48750,
1.23930, -0.72788, 2.48750,
0.72788, -1.23930, 2.48750,
0.00000, -1.43241, 2.48750,
1.50000, 0.00000, 2.40000,
1.29778, -0.76222, 2.40000,
0.76222, -1.29778, 2.40000,
0.00000, -1.50000, 2.40000,
0.00000, -1.40000, 2.40000,
-0.71141, -1.21126, 2.40000,
-1.21126, -0.71141, 2.40000,
-1.40000, -0.00000, 2.40000,
0.00000, -1.38426, 2.48750,
-0.70341, -1.19764, 2.48750,
-1.19764, -0.70341, 2.48750,
-1.38426, -0.00000, 2.48750,
0.00000, -1.43241, 2.48750,
-0.72788, -1.23930, 2.48750,
-1.23930, -0.72788, 2.48750,
-1.43241, -0.00000, 2.48750,
0.00000, -1.50000, 2.40000,
-0.76222, -1.29778, 2.40000,
-1.29778, -0.76222, 2.40000,
-1.50000, -0.00000, 2.40000,
-1.40000, 0.00000, 2.40000,
-1.21126, 0.71141, 2.40000,
-0.71141, 1.21126, 2.40000,
-0.00000, 1.40000, 2.40000,
-1.38426, 0.00000, 2.48750,
-1.19764, 0.70341, 2.48750,
-0.70341, 1.19764, 2.48750,
-0.00000, 1.38426, 2.48750,
-1.43241, 0.00000, 2.48750,
-1.23930, 0.72788, 2.48750,
-0.72788, 1.23930, 2.48750,
-0.00000, 1.43241, 2.48750,
-1.50000, 0.00000, 2.40000,
-1.29778, 0.76222, 2.40000,
-0.76222, 1.29778, 2.40000,
-0.00000, 1.50000, 2.40000,
0.00000, 1.40000, 2.40000,
0.71141, 1.21126, 2.40000,
1.21126, 0.71141, 2.40000,
1.40000, 0.00000, 2.40000,
0.00000, 1.38426, 2.48750,
0.70341, 1.19764, 2.48750,
1.19764, 0.70341, 2.48750,
1.38426, 0.00000, 2.48750,
0.00000, 1.43241, 2.48750,
0.72788, 1.23930, 2.48750,
1.23930, 0.72788, 2.48750,
1.43241, 0.00000, 2.48750,
0.00000, 1.50000, 2.40000,
0.76222, 1.29778, 2.40000,
1.29778, 0.76222, 2.40000,
1.50000, 0.00000, 2.40000
};


int count = 0;

/*
a) View reference point : (0.0, 0.0, 0.0)
b) View plane normal: (1.0, 1.0, 0.0)
c) View distance: 5
d) View up vector: (0.0, 0.0, 1.0)
e) Center of Projection: (-28.0, -23.0, 10.0, 1.0)
*/


pt4d refPoint;
refPoint.x = 0.0;
refPoint.y = 0.0;
refPoint.z = 0.0;
refPoint.w = 1.0;

pt4d viewNormal;
viewNormal.x = 1.0;
viewNormal.y = 1.0;
viewNormal.z = 1.0;
viewNormal.w = 1.0;

int distance = 5;

pt4d viewUpVector;
viewUpVector.x = 0.0;
viewUpVector.y = 0.0;
viewUpVector.z = 0.0;
viewUpVector.w = 1.0;

pt4d centerOfProj;
centerOfProj.x = -28.0;
centerOfProj.y = -23.0;
centerOfProj.z = 10.0;
centerOfProj.w = 1.0;



for (int x = 0; x < (64 * 3) - 1; x+=3)
{

PT3D point1, point2;

point1.x = points[x];
point1.y = points[x+1];
point1.z = points[x+2];

point2.x = points[x+3];
point2.y = points[x+4];
point2.z = points[x+5];

// get projection view
Mat4x4 in;
IdentMatrix4D(in);

ProjectionView(in, centerOfProj, refPoint, distance, viewNormal, viewUpVector);

VectMatMply(point1, in);
VectMatMply(point2, in);

// go to ndc
WorldToScreen(0.0, 0.52, 0.35, 0.75, -26.0, 26.0, -20.0, 20.0, point1, 250, 250);
WorldToScreen(0.0, 0.52, 0.35, 0.75, -26.0, 26.0, -20.0, 20.0, point2, 250, 250);

glColor3f (0.5, 1.0, 0.5);
glBegin(GL_LINES);
glVertex3f (point1.x, point1.y, point1.z );
glVertex3f (point2.x, point2.y, point2.z );
glEnd();

}



Does anyone see any glaring errors?

-J