Lekan Lanihun
Lekan Lanihun

Reputation: 45

Camera matrix to a 4 by 4 Matrix

Can someone let me know the corresponding index in a 4 by 4 matrix M[16] that a virtual camera parameters can be assigned. for example the parameters

camera->setPosition(Vec(P[0],P[1],P[2]);
camera->setviewDirection(Vec(v[0],v[1],v[2]);
camera->setupVector(Vec(up[0],up[1],up[2]);

Upvotes: 0

Views: 132

Answers (1)

Jupiter
Jupiter

Reputation: 1532

Indeed it depends on whether you want a column-major or row-major matrix. The difference is how matrices are stored in memory. Column-major means that it is stored column after column and row-major means it is stored row after row.

Below is a possible implementation of a function that constructs a complete view matrix from the position, target and up vectors. I got it from here Understanding the view matrix. The article also explains a lot about matrix arithmetic in general and common transformations needed when doing 3d graphics rendering. I got the article by doing a simple google search. You might want to consider trying this yourself next time before posting a question.

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 );
}

Upvotes: 1

Related Questions