Sign in to follow this  
richardmonette

Creating/Using Projection Matrix

Recommended Posts

richardmonette    129
I am working on a software renderer (for fun and learning). Currently I am handling the projection of my vertices by doing; float inv = (120.0f / v->coordsWorld.z); v->scr[0] = v->coordsWorld.x * inv + (resX >> 1); v->scr[1] = -v->coordsWorld.y * inv + (resY >> 1); but I would like to handle this the same way one would in DirectX (creating a WorldViewProjection matrix). I've implemented a nice little Matrix4x4 class which I am using to apply world transformation to my object(s). I believe that my ViewMatrix will simply be a matrix which has the translation and rotations of the camera (no special work required here). However I am a little bit stuck on creating my projection matrix (instead of using the above code). Here is what I've got so far; void CreateProjectionMatrix(float fov, float aspect, float zn, float zf, Mat4x4 &mat) { float h = cot(fov / 2.0f); float w = h / aspect; mat.m[0][0] = w; mat.m[1][1] = h; mat.m[2][2] = zf / ( zn - zf); mat.m[2][3] = -zn * zf / ( zn - zf ); mat.m[3][2] = 1; } I've based this of the example of the Left Handed CreateProjection function explained in the DirectX sdk. Once I've got all these matrices I am attempting to use them doing something like; vert *= worldMatrix; vert *= viewMatrix; vert *= projectionMatrix; vert.screenXPos = vert.worldPosition.x; vert.screenYPos = vert.worldPosition.y; I believe I have the general concept here correct - but when I use my new matrix based method instead of my simple projection method I just get a blank screen (as opposed to the correct result I was getting before). Anyone see the problem in my approach?

Share this post


Link to post
Share on other sites
Halifax2    295
Are you sure that your view matrix is right. Remember that you are transforming the vertices, not the camera. So if you want the camera to move forward 5 spaces, you must move all vertices backwards 5 spaces.

Share this post


Link to post
Share on other sites
richardmonette    129
I've read through the suggested document now and it seems like the steps in the DirectX transformation pipeline are:

World -> View -> Projection -> Clipping (Optional, Im skipping this for now) -> Viewport Scale Matrix -> Divide by W

Using my old method I've verified my World matrix is OK. I've set my view matrix to identity for now. To create my Projection Matrix I am using the function:


Mat4x4 CreateProjectionMatrix(float fov, float aspect, float zn, float zf)
{
Mat4x4 rMat;

rMat.set(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);

float h = 1.0f / tan(fov / 2.0f);
float w = h / aspect;

rMat.m[0][0] = w;
rMat.m[1][1] = h;
rMat.m[2][2] = zf / ( zf - zn );
rMat.m[2][3] = -zf * zn / ( zf - zn );
rMat.m[3][2] = 1;

return rMat;
}


Then to create my Viewport Scale Matrix I am doing:


Mat4x4 CreateViewportScaleMatrix(int w, int h, float x, float y, float cw, float ch)
{
Mat4x4 rMat;

rMat.set(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);

rMat.m[0][0] = w;
rMat.m[1][1] = -h;
rMat.m[3][0] = x;
rMat.m[3][1] = h + y;

return rMat;
}


To get my W value (this is the depth correct?):


float w = len(v.coordsWorld); // This assumes my camera is at the origin


I'm still not getting the correct values however. I tried checking the matrix DirectX makes doing the same projection call and I am now producing the same values;

0.434 0 0 0
0 0.577 0 0 0
0 0 1 -1
0 0 1 0

This makes me thing my viewport matrix or W value is wrong.

Is there some part I am still mixing up here?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this