LWJGL Tutorial Series - Perspective Projection
Welcome to the tenth part of the LWJGL Tutorial Series. In the previous tutorial, I've showed you how to render a pyramid in Orthographic Projection. In this tutorial, let's learn how to create a Perspective Projection which gives more natural 3D effect. So let's get started and first learn what Perspective Projection is.
Difference between Orthographic and Perspective projections
Why should we move to another type of projection? Our pyramid is rendering fine? The problem with Orthographic projection which we are using till now is that it uses the z-coordinate only to select which vertices should come first, that is even if an object has a large depth into the scene, it appears nearer. This is because the sizes of near and far planes being equal. This is how the orthographic looks like.
And this is how the Perspective projection looks like. The main difference is the size of the near plane which distinguishes the two types of projections.
Since the size of the near plane and far planes are equal in the Orthographic projection, it shows distant objects at the same depth of the nearer objects. To solve this problem, Perspective projection was invented. Since the size of the near plane is now smaller than the far plane, the distant objects now appear smaller than the nearer objects. Now that we have understood the difference between the two types of projections, let's now get into some code to change to perspective projection.
Now that we have understood the working of the Perspective projection, let's now set the projection. There are two methods that can setup the perspective projection for us. One of them is the actual OpenGL function called
and the other is a utility method called
which exists in
class. Personally I'm more comfortable with
so that I'm going to use it for the example at the end but I'm going to explain both of the methods for you. At first let me explain the
is just a variation of the
function and it takes an angle in degrees called 'Field Of View' and takes aspect ratio of the window size to setup the coordinate axis. This is the syntax of it.
void gluPerspective(float fovy, float aspect, float zNear, float zFar);
You may confuse what is field of view. So here is a visual defining the angle.
This means if you draw two lines from the eye to the top and bottom of the near plane, then the angle between those two lines is called the field of view. There is no perfect value for this but the actual angle in the real world lies around 70 degrees. You can change it to change the area of the scene which can be viewed. Then the aspect ratio is nothing but the quotient of the division of screen width by the screen height. There is no need to define
since these are already explained in the previous tutorial. So to change to the perspective projection using
function, we use
gluPerspective(70f, 800f / 600f, 1, 1000);
This creates a perspective projection with the camera at the origin. So if you are having problems viewing your model, you'll have to translate it further into the screen to view it perfectly. Now let me explain the
function to create the perspective projection.
Now let use see how to create a perspective projection using the
function. So let's start with its syntax.
void glFrustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
If you are confused what are the values, the values are clearly defined in this visual.
There is nothing to explain this function since it's so similar to the
function we used till now. So I'm going to just show you the code which sets up perspective projection by taking the values we passed till now to the
glFrustum(-1, 1, -1, 1, 1, 1000);
The drawback is that you have to calculate the coordinates for the aspect ratio of the window. So for future tutorials, we'll be using
function. That's the end of this concept and in the next part, I'll show you a demo using this which creates a rotating cube using triangles.
If there are any corrections or improvements that needs to be made, please notify them to me using comments.
There is no source code associated with this tutorial since this tutorial intended to cover some concepts we will be using in the next part of the tutorial.