Chapter Three
Chapter Three
Chapter Three
1
OUTLINE
The Role of OpenGL in the Reference Model
Coordinate Systems
Viewing Using a Synthetic Camera
Output Primitives and Attributes
2
What Is OpenGL?
4
Cont..
6
Cont..
Rendering Pipeline is the sequence of steps that OpenGL takes when
steps to generate the final image on the screen. There are usually 9-steps in
this pipeline most of which are optional and many are programmable.
7
Cont..
The Sequence of steps taken by
openGL to generate an image are
the following:
8
Cont..
of vertices that define the boundaries of the primitive. Along with this,
one can define other vertex attributes like color, texture coordinates etc.
9
Cont..
2. Vertex Shader : The vertex specification defined above now pass through
Vertex Shader.
Vertex Shader is a program written in GLSL that manipulate the vertex data.
The main job of a vertex shader is to calculate the final positions of the
vertices in the scene.
10
Cont..
Vertex shaders are executed once for every vertex(in case of a triangle it
will execute 3- times) that the GPU processes. So if the scene consists of
one million vertices, the vertex shader will execute one million times once
11
Cont..
4. Geometry Shader : This shader stage is also optional.
The work of Geometry Shader is to take an input primitive and generate zero or
more output primitive. If a triangle strip is sent as a single primitive, geometry shader
13
Cont..
6. Primitive Assembly : This stage collects the vertex data into a
ordered sequence of simple primitives(
14
Cont..
8. Fragment Shader : Although not necessarily a required stage it is
used 96% of the time
Some of these tests for example are Pixel ownership test, Scissor
Test, Stencil Test, Depth Test.
16
cont..
17
THE ROLE OF OPENGL IN THE REFERENCE MODEL
18
Cont..
OpenGL is the software interface to graphics hardware. In other
words, OpenGL graphic rendering commands issued by your
applications could be directed to the graphic hardware and
accelerated.
19
Cont..
We use 3 sets of libraries in our OpenGL programs:
20
Cont..
2. OpenGL Utility Library (GLU): built on-top of the core
OpenGL to provide important utilities (such as setting camera
view and projection) and more building models (such as quadric
surfaces and polygon tessellation).
GLU commands start with a prefix "glu"
21
Cont..
23
Cont.
Summary of the most important of the OpenGL family of graphics libraries:
OpenGL: the core library, it is platform (i.e. hardware system) independent, but not
windows-system independent (i.e. the code for running on Microsoft Windows will
be different to the code for running on the UNIX environments, X-Windows or
Gnome).
glut: The GL Utilities Toolkit, it contains some extra routines for drawing 3D
objects and other primitives. Using glut with OpenGL enables us to write windows-
system independent code.
glu: The OpenGL Utilities, it contains some extra routines for projections and
rendering complex 3D objects.
glui: Contains some extra routines for creating user-interfaces.
24
Cont.
Every routine provided by OpenGL or one of the associated libraries listed above
follows the same basic rule of syntax:
The prefix of the function name is either gl, glu, or glut, depending on which of
these three libraries the routine is from.
The main part of the function name indicates the purpose of the function.
The suffix of the function name indicates the number and type of arguments
expected by the function.
For example, the suffix 3f indicates that 3 floating point arguments are expected.
o The function glVertex2i is an OpenGL routine that takes 2 integer arguments and defines
as vertex.
25
Cont.
Some function arguments can be supplied as predefined
symbolic constants. (These are basically identifiers that have
been defined using the C++ #define statement.)
These functions are always written in a capital letter.
For example, GL_RGB, GL_POLYGON and
GLUT_SINGLE are all symbolic constants used by OpenGL
and its associated libraries;
26
Getting Started with OpenGL
The first thing in OpenGL is we need to know which header files to include.
If we are only using the core OpenGL library, then the following line must be added
If we also want to use the GL Utilities library, we must add the following line:
#include <GL/glu.h>
#include <GL/glui.h>
27
Cont.
If we want to use the glut library (and this makes using OpenGL a
lot easier) we do not need to include the OpenGL or glu header files.
#include <GL/glut.h>
This will automatically include the OpenGL and glu header files too.
28
Cont.
Before displaying any graphics primitives we always need to perform some
basic initialization tasks and creating a window for display.
30
Cont.
4. To set a number of other options for the display window, such as the type of
frame buffer we want to have and a choice of color modes, we use the
glutInitDisplayMode function.
The most important event is the display event. This occurs whenever
It is guaranteed to happen once at the beginning of the event loop; after this
Any primitives that we want to draw should be drawn in the display event
glutDisplayFunc(myDisplay);
To start the event loop we write:
glutMainLoop();
34
Cont.
Define any required callback functions and associate them with the
appropriate events.
35
Cont.
#include<GL/glut.h> glutCreateWindow("GLUT Points
int main(int argc, char *argv[])
demo");
{
glutDisplayFunc(display);
glutInit(&argc, argv);
glutInitWindowSize(640,480); myInit();
glutInitWindowPosition(10,10); glutMainLoop();
glutInitDisplayMode(GLUT_SINGLE|
}
GLUT_RGB);
36
Coordinate systems
Coordinate systems are an important concept in computer graphics, as they
are used to specify the position and orientation of objects in a virtual space.
A coordinate system is a system of numbers, symbols, and axes that is used
to describe the position of a point or object in space.
37
Cont.
In geometry, a coordinate system is a system that uses one or more numbers,
elements on a manifold.
The order of the coordinates is significant, and they are sometimes identified by their
The coordinates are taken to be real numbers in elementary mathematics, but may
ring.
38
Common Coordinate Systems
1. Number line: The simplest example of a coordinate system is the identification of
points on a line with real numbers using the number line. In this system, an arbitrary
point O (the origin) is chosen on a given line
40
Cont..
There are several different types of coordinate systems that are commonly used in
computer graphics, including the cartesian, polar, and spherical coordinate
systems.
The most commonly used coordinate system in computer graphics is the Cartesian
coordinate system, which is named after the mathematician René Descartes. In this
system, a point in space is defined by two or three numbers: its x-coordinate, y-
coordinate. The x-coordinate specifies the point's position along the x-axis, the y-
coordinate specifies the point's position along the y-axis. In 3D graphics a 3rd z-
coordinate is used.
41
Cartesian Coordinates (Rectangular Coordinate System)
2d coordinates 3d coordinates
42
3D Coordinates Cartesian
2 type
Left-handed system Right-handed system
44
convert Cartesian to polar coordinates
To convert from Cartesian to polar coordinates, you need to first
determine the radius of the point. This can be done using the
Pythagorean theorem, which states that the square of the radius
is equal to the sum of the squares of the x-coordinate and the y-
coordinate. In other words, the radius is the square root of the
sum of the squares of the x-coordinate and the y-coordinate.
45
Example: what is (12, 5) in polar coordinates?
46
Cont..
Once the radius has been determined, the angle can be calculated using the arc tangent
function. This function takes the y-coordinate and the x-coordinate as inputs and
returns the angle in radians. The angle can then be converted to degrees if desired.
For example, to convert the point (3,4) from Cartesian to polar coordinates, first
determine the radius using the Pythagorean theorem:
radius = sqrt(3^2 + 4^2) = 5
Next, use the arc tangent function to determine the angle:
angle = atan2(4/3) = 53.13 degrees
Therefore, the polar coordinates of the point (3,4) are (5,53.13).
47
Cont.
computer graphics.
and later place this model into a scene in the world coordinate frame.
We often refer to the modeling frame as the object frame, and the
49
View Volume
A view volume contains everything visible from the point of view or direction (it
bounds portion of 3D space that is to be clipped out and projected onto view plane).
What does the camera see?
Conical view volumes:
Approximates what eye sees
Expensive math (simultaneous quadratic equations)
when clipping objects against cone’s surface.
Can approximate with rectangular cone instead (called a frustum)
Works well with a rectangular viewing window
Simultaneous linear equations for easy clipping
50
Position
Determining the Position is analogous to a photographer deciding the vantage point
from which to shoot a photo.
Three degrees of freedom: x, y, and z coordinates in 3-space
This x, y, z coordinate system is right-handed: if you open your right hand, align your
fingers with the +x axis, and curl them towards the +y axis, your thumb will point
along the +z axis
51
Orientation
Orientation is specified by a point in 3D space to look at (or a
direction to look in) and an angle of rotation about this direction.
Default (conical )orientation is looking down the negative z-axis
and up direction pointing straight up the y-axis.
52
Output primitives and Attributes
Attribute of a primitive is a parameter that affects the way the
primitive is displayed.
OpenGL is a state system: it maintains a list of current state
variables that are used to modify the appearance of each primitive.
State variables represent the attributes of the primitives.
The values of state variables remain in effect until new values are specified.
Changes to state variables affects primitives drawn after the change.
53
Color Attributes
We can define colours in two different ways:
RGB: the colour is defined as the combination of its red, green
and blue components.
RGBA: the colour is defined as the combination of its red, green
and blue components together with an alpha value, which
indicates the degree of opacity/transparency of the colour.
54
Color Values
Color Code Red Green Blue Displayed Color
0 0 0 0 Black
1 0 0 1 Blue
2 0 1 0 Green
3 0 1 1 Cyan
4 1 0 0 Red
5 1 0 1 Magenta
6 1 1 0 Yellow
7 1 1 1 White
55
Cont.
Frame buffer is used by raster graphics systems to store the
image ready for display.
There are two ways in which graphics packages store color
values in a frame buffer:
Direct storage
Look-up table
56
Direct storage
The color values of the pixel are stored in the frame buffer.
Each pixel in the frame buffer must have 3 or 4 values stored.
57
Cont.
58
Color Look-up table
59
Cont.
Color look-up tables save storage, but they slow down the
rasterization process as an extra look-up operation is required.
They were commonly used in the early days of computer graphics
when memory was expensive, but these days memory is cheaper,
so most systems use direct storage.
OpenGL uses direct storage by default, but the programmer can
choose to use a color look-up table if they wish.
60
OpenGL Colour Attributes.
We change the current drawing colour using the glColor* function and the current
background colour using the glClearColor function. The background colour is always set
using an RGBA colour representation (although, as we will see below, the alpha value is
sometimes ignored).
The drawing colour can be set using either RGB or RGBA representations. The following
are all valid examples of setting the drawing and background colours:
glColor3f(1.0,0.0,0.0); // set drawing colour to red
glColor4f(0.0,1.0,0.0,0.05); // set drawing colour to semi-transparent green
glClearColor(1.0,1.0,1.0,1.0); // set background colour to opaque white
61
Cont.
Before we do any drawing in OpenGL, we must define a frame buffer.
To do color drawing we must specify a color frame buffer.
glutInitDisplayMode(GLUT_SINGLE,GLUT_RGB)
To store alpha values in the frame buffer.
glutInitDisplayMode(GLUT_SINGLE,GLUT_RGBA)
To use a color look-up table.
glutInitDisplayMode(GLUT_SINGLE,GLUT_INDEX)
62
Point Attributes
Points are the simplest type of primitive.
The only attributes we can modify for point are the color and
size.
To change the size of points in pixels we use:
glPointSize(size)
The default point size is 1 pixel.
63
Line Attributes
The attributes of line primitives that we can modify include the
following:
Line color
Line width
Line style (solid/dashed etc.)
64
Line Color
Using this function along with the Line() function, users can able to draw
a colorful line. There in this function, we have to provide the color name
as the argument.
This function needs to be placed before the Line() function. This will help
66
Cont.
different y-coordinate.
67
Cont.
If |m|> 1, plot extra pixels horizontally, i.e. same y-coordinate,
different x-coordinate.
68