Skip to main content
gamedev.net
🔒 Locked

Projection Matrix

Started by aznium Aug 25, 2006 at 5:00 PM 2 replies 650+ views
Original Post
aznium
aznium
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

  public static 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);

    return new Pixel2D( (int) fm[0][0], (int) fm[1][0], p.getColour());
  }
for complete source code so far: 3D Engine Applet Source Code
MePHyst0
MePHyst0
What kind of coordinate system are you using? Left- or Right- handed? If left- then check the DirectX documentation if right- then check the OpenGL one.
aznium
aznium
im not using opengl or directx

im trying to build perspective from scratch
Zipster
Zipster
The OpenGL and DirectX documentation have some references on building the perspective matrix, even if you aren't using the APIs. This is the code I use to build a perspective matrix, based off OpenGL (right-handed, column vectors):

template<typename T>inline basemat<4, 4, T> perspective3D(T fov, T aspect, T n, T f){	basemat<4, 4, T> m(static_cast<T>(0));	T cot = 1 / std::tan(fov / 2);	m[0][0] = cot / aspect;	m[1][1] = cot;	m[2][2] = (f + n) / (n - f);	m[2][3] = (2 * f * n) / (n - f);	m[3][2] = static_cast<T>(-1);	return m;}


There's a discussion in Math and Physics on the derivation of this matrix, with a strikingly similar subject line.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.