Ambo University
Hachalu Hundessa Campus
Department of Computer Science
Computer Graphics
(CoSc3072)
1
Chapter 4
Windows and Viewports
2
Topics we will cover
The 3-D Graphics Viewing Pipeline
o The Modeling Transformation
o The Viewing Transformation
o The Projection Transformation
o Normalization and Clipping
o The Viewport Transformation
The OpenGL Viewing Pipeline
The Model view Matrix
The Projection Matrix
The Viewport Matrix
3 3-D Graphics Viewing Pipeline
The viewing pipeline is a sequence of
transformations that every primitive must undergo in
order to be displayed.
In general terms, the viewing pipeline consists of
the following transformations:
The Modelling Transformation
The Viewing Transformation
The Projection Transformation
Normalization and Clipping
The Viewport Transformation
Every transformation can be thought of as defining
a new coordinate system.
4
Viewing Pipeline
Figure 1- The 3-D Graphics Viewing Pipeline
5 [Link] Modeling Transformation
The modeling transformation is used to
transform primitives relative to each other to
form a 3-D scene.
After the modeling transformation all
primitives are in world coordinates.
Primitives must be transformed to get them in the
correct position, scale and orientation to
construct our scene.
6 [Link] Modelling Transformation
Figure 2 shows a wheel primitive being transformed in
four different ways to form the wheels of a car.
Figure 2- The Modeling Transformation
7 [Link] Viewing Transformation
The viewing transformation represents the
positioning, direction and orientation of the
virtual camera that will take the picture of the
scene.
o After the viewing transformation all primitives are in
viewing coordinates.
o Just like cameras in real life, virtual cameras must have a
position and an orientation.
o These parameters are typically represented by a camera
position, a ‘look’ vector and an ‘up’ vector.
The camera “looks” at z-axis, and the y-axis is “up”.
8
[Link] Viewing Transformation
Figure 3- The Viewing Coordinate System: the virtual camera ‘looks’ along the z-axis and the y-axis is
‘up’.
9 [Link] Projection Transformation
The projection transformation projects 3-D
points onto a 2-D image plane.
After projection the primitives are in projection
coordinates.
Taking picture involves ‘projecting’ 3-D primitives on to a 2-D
plane.
To be able to do this, we need to know something about the
type of camera we have.
10 [Link] Projection Transformation
Figure 4- The Projection Transformation
11
The Projection Types
Projection transformations can be:
Parallel projections or
Perspective projections
Parallel projections can be:
o Orthogonal (orthographic) or
o Oblique parallel projections.
12
The Projection Types
In Parallel projections the projection lines of
the two end-points are parallel:
o after they have passed through the image plane, they
continue to infinity without ever meeting.
Both orthographic and oblique projections
are commonly used in engineering
applications, particularly CAD software.
In a perspective projection all projection
lines converge to a point, the center of
projection.
The Projection Types
13
Figure 5- The Projection types
14
4. Normalization and Cliping
The normalization and clipping
transformation normalizes the view volume of
the projection.
Decide which primitives are ‘inside’ the
picture and which ones are ‘outside’.
In computer graphics, this process is known as
clipping.
Scales coordinates to the range [0,1] or [-1,1].
After this transformation the primitives are in
normalized coordinates.
To draw our picture into the display window.
4. Normalization and Cliping
15
Figure 6- Near and Far Clipping Planes
16
The View Volume of the Projection
Different types of projection transformation lead
to different shapes of view volume:
Using a perspective projection forms a view volume
in the shape of a frustum.
A parallel (orthographic) projection leads to a
view volume that is a cuboid (a 3-D rectangle).
17
The View Volume of the Projection
Figure 7- View volume of the projection
18 [Link] Viewport Transformation
The viewport transformation maps normalized
coordinates to device coordinates.
This involves drawing the projected primitives
onto a viewport of the screen window.
OpenGL creates a display window, or screen window.
If we wish, we can draw in the whole of the screen
window,
but we can also specify a sub region of it for drawing.
Whatever region we specify, it is known as the
viewport.
19
[Link] Viewport Transformation
Figure 8- The Viewport Transformation
20
The OpenGL Viewing Pipeline
OpenGL defines 3 matrices in its viewing pipeline.
1. The Projection Matrix
o This represents the projection of 3-D viewing coordinate onto the
image plane.
2. The Modelview Matrix
This matrix combines the effects of the modeling and viewing
transformations in the general graphics pipeline.
3. The Viewport Matrix
This matrix defines the part of the display that will be used for
drawing.
[Link] Model view Matrix
21
In OpenGL, the modeling and viewing transformations are
combined into a single matrix with a routine of:
o glMatrixMode(GL_MODELVIEW);
Now we can use any of the following OpenGL functions to
modify the current model view matrix:
glTranslate*
glRotate*
Used to define the modeling part of the
glScale* model view matrix.
glLoadMatrix*
glMultMatrix*
gluLookAt Used to define the viewing part of the
transformation.
22
gluLookAt
This function is used to define the viewing part of the
transformation.
Recall that the viewing transformation defines the
position, direction and orientation of a virtual camera.
The basic format of gluLookAt is as follows:
E.g. The following 3 lines define a virtual camera positioned at
(8, 0, 8) in world coordinates, looking at the origin, and with the y-
axis as ‘up’.
Example Code
23
24 [Link] Projection Matrix
In OpenGL, the projection transformation is represented
by the projection matrix.
glMatrixMode(GL_PROJECTION);
Depending on the type of projection required, OpenGL
provides 4 alternative functions for specifying the
projection matrix:
1) gluOrtho2D Used for specifying orthographic
parallel projections.
2) glOrtho
1) gluPerspective Used for specifying
perspective projections.
2) glFrustum
25
gluOrtho2D
The gluOrtho2D function is intended for use with
2-D graphics only,
o It does not allow you to specify near and far clipping planes.
The basic format of gluOrtho2D is:
For example, the lines of code shown below define a 2-D
orthographic projection that will display all primitives with x-
coordinate between 0 and 640, and y-coordinate between 0 and
480.
26
glOrtho
This is very similar to gluOrtho2D except that,
It allows us to specify near and far clipping planes, in addition to
the left, right, bottom and top ones.
The basic format is:
E.g. the following code specifies an orthographic projection that
defines a view volume that is a 24x24x30 cuboid.
gluPerspective
27
The basic format is:
Here, theta represents the height angle of the perspective
projection.
The next argument, aspect, is the aspect ratio of the image
(the ratio of the width to the height).
The last two arguments, dnear and dfar, are the z-
coordinates of the near and far clipping planes.
E.g.
28
glFrustum
The basic format is:
Here, the first 4 arguments are the x and y coordinates of
the left, right, bottom and top clipping planes at the
near clipping plane.
The last two arguments, dnear and dfar, are the z-
coordinates of the near and far clipping planes.
E.g.
29
[Link] Viewport Matrix
In OpenGL, the viewport transformation is represented by
the viewport matrix.
The OpenGL viewport matrix can be defined using the
glViewport function.
glViewport(xmin, ymin, xsize, ysize);
Here, (xmin, ymin) is the bottom-left corner of the
viewport and the arguments xsize and ysize are the size of
the viewport in pixels.
For example,
Example Code
30
Any Question?