3D Engine - Projection Matrix

Hi

Im basically building a 3d engine (very simple one) using software rendering (no opengl or directx) for learning purposes.

The 3d engine has one simple rendering method thats suppose to render a point from world space to the 2d space (monitor)

i believe i got the rendering working properly, but it has no perspective

i believe i need a perspective matrix

can you pros help me build one

publicstatic Pixel2D render(Camera c, Pixel3D p){

//matrix transformations (with euler style rotation)

//fm = pm * cm * wm * point

double fm[][];

double om[][] = createMatrix(4, 1);

double cm[][];

double ctm[][] = createIdentityMatrix(4);//camera translation

double crxm[][] = createIdentityMatrix(4);//camera x-axis rotation

double crym[][] = createIdentityMatrix(4);//camera y-axis rotation

double crzm[][] = createIdentityMatrix(4);//camera z-axis rotation

double pm[][] = createIdentityMatrix(4);//perspective matrix

double rx = Math.toRadians(c.getRoll());

double ry = Math.toRadians(c.getPitch());

double rz = Math.toRadians(c.getYaw());

//camera matrix

//translation

ctm[0][3] = c.getX();

ctm[1][3] = c.getY();

ctm[2][3] = c.getZ();

//roll

crxm[1][1] = Math.cos(rx);

crxm[1][2] = Math.sin(rx);

crxm[2][1] = -Math.sin(rx);

crxm[2][2] = Math.cos(rx);

//pitch

crym[0][0] = Math.cos(ry);

crym[0][2] = -Math.sin(ry);

crym[2][0] = Math.sin(ry);

crym[2][2] = Math.cos(ry);

//yaw

crzm[0][0] = Math.cos(rz);

crzm[0][1] = Math.sin(rz);

crzm[1][0] = -Math.sin(rz);

crzm[1][1] = Math.cos(rz);

cm = multiplyMatrix(crxm, crym);

cm = multiplyMatrix(cm, crzm);

cm = multiplyMatrix(cm, ctm);

//projection matrix

pm[3][2] = 1;

pm[3][3] = 0;

//object matrix (point)

om[0][0] = p.getX();

om[1][0] = p.getY();

om[2][0] = p.getZ();

om[3][0] = 1;

//we have no world matrix

//fm = multiplyMatrix(pm, cm);

//fm = multiplyMatrix(cm, om);

fm = multiplyMatrix(cm, om);

returnnew Pixel2D( (int) fm[0][0], (int) fm[1][0], p.getColour());

}

for complete source code so far: http://williamchan.brixonsolutions.com/myworks/java/graphics3d/source.php

[3512 byte] By [aznsofta] at [2007-10-3 3:35:22]
# 1

I can't remember the details of doing a perspective transformation, but what I do remember is that you add an extra column to your data to put it into a 4d space, called homogenous coordinates. Then you apply a linear transformation that maps the viewpoint into a plane in 4d. This causes all the lines that project out from the viewer to be parallel. You can then lose the 4th dimension and the resulting x and y are in perspective and the z is the distance from the viewer. The phrase to google for is "homogenous transformation".

pkwoostera at 2007-7-14 21:30:04 > top of Java-index,Java Essentials,Java Programming...
# 2
Here's a practical, but mathematical discussion of the theory behind homogenous coordinates and their use in computer graphics. http://www.unchainedgeometry.com/jbloom/pdf/homog-coords.pdf
pkwoostera at 2007-7-14 21:30:04 > top of Java-index,Java Essentials,Java Programming...