## distance vs. lens angle

Mar 31, 2008 at 9:11pm

# distance vs. lens angle

Hi!

Is there someone who can guide me to some information regarding the
relation between the lens angle and the distance of the camera?
I want to move the camera away or towards an object in relation to
the lens angle so that the relative size on screen of the object
stays the same…

thx!.
B.

#36651
Mar 31, 2008 at 9:16pm

P.S. I have asked a similar question before, but I’m looking for some
more background info, math explanation or something similar. I’ve
been quite some googeling but haven’t found anything solid (read:
what my puny mind can fathom) so far.

regards,
B.

On 31 Mar 2008, at 23:11, Brecht Debackere wrote:

> Hi!
>
> Is there someone who can guide me to some information regarding the
> relation between the lens angle and the distance of the camera?
> I want to move the camera away or towards an object in relation to
> the lens angle so that the relative size on screen of the object
> stays the same…
>
> thx!.
> B.

#125909
Apr 1, 2008 at 5:59pm

On Mon, Mar 31, 2008 at 9:11 PM, Brecht Debackere
wrote:

>
> Is there someone who can guide me to some information regarding the
> relation between the lens angle and the distance of the camera?
> I want to move the camera away or towards an object in relation to
> the lens angle so that the relative size on screen of the object
> stays the same…
>
>
Hi Brecht,

I guess you’re talking about OpenGL no? I’ve been playing around with camera
stuff recently. This is one of those things I would definitely implement
using a procedural language instead of patching. Disclaimer: I haven’t
tested this, and its pseudo code, so it won’t work straight out of the box.

OK here we go…

Calculate the distance of the object from the camera by subtracting one
point from the other and taking the magnitude of the resulting vector:

distance = magnitude (object_pos – cam_pos)

Set “screen” height to a ratio of your object height:
screen_h = object_h * ratio

Now you can calculate the field of view in y direction (fovy) by a simple
trigonometric function (see attached crappy drawing):

tan x = screen_h / distance
x = arctan(screen_h / distance)

convert x (in radians) to degrees:
fovy = x*(180/PI)

Now you should have enough information to set up your camera.
Your “up” and “lookat” vectors are for you to choose. Here’s how you
set the projection and modelview matrix to match your camera settings:

/// SET CAMERA
glMatrixMode(GL_PROJECTION)
glPushmatrix()
gluPerspective(fovy, aspect, near, far)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
gluLookat(cam_x, cam_y, cam_z, lookat_x, lookat_y, lookat_z, up_x, up_y,
up_z)

/////// DO YOUR DRAWING HERE /////////

/// RESET CAMERA
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()