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

时间:2017-10-09 00:04:59

标签: opengl glsl shader coordinate-transformation perspectivecamera

我想从相机的方向照亮顶点水平的平面。我希望在旋转视图时灯光不会改变,但是当我略微偏离时它是最亮的。 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) );
}

1 个答案:

答案 0 :(得分:4)

  

模型视图投影矩阵中的第4列是查看位置吗?

不,不是。视图矩阵的第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;