# 3D Projection using lens formula

This is how I re-invented the wheel 🙂

This is an alternate approach to do projection in computer graphics using a lens formula. As it is a technique which is derived from lens formula, the focal length of the lens is the major parameter. The traditional projection techniques uses the ‘field of view’ value – which is an angle expressed in degrees or radians.

Anyways, if you are a person who didn’t even experienced the 3D graphics programming, you may ask these following questions to yourself before you get started.

1. How to draw 3D objects on a 2D Plane?

2. How can we plot a 3D vertex(x, y, z) into a 2D plane which has only 2 axes.

I asked this questions to myself long time back before attending some 3D graphics lessons. As a result, after some days of deep thought, I landed up with an idea to go through my Physics book. I flipped through the pages and stopped at the chapter Ray Optics. Finally, after some days, I ended up with some clues to solve the above problems. The final outcome of this was an equation by which we can accomplish projection. Later when I covered computer graphics I came to know that this is called the “Projection” operation in 3D Graphics. Yeah. The moment I came to know that I re-invented the wheel.

Here we start,

$\frac{1}{v} - \frac{1}{u} = \frac{1}{f}$

Most probably you might be familiar with this equation. Its the equation of a convex lens. The main reason why I choose a lens is that, it can project a 3D objects on a 2D plane. For eg, have a look at the working princpile of an human eye. The lens of the eye projects the 3D world around us into a small plane inside the eyeball – called retina. Similarly, a camera does the same thing. It projects the world into a photographic film or a digital sensor.

Now lets visualize the 3D coordinate system(Right handed), where the Y axis grows up, X Axis grows right and the Z axis grows towards the viewer. Place the lens at the Origin facing the viewer so that the Z axis coincides with the principal axis,also the Origin coincides with the Optical Center of the lens. Now lets place a point object P(x1, y1, z1) infront of the lens as shown below

Lets setup a diagram and workout a formula

From the diagram we’ll calculate projected image coordinate(x2, y2) from original 3D coordinate values (x1, y1, z1)
Calculate x2
OC = u= z1 = Distance of the object from the lens
IC= v = Distance of the image from the lens
OB = x1
IM = x2
Triangles $\bigtriangleup BOC$ and $\bigtriangleup MIC$ are similar
$therefore \frac{OB}{OC} = \frac{IM}{IC}$ ———- (1)
$IM = \frac{x_{1} \times v}{u}$

$=\frac{x_1 \times frac{uf}{(f+u)}}{u}$

$=x_1 \times \frac{f}{f+u}$

$=x_1 \times \frac{f}{f+z_1}$

ie,

$x_2 = x_1 \times \frac{f}{f+z_1}$

Calculating $y_2$

Similarly, for $y_1$, substitute $y_1$ for OB in eq (1), we’ll get,

$y_2 = y_1 \times \frac{f}{f+z_1}$

So we can conclude,

A convex lens with focal length ‘f’ projects a 3D coordinate $(x_1, y_1, z_1)$ into a plane, at position $(x_1 \times \frac{f}{f+z_1}, y_1 \times \frac{f}{f+z_1})$
The implementation of this algorithm can be found here