模型视图投影中的第 4 行是观看位置吗?

Is 4th row in model view projection the viewing position?

我想从相机的方向在顶点级别照亮一个平面。我希望当我旋转视图时照明不会改变,但当我稍微移开视线时它最亮。 mvp[3]是不是像我想的那样是相机坐标?

#version 450
in vec3 vertex;
uniform mat4 mvp;
out vec4 color;

void main()
{
    gl_Position =  mvp * vec4(vertex,1.);
    vec3 n = vec3(0.,0.,1.);
    vec3 v = normalize( vec3(mvp[3])-vertex );
    //I tried the other direction in the mat4
    //vec3(mvp[0][3],mvp[1][3],mvp[2][3]);

    color = vec4( dot(v,n) );
}

Is 4th column in the model view projection matrix the viewing position?

不,不是。视图矩阵的第 4 列将包含相机位置,但模型视图投影矩阵是模型矩阵、视图矩阵和投影矩阵的组合。

视图矩阵通常如下所示:

mat4 view;

view[0] : ( X-axis.x, X-axis.y, X-axis.z, 0 )
view[1] : ( Y-axis.x, Y-axis.y, Y-axis.z, 0 )
view[2] : ( Z-axis.x, Z-axis.y, Z-axis.z, 0 )
view[3] : ( trans.x,  trans.y,  trans.z,  1 ) 

透视投影矩阵可能如下所示:

r = right, l = left, b = bottom, t = top, n = near, f = far 

mat4 projection;

projection[0] :     2*n/(r-l)      0              0                0
projection[1] :     0              2*n/(t-b)      0                0
projection[2] :     (r+l)/(r-l)    (t+b)/(t-b)    -(f+n)/(f-n)    -1    
projection[3] :     0              0              -2*f*n/(f-n)     0

矩阵乘法是这样的:

mat4 matA;
mat4 matB;{

mat4 matC;
for ( int i0 = 0; i0 < 4; ++ i0 )
    for ( int i1 = 0; i1 < 4; ++ i1 )
        matC[i0][i1] = matB[i0][0] * matA[0][i1] + matB[i0][1] * matA[1][i1] + matB[i0][2] * matA[2][i1] + matB[i0][3] * matA[3][i1];

接下来,视图投影矩阵的第 4 列包含以下内容:

mv[3][0] =  trans.x * 2*n/(r-l)   + trans.z * (r+l)/(r-l);
mv[3][1] =  trans.y * 2*n/(t-b)   + trans.z * (t+b)/(t-b);
mv[3][2] = -trans.z * (f+n)/(f-n) - 2*f*n/(f-n);
mv[3][3] = -trans.z;