25-08-2017, 09:32 PM

1457586620-cg.docx (Size: 35.22 KB / Downloads: 4)

The header are included to access the OpenGL libraries. Then there is variable for the angle of rotation of the cube. Next we call the display function which will have the matrix that holds the vertices as well the color values in them.

Rest display function will have the transformation functions and drawing of the cubes. Store the vertices of the cube by calling the glPushMatrix(). It pushes the matrix values on the stack (different stacks for glMatrixmode modes). Move all vertices by vector called on glTranslatef() and rotate by the declared angled calling on glRotatef() toward the axis coded.

As mentioned in the above there are many modes for glMatrixmode one is GL_PROJECTION which is to set the projection like widening the camera lenses and GL_MODELVIEW is for the setting up the object that is to be drawn, like size and place in space. The glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. glLoadIdentity() replaces the current matrix with the identity matrix. glFrustum() describes a perspective matrix that produces a perspective projection.

Projection Transform

GLfloat aspect = (GLfloat)width / (GLfloat)height; // Compute aspect ratio of window

glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix

glLoadIdentity(); // Reset

gluPerspective(45.0f, aspect, 0.1f, 100.0f); // Perspective projection: fovy, aspect, near, far

A camera has limited field of view. The projection models the view captured by the camera. There are two types of projection: perspective projection and orthographic projection. In perspective projection, object further to the camera appears smaller compared with object of the same size nearer to the camera. In orthographic projection, the objects appear the same regardless of the z-value. Orthographic projection is a special case of perspective projection where the camera is placed very far away. We shall discuss the orthographic projection in the later example.

To set the projection, we need to operate on the projection matrix. (Recall that we operated on the model-view matrix in model transform.)

We set the matrix mode to projection matrix and reset the matrix. We use the gluPerspective() to enable perspective projection, and set the fovy (view angle from the bottom-plane to the top-plane), aspect ratio (width/height), zNear and zFar of the View Frustum (truncated pyramid). In this example, we set the fovy to 45°. We use the same aspect ratio as the viewport to avoid distortion. We set the zNear to 0.1 and zFar to 100 (z=-100). Take that note the color-cube (1.5, 0, -7) and the pyramid (-1.5, 0, -6) are contained within the View Frustum.

The projection transform transforms the view frustum to a 2x2x1 cuboid clipping-volume centered on the near plane (z=0). The subsequent viewport transform transforms the clipping-volume to the viewport in screen space. The viewport is set earlier via the glViewport()