Hi,
I’m trying to compute the right fov for a scene. For testing purposes, I’ve created a cube starting at (0,0,0) and ending at (100,100,100). Then, inspired by the ‘red book’, I’ve done this :
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* Cube has size 100 and its distance to the
camera is 200 since the camera lies at (300,50,50) /
fov = calculate_angle(100.0,200.0);
gluPerspective(fov, (GLfloat)width/(GLfloat)height, 10.0, 500.0);
glMatrixMode(GL_MODELVIEW);
/ Viewport : right view /
glViewport (0, 0, (GLsizei)width, (GLsizei)height);
/ Set camera position (viewing transformation) /
glLoadIdentity();
gluLookAt(300.0, 50.0, 50.0, 50.0, 50.0, 50.0, 0.0, 1.0, 0.0);
/ Draw Cube (not centered at the origin) */
draw_cube();
[…]
where calculate_angle is :
/* Calculates the projection’s fov. It requires
the size of the object and its distance from the
viewpoint */
double calculate_angle(double size, double distance)
{
double radtheta, degtheta;
radtheta = 2.0 * atan2(size*0.5,distance);
degtheta = (180.0 * radtheta) / PI;
return degtheta;
}
The problem with this approach is that the produced fov is too small and the cube doesn’t fit entirely on screen. For instance, using the values in this example the produced fov is 28.072 when a fov of at least 60.0 is needed. I’m completely at a loss here, so any help would be greatly appreciated. Thanks.
Frederico Jeronimo