|
I spent several days to convince openGL to display the 3D model at the right position in the image given the camera and because when it comes to transformations usually 5 people have 5 different opinions I want to share the code here. For those looking for an explanation ... it is at my desk ... somewhere ... actually I totally forgot :)
I assume the camera coordinate system specified in the book multiple view geometry, i.e. x-axis to the right, y-axis down, z-axis facing away from the camera. If you use the MATLAB camera calibration toolbox, this is exactly the output you get.
Here is the code:
void updateViewport ()
{
float u0 = // principal point x coordinate
float v0 = // principal point y coordinate
float fx = // focal x
float fy = // focal y
// camera matrix should look like this
// K = [ fx 0 u0
// 0 fy v0
// 0 0 1 ]
float w = // image width in pixels
float h = // image height in pixels
glViewport ( 0,0,w,h);
glMatrixMode( GL_PROJECTION );
float proj[] = {
fx/(w/2), 0.0, 0.0, 0.0,
0.0, fy/(h/2), 0.0, 0.0,
-(u0/(w/2)-1.0f), (v0/(h/2)-1.0f), (0.1f+1e6f)/(0.1f-1e6f), -1.0f,
0.0, 0.0, (2*0.1f*1e6f)/(0.1f-1e6f), 0.0
};
glLoadIdentity ();
if ( fx != 0 && fy != 0)
glLoadMatrixf ( proj );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
}
|