# How do 3D games create the illusion of depth?

How do libraries like OpenGL convert three dimensional coordinates into the two dimensional pixels we see on our screens? I'm not talking about the 3DS or games that require 3D glasses, but rather the illusion of three dimensions.

I don't need any code to create this effect - I'm just curious about the concept.

• en.wikipedia.org/wiki/Perspective_(graphical) Mar 29, 2015 at 18:02
• Probably the technical answer is simply perspective projection" (Google has a lot on the topic). You are most likely interested in *monocular depth perception (en.wikipedia.org/wiki/Depth_perception). Besides the Mathematics behind the technical explanation, depth perception is very close to being an art: photographers have a gut feeling of it and make use of different strategies when composing their photos. So, technical answer: perspective projection 3D to 2D. Nontechnical: the touch of an artist. Mar 29, 2015 at 18:02

## 1 Answer

As the commenters on the opening post have already pointed out, this mechanism is called 'perspective correction' or in 3D terms 'perspective projection'.

The basic idea is that as things get further away, they get smaller. It's all about trigonometry (calculating triangles).

You can calculate this by assuming that your Field of View is like a pyramid with a square base, where the point is at your eye (or camera) and the pyramid is aligned to the direction you are looking.

If you know the shape of your Field of View and you know the size and location of a point on screen (Near plane), you can extrapolate what size the object should be at a given distance when you scale proportionally.

Remember that objects closer to the Near plane will take up more of the view space and objects further away will take up less space.

Notice how in the picture below, everything gets smaller the further away it is.

The vanishing point is another way to think about it. The vanishing point is the place in the distance where everything converges (the Far plane accommodates this). You can observe this in real-life when you look down a street. If the street were long enough, it would look like a triangle.

You could think of a 2D shape like a circle on the screen and apply the same principle as the view frustum to the object itself, but in reverse. When you extrapolate the circle into a 3D shape, it becomes a cone. The circular base is in your view and the cone points away extending to the vanishing point. If you want to know how big to draw the circle when it is X units away, then you calculate the diameter of the cone at that distance.