相机矩阵为4乘4矩阵

时间:2015-11-26 14:28:16

标签: opengl

有人可以告诉我4 x 4矩阵M [16]中的相应索引,可以分配虚拟摄像机参数。例如参数

marker.getAttribute(IMarker.CHAR_START);
marker.getAttribute(IMarker.CHAR_END);

1 个答案:

答案 0 :(得分:1)

实际上,这取决于您是否需要列主要或行主要矩阵。不同之处在于矩阵如何存储在内存中。 Column-major表示它在列之后存储列,而row-major表示它是逐行存储的。

下面是一个函数的可能实现,该函数根据位置,目标和向上向量构造完整的视图矩阵。我从这里得到了Understanding the view matrix。本文还解释了很多关于矩阵算法的一般情况以及进行3D图形渲染时所需的常见变换。我通过简单的谷歌搜索获得了这篇文章。您可能想在下次发布问题之前考虑自己尝试这个。

mat4 LookAtRH( vec3 eye, vec3 target, vec3 up )
{
    vec3 zaxis = normal(eye - target);    // The "forward" vector.
    vec3 xaxis = normal(cross(up, zaxis));// The "right" vector.
    vec3 yaxis = cross(zaxis, xaxis);     // The "up" vector.

    // Create a 4x4 orientation matrix from the right, up, and forward vectors
    // This is transposed which is equivalent to performing an inverse 
    // if the matrix is orthonormalized (in this case, it is).
    mat4 orientation = {
       vec4( xaxis.x, yaxis.x, zaxis.x, 0 ),
       vec4( xaxis.y, yaxis.y, zaxis.y, 0 ),
       vec4( xaxis.z, yaxis.z, zaxis.z, 0 ),
       vec4(   0,       0,       0,     1 )
    };

    // Create a 4x4 translation matrix.
    // The eye position is negated which is equivalent
    // to the inverse of the translation matrix. 
    // T(v)^-1 == T(-v)
    mat4 translation = {
        vec4(   1,      0,      0,   0 ),
        vec4(   0,      1,      0,   0 ), 
        vec4(   0,      0,      1,   0 ),
        vec4(-eye.x, -eye.y, -eye.z, 1 )
    };

    // Combine the orientation and translation to compute 
    // the final view matrix
    return ( orientation * translation );
}
相关问题