How to define projection matrix in stage3d without using perspectiveFieldOfViewLH method

904 views Asked by At

i did this tutorial:

http://ltslashgt.com/2011/02/28/molehill-spinning-cube/

The interesting part is that he didn't use perspectiveFieldOfViewLH and instead he defined his own projection matrix. I'm new to the 3d programming, so after reading this explenation of projection matrix:

http://www.songho.ca/opengl/gl_projectionmatrix.html

i tried to implement my own matrix to see if i really understand it:

var y2:Number = near * Math.tan(fov * Math.PI / 360);
var y1:Number = -y2;
var x1:Number = y1 * aspect;
var x2:Number = y2 * aspect;

return new Matrix3D(Vector.<Number>([
    2 * near / (x2 - x1), 0, 0, 0, //x
    0, 2 * near / (y2 - y1), 0, 0, //y
    0, 0, (far + near) / (near - far), -2 * far * near/ (far - near), //z
    0, 0, -1, 0 //w
    ]));

And i got the black screen. All i did was exchanging the matrix from the tutorial with this matrix. I tried to play with it for some time, but with no results. This is the matrix from the tutorial:

protected function perspectiveProjection(fov:Number=90,
  aspect:Number=1, near:Number=1, far:Number=2048):Matrix3D {
  var y2:Number = near * Math.tan(fov * Math.PI / 360);
  var y1:Number = -y2;
  var x1:Number = y1 * aspect;
  var x2:Number = y2 * aspect;

  var a:Number = 2 * near / (x2 - x1);
  var b:Number = 2 * near / (y2 - y1);
  var c:Number = (x2 + x1) / (x2 - x1);
  var d:Number = (y2 + y1) / (y2 - y1);
  var q:Number = -(far + near) / (far - near);
  var qn:Number = -2 * (far * near) / (far - near);

  return new Matrix3D(Vector.<Number>([
    a, 0, 0, 0,
    0, b, 0, 0,
    c, d, q, -1,
    0, 0, qn, 0
  ]));
}

Notice that the first and the second rows are the same.

1

There are 1 answers

0
starmole On

infact has a very good answer, but it might be a bit too high level. the job of a projection matrix is to go from eye space to clip space. eye space is where the eye, or camera, is at the origin (0,0,0) and looking down z. in eye space vertices are usually (x,y,z,1). the vertex program output is in clipspace. the easiest way to understand it is what happens with those (x,y,z,w) clipspace coordinates for drawing: they are projected by dividing all coordinates by w (not z!). so the x screen coordinate is x/w etc. but to get perspective you want to divide by z, or distance, not by w which is still 1 in eyespace. so the main job of the projection matrix is to move the eyespace z into the clipspace w. all the other stuff is just additional scaling folded in. it looks like one of those scalings is wrong in you version, most likely z. in molehill the z output is what is used for depth testing and is in the -1..0 range. just make sure not to forget that z is going to be divided by w also. this is why the tutorial matrix has a -1 on w for the z row, to get something like 1/z into the depth test. now i wrote a wall of text :) maybe the best advice for learning is to get really familiar with 4x4 matrix math and try to follow transformation pipelines on paper.