实现图形管道 [英] Implementing graphics pipeline
问题描述
继昨天的这则讯息之后。情况与原始帖子中的情况相同:我实现了自己的图形管道,变换的奇怪问题。然而,我现在相信这个问题不是透视投影,而是一个比这个问题更大的问题。
现在,管道只有三个阶段,第一个使用模型和投影矩阵转换单个顶点。接下来将它们组装成原语,这些原语最终由栅格化器呈现。这是测试运行的输出:
这是顶点数据:
顶点1:0,0,-1,1
顶点2:0.5,1,-1,1
顶点3:1,0,-1,1
当没有发生变换时(也就是说模型矩阵是单位矩阵):
模型矩阵
pre>
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
新垂直x:0.000000 y:0.000000 z:-1.000000 w:1.000000
新垂直x:0.500000 y:1.000000 z:-1.000000 w:1.000000
新垂直x:1.000000 y:0.000000 z:-1.000000 w:1.000000
投影矩阵
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
新垂直x:0.000000 y:0.000000 z:-1.202202 w:1.000000
新垂直x:0.965685 y:2.414213 z:-1.202202 w:1.000000
新垂直:1.931371 y:0.000000 z:-1.202202 w:1.000000
除以W
新垂直x:0.000000 y:0.000000 z:-1.202202 w:1.000000
新垂直x:0.965685 y:2.414213 z:-1.202202 w:1.000000
新垂直x:1.931371 y:0.000000 z:-1.202202 w:1.000000
屏幕空间矩阵
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
新垂直x:400.000000 y:320.000000 z:-1.202202 w:1.000000
新垂直x:786.274170 y:1092.548340 z:-1.202202 w:1.000000
新垂直x:1172.548340 y:320.000000 z:-1.202202 w:1.000000
其结果如下:
这对于我来说看起来是合理的,0,0,0顶点看起来是在原点的中心,其他点在屏幕外面,在-1远离相机是我所期望的。
当我调整模型矩阵使得:
模型矩阵
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
新垂直x:-4.799998 y:-3.999998 z :-3.300000 w:1.000000
新垂直x:-4.299998 y:-2.999998 z:-3.300000 w:1.000000
新垂直x:-3.799998 y:-3.999998 z:-3.300000 w:1.000000
投影矩阵
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
新垂直x:-9.270576 y:-9.656849 z:-3.506807 w:1.000000
新垂直x:-8.304890 y:-7.242636 z:-3.506807 w:1.000000
新垂直x:-7.339205 y: -9.656849 z:-3.506807 w:1.000000
除以W
新垂直x:-2.809265 y:-2.926318 z:-1.062669 w:3.300000
新vert x:-2.516633 y:-2.194738 z:-1.062669 w:3.300000
新vert x:-2.224001 y:-2.926318 z:-1.062669 w:3.300000
屏幕空间矩阵
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
新垂直x:196.293823 y:119.578247 z :-1.062669 w:3.300000
新垂直x:313.346680 y:353.683777 z:-1.062669 w:3.300000
新垂直x:430.399414 y:119.578247 z:-1.062669 w:3.300000
此数据的输出为:
这看起来不对我,但我不能看到在矩阵堆栈我的问题是。
解决方案与原始帖子中一样:
所以我回到这个项目后,经过一些休息,我意识到,我的投影鸿沟(aka / w)只是除以xyz坐标。因此,当我应用屏幕空间矩阵translate列乘以大于一个W值,使得三角形随着Z减小而移动到右上角。
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: 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:
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?
解决方案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."
这篇关于实现图形管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!