Search code examples
c++mathmatrixgraphicsprojection

Implementing graphics pipeline


Following on from this post from yesterday. The situation is the same as in the original post: I'm implementing my own Graphics pipeline, odd issue with transforms. However, I now believe that the issue isn't with the perspective projection and is a larger issue than just that.

Right now the pipeline only has three stages, the first transforms individual vertices using the model and projection matrices. The next assembles these into the primitives, which are finally rendered by the rasteriser. This is the output from a test run:

This is the vertex data:

 Vertex1:  0  ,  0,  -1,  1
 Vertex2:  0.5,  1,  -1,  1
 Vertex3:  1  ,  0,  -1,  1

When no transforms take place (that is to say model matrix is identity matrix):

Model Matrix
1.000000 0.000000 0.000000 0.000000 
0.000000 1.000000 0.000000 0.000000 
0.000000 0.000000 1.000000 0.000000 
0.000000 0.000000 0.000000 1.000000 

    New vert x: 0.000000 y: 0.000000 z: -1.000000 w: 1.000000
    New vert x: 0.500000 y: 1.000000 z: -1.000000 w: 1.000000
    New vert x: 1.000000 y: 0.000000 z: -1.000000 w: 1.000000

Projection Matrix
1.931371 0.000000 0.000000 0.000000 
0.000000 2.414213 0.000000 0.000000 
0.000000 0.000000 1.002002 -0.200200 
0.000000 0.000000 -1.000000 0.000000 

    New vert x: 0.000000 y: 0.000000 z: -1.202202 w: 1.000000
    New vert x: 0.965685 y: 2.414213 z: -1.202202 w: 1.000000
    New vert x: 1.931371 y: 0.000000 z: -1.202202 w: 1.000000

Divide by W

    New vert x: 0.000000 y: 0.000000 z: -1.202202 w: 1.000000
    New vert x: 0.965685 y: 2.414213 z: -1.202202 w: 1.000000
    New vert x: 1.931371 y: 0.000000 z: -1.202202 w: 1.000000

Screenspace Matrix
400.000000 0.000000 0.000000 400.000000 
0.000000 320.000000 0.000000 320.000000 
0.000000 0.000000 1.000000 0.000000 
0.000000 0.000000 0.000000 1.000000 

    New vert x: 400.000000 y: 320.000000 z: -1.202202 w: 1.000000
    New vert x: 786.274170 y: 1092.548340 z: -1.202202 w: 1.000000
    New vert x: 1172.548340 y: 320.000000 z: -1.202202 w: 1.000000

The result of this looks like: enter image description here Which looks reasonable to me, the 0,0,0 vertex appears to be centered on the origin with the other points being out of the screen which at -1 away from the camera is what I would expect.

When I adjust the model matrix so that:

Model matrix
1.000000 0.000000 0.000000 -4.799998 
0.000000 1.000000 0.000000 -3.999998 
0.000000 0.000000 1.000000 -2.300000 
0.000000 0.000000 0.000000 1.000000 
    New vert x: -4.799998 y: -3.999998 z: -3.300000 w: 1.000000
    New vert x: -4.299998 y: -2.999998 z: -3.300000 w: 1.000000
    New vert x: -3.799998 y: -3.999998 z: -3.300000 w: 1.000000

Projection Matrix
1.931371 0.000000 0.000000 0.000000 
0.000000 2.414213 0.000000 0.000000 
0.000000 0.000000 1.002002 -0.200200 
0.000000 0.000000 -1.000000 0.000000 

    New vert x: -9.270576 y: -9.656849 z: -3.506807 w: 1.000000
    New vert x: -8.304890 y: -7.242636 z: -3.506807 w: 1.000000
    New vert x: -7.339205 y: -9.656849 z: -3.506807 w: 1.000000


Divide by W
    New vert x: -2.809265 y: -2.926318 z: -1.062669 w: 3.300000
    New vert x: -2.516633 y: -2.194738 z: -1.062669 w: 3.300000
    New vert x: -2.224001 y: -2.926318 z: -1.062669 w: 3.300000


Screenspace Matrix
400.000000 0.000000 0.000000 400.000000 
0.000000 320.000000 0.000000 320.000000 
0.000000 0.000000 1.000000 0.000000 
0.000000 0.000000 0.000000 1.000000 

    New vert x: 196.293823 y: 119.578247 z: -1.062669 w: 3.300000
    New vert x: 313.346680 y: 353.683777 z: -1.062669 w: 3.300000
    New vert x: 430.399414 y: 119.578247 z: -1.062669 w: 3.300000

The output from this data is: enter image description here

This doesn't look right to me but I can't see where in the matrix stack my problem is. From the above can anyone else see any problem with the matrix stack?


Solution

  • As in the original post:

    "So I came back to this project after quite some break and I realised that my projection divide (aka /w) was only dividing the xyz coords. So when I applied the screenspace matrix the translate column was being multiplied by the 'greater than one' W value causing the triangle to move to the upper right as Z decreases."