I have 3*4 Camera Matrix which is P = K[R | -RC]
where K
is intrinsic camera matrix, R
is rotation matrix, C
is camera position with respect to world origin and -RC
is translation. I can decompose P
to K
, R
and C
. How I want to render in openGL using this camera information. How should I set View Matrix and Projection Matrix using K
, R
, C
; in openGL such that it represent the actual camera position ?
1 Answers
In OpenGL, all matrices are 4x4 homogeneous transforms, described below:
[Xx][Yx][Zx][Tx]
[Xy][Yy][Zy][Ty]
[Xz][Yz][Zz][Tz]
[Xw][Yw][Zw][Tw]
Your camera matrix and view matrix is the same thing, and is constructed from an "up" vector, a "location" vector, and a "direction" vector.
From these vectors, you can compute the view matrix:
vec3 Z = Eye - Location; //this is your "forward" vector
Z.Normalize();
vec3 Y = Up;
vec3 X = Y.Cross( Z ); //crossing Y with Z gives you a perpendicular vector, which is needed to construct a coordinate basis matrix
You then Construct the matrix like so:
[Xx][Yx][Zx][-X.Dot( Eye )]
[Xy][Yy][Zy][-Y.Dot( Eye )]
[Xz][Yz][Zz][-Z.Dot( Eye )]
[Xw][Yw][Zw][1.0]
Then you have your view matrix
Your projection matrix is unrelated to this data, and represents the view/world to screen space transform, and is dependent on your screen dimensions, field of view angle, and near & far clipping ranges.
Off the top of my head I can't remember how to compute the projection matrix from this data, so I will simply recommend, that if you are doing your GL work using C++, that you download and use the GLM mathematics library, which has these common functions already written.