Unit 2: Computer Graphics
Unit 2: Computer Graphics
Unit 2: Computer Graphics
Unit 2
2.1 The OpenGL API
2.1.1 Graphics functions
Graphics system can be viewed as a box whose inputs are function calls from a user program
(say measurements from input devices, such as the mouse and keyboard and possibly other
input, such as messages from the operating system); the outputs are primarily the graphics
sent to the output devices.
A good API may contain hundreds of functions, so it is helpful to divide the functions into six
groups by their functionality:
1. The primitive functions define the low-level objects or atomic entities that the system
can display. The primitive includes points, line segments, polygons, pixels, text, and
various types of curves and surfaces. The primitives tells what the objects that can be
displayed,
2. The attributes govern the way that a primitive appears on the display. Attribute
functions perform operations ranging from choosing the color, to picking a pattern with
which to fill the inside of a polygon, to selecting a type face for the titles on a graph. The
attributes tells how the objects can be displayed
3. The viewing functions specify various views, position and orientation in its world, and
equivalent of a lens must also be selected. This process not only will fix the view, but also
clip out objects that are too close or too far away.
4. Transformation functions carry out transformations of objects, such as rotation,
translation, and scaling.
5. For interactive applications, the API provides a set of input functions to deal with
diverse forms of input that characterize modern graphics systems say functions to deal
with devices such as keyboards, mice, and data tablets.
6. The control functions help to communicate with the window system, to initialize the
programs, and to deal with any errors that take place during the execution of programs.
7. Query functions: They provide useful information of API that can be useful in an
application. E.g. Camera parameters or values in the frame buffer.
GL (Graphics Library): OpenGL function names begin with the letters gl and are stored in
a library usually referred to as GL.
Graphics utility library (GLU): This library uses only GL functions, but contains code for
common objects, such as spheres, that users prefer not to have to write repeatedly. This
library is available in all OpenGL implementations.
GL Utility Toolkit (GLUT): It addresses the problems of interfacing with the window
system. It provides the minimum functionality that should be expected in any modern
windowing system.
Priyanka H V
Page 1
Computer Graphics
Figure below shows the organization of the libraries for an X Window system environment.
The value of type specifies how OpenGL interprets the vertices to define geometric objects.
Other code and OpenGL function calls can occur between glBegin and glEnd.
Attributes can be changed or calculations can be performed for the next Vertex between
glBegin and glEnd, or between two invocations of glVertex.
A major conceptual difference between the basic geometric types is whether or not they have
interiors.
Aside from the point type, all the other basic types will be defined either in terms of vertices
or by finite pieces of lines, called line segments
Line segments (GL_LINES) The line-segment type causes successive pairs of vertices to be
interpreted as the endpoints of individual segments. Because the interpretation is done on a
pair wise basis, successive segments usually are disconnected.
Polylines
(GL_LINE_STRIP) It is used if successive vertices (and line segments) are to be
connected. Many curves can be approximated via a suitable polyline.
GL_LINE_LOOP: If the polyline need to be closed the final vertex is located in the same
place as the first,
GL_LINE_LOOP can be used which will draw a line segment from the final vertex to the
first, thus creating a closed path.
Priyanka H V
Page 2
Computer Graphics
Polygon refers to an object that has a border that can be described by a line loop, but has an
interior.
curved surfaces.
The performance of graphics systems is measured by the number of polygons per second that
can be displayed.
Outer edges of a polygon can be defined easily by an ordered list of vertices. But if the
interior is not well defined, then the polygon may be rendered incorrectly.
Three properties will ensure that a polygon will be displayed correctly: It must be
Simple
Convex and
Flat.
Simple:
A 2-D polygon in which no pair of edges cross each other. They will have
well-defined interiors. Although the locations of the vertices determine
whether or not a polygon is simple, the cost of testing is sufficiently high that
most graphics systems require that the application program do any necessary testing.
Non-simple:
A 2-D polygon in which pair of edges cross each other. Graphics system must
handle by some means if a non-simple polygon is to be displayed and to
define an interior for a non-simple polygon.
Convex:
An object is convex if all points on the line segment between any two points inside the object, or
on its boundary, are inside the object regardless of the type of the object and its dimension
(whether 2-D or 3-D). Convex objects include triangles, tetrahedra, rectangles, circles, spheres,
and parallelepipeds.
Priyanka H V
Page 3
Computer Graphics
Polygons (GL_POLYGON):
Successive vertices define line segments, and a line segment connects the final vertex to the
first. The interior is filled according to the state of the relevant attributes.
In OpenGL, glPolygonMode function can be used to select edges instead of fill (the default).
In the triangle strip, each additional vertex is combined with the previous two vertices to define
a new triangle.
For the quad strip, two new vertices are combined with the previous two vertices to define a
new quadrilateral.
A triangle fan is based on one fixed point. The next two points determine the first triangle, and
subsequent triangles are formed from one new point, the previous point, and the first (fixed)
point.
2.2.3 Text
Fonts are families of type faces of a particular style, such as Times, Computer Modern, or
Helvetica.
Vertices are used to define line segments or curves that outline each character.
Advantage:
It can be defined to have all the detail of any other object, and, because it is defined in the
same way as are other graphical objects, it can be manipulated by standard
Defining a full 128- or 256-character stroke font, however, can be complex, and the font can take
up significant memory and processing time.
Priyanka H V
Page 4
Computer Graphics
The standard PostScript fonts are defined by polynomial curves, and they illustrate all the
advantages and disadvantages of stroke text.
Raster text:
Each block defines a single character by the pattern of 0 and 1 bits in the
block.
A raster character can be placed in the frame buffer rapidly by a bit-blocktransfer (bitblt) operation, which moves the block of bits using a single
instruction.
Size of raster characters can be increased only by replicating or duplicating pixels, a process
that gives larger characters a blocky appearance.
Limitations:
Other transformations of raster characters, such as rotation, may not make sense, because
the transformation may move the bits defining the character to locations that do not
OpenGL does not have a text primitive, because stroke and bitmap characters can be created
from other primitives. However, GLUT provides a few bitmap and stroke character sets that
are defined in software and are portable. E.g. A bitmap character of 8 x 13 pixels can be
produced by
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c)
Priyanka H V
Page 5
Computer Graphics
Sl.No Primitive
1
Point
2
displayed differently.
Attributes may be associated with, or bound to, primitives at various points in the modeling
2.3. Color
Color in computer graphics is based the three-color theory which says:
If two colors produce the same tristimulus values, then they are visually
indistinguishable. Two colors that match visually are known as metameric pairs; they have
the same tristimulus values.
An additive color model is used because it is more appropriate for CRT displays with the
primaries red, green, and blue. This model considers a color, as being formed from three primary
colors that are mixed to form the desired color. All colors can not be matched in this way. But
with a particular set of primaries say standard red, green, and blue, combined a color, close to the
desired one can be obtained.
The matching process can be represented abstractly as follows.
Matching Color i.e., C = T1R + T2G + T3B,
Priyanka H V
Page 6
Computer Graphics
were R, G, and B represent the three primaries Red, Green and Blue respectively.
T1, T2, T3 are the strengths, or intensities, of the three primaries, called the tristimulus values.
Thus relative to the particular set of primaries, the target color can be characterized by the triplet
(T1, T2, T3). Thus a continuous function is reduced to three numbers. Hence for a visual match,
only a color's tristimulus values are to be matched.
Subtractive color model : It suits for processes such as commercial printing and painting. The
primaries are usually the complementary colors: cyan, magenta, and yellow (CMY). RGB
additive system has a dual with a CMY subtractive system.
Color Gamut: The range of colors that can be produced on a given system with a given set of
primaries.
Color Cube:
In additive color model, a color is viewed as a point in a color solid, as shown below.
primaries on); red, green, and blue (one primary fully on); the pairs of primaries, cyan
(green and blue fully on), magenta (red and blue fully on), and yellow (red and green
fully on); and white (all primaries fully on). The principal diagonal of the cube connects
the origin (black) with white. All colors along this line have equal tristimulus values and
Color handling in a graphics system from the programmer's perspective - i.e. , through the
API.:
There are two different approaches.
2.3.1 RGB Color Model
In a three-primary-color, additive-color RGB system, there are conceptually separate frame
buffers for red, green, and blue images.
Each pixel has separate red, green, and blue components that correspond to locations in
memory.
Priyanka H V
Page 7
Computer Graphics
Programmers specify color components as numbers (using the color cube) between 0.0 and
1.0, where 1.0 denotes the maximum value of the corresponding primary, and 0.0 denotes a
zero value of that primary.
Graphic Function:
In OpenGL, to draw in red, the following function call is used:
glColor3f(1.0, 0.0, 0.0);
The execution of this function will set the present drawing color to red. Because the color is part
of the state, draw in red is continued until the color is changed.
The "3f" is used in a manner similar to the glVertex function: It conveys that, a three-color
(RGB) model is used, and that the values of the components are given as floats in C.
If an integer or byte type to specify a color value, the maximum value of the chosen type
corresponds to the primary fully on, and the minimum value corresponds to the primary fully off.
Advantages and Disadvantages:
RGB Model is used in lighting and shading. It is a more difficult model to support in
hardware, due to higher memory requirements, but modern systems support it more easily
now that memory is cheaper. It is also possible for a system to support true color in
software through the API, and to have the hardware use approximate techniques to
implement a display visually close to an RGB-color display.
Many systems have frame buffers that are limited in depth. Consider a frame buffer
having a spatial resolution of l280 x 1024, but each pixel is only 8 bits deep. The 8 bits
can be divided into smaller groups of bits to assign to red, green, and blue. Although this
technique is adequate in a few applications, it usually does not give enough flexibility in
color assignment, and breaking apart individual bytes into small groups of bits can affect
performance.
Four-color (RGBA) system. The fourth color (A) uses what is called the alpha channel, but is
stored in the frame buffer, as are the RGB values; it can be set with four-dimensional versions of
the color functions. (Chapter 9 covers various uses of the alpha channel, such as for creating fog
effects or for combining images). Here, alpha value is just used in the initialization of an
OpenGL program. The alpha value will be treated by OpenGL as an opacity or transparency
value. Transparency and opacity are complements of each other. An opaque object passes no
light through it; a transparent object passes all light. An object can range from fully transparent
to fully opaque.
To clear an area of the screen-a drawing window-in which output is displayed:
It is done
Page 8
Computer Graphics
Description:
Colors can be selected by interpreting the limited-depth pixels as indices, rather than as color
values.
These indices correspond to lines or entries in a table. Suppose that the frame buffer has k
bits per pixel.
Suppose that the colors can be displayed with an accuracy of m bits; that is, it is possible to
chose from 2m reds, 2m greens, and 2m blues. Hence, any of 23m colors can be produced on the
display, but the frame buffer can specify only 2k of them.
that selects a particular color out of the table. Setting and changing the entries in the color-lookup
table involves interacting with the window system.
GLUT allows to set the entries in a color table for each window through the function
glutSetColor(int color, GLfloat red, GLfloat blue, GLfloat green)
Priyanka H V
Page 9
Computer Graphics
2. Rendering color for the points by setting the color state variable to red through the
function call
glColor3f(1.0, 0.0, 0.0);
3. Size of rendered points: For 2 pixels wide, following call is used.
glPointSize(2.0);
2.4. Viewing
A fundamental concept that emerges from the synthetic-camera model is that, the
specification of the objects in the scene is completely independent of the specification of the
camera. Once both the scene and the camera have been specified, the computer system forms
2.4.1. 2-D viewing: is based on taking a rectangular area of 2-D world and transferring its
contents to the display.
The size of the window on the display, and where this window is placed on the display, is
independent decisions.
2-D graphics view is a special case of 3-D graphics. Hence the viewing rectangle is in the
plane z = 0 within 3-D viewing volume:
If viewing volume is not specified, OpenGL uses its
default, a 2 x 2 x 2 cube, with the origin in the center. In
terms of 2-D plane, the bottom-left corner is at
(-1.0, -1.0), and the upper-right corner is at (1.0, 1.0).
Orthographic projection
This simple orthographic projection takes a point (x, y, z) and projects it into the point (x, y,
0), as shown below:
Priyanka H V
Page 10
Computer Graphics
void glOrtho(Gldouble left, Gldouble right, Gldouble bottom, Gldouble top, Gldouble near,
GLdouble far)
In OpenGL, the camera starts off at the origin pointing in the negative z direction as below:
The orthographic projection "sees" only those
objects in the volume specified by viewing
volume.
The
orthographic
projection
also
The values of these matrices are part of the state of the system and remain in effect until
changed.
At any time, the state includes values for both of these matrices, which start off set to identity
matrices.
The usual sequence is to modify the initial identity matrix, by applying a sequence of
transformations.
There is only a single set of functions that can be applied to any type of matrix.
Matrix mode:
Setting the matrix mode selects the matrix to which the operations apply. It is a variable that is
set to one type of matrix and is also part of the state. The default matrix mode is to have
operations apply to the model-view matrix. So to alter the projection matrix, modes must be
switched.
This sequence defines a 500 x 500 viewing rectangle with the lower-left corner of the rectangle
at the origin of the 2-D system. It then switches the matrix mode back to model-view mode. In
Priyanka H V
Page 11
Computer Graphics
complex programs, it is always better to return to a given matrix mode, to avoid possible
confusion over the current mode. E.g. In this case mode returned to model-view mode.
2.5. Control Functions
The OpenGL Utility Toolkit (GLUT) is a library of functions that provides a simple interface
between the systems.
The application programs written using GLUT run under multiple window systems.
It initiates an interaction between the windowing system and OpenGL. It is used before
opening a window in the program.
The two arguments allow the user to pass command-line arguments, as in the standard C
main function, and are usually the same as in main.
2. glutCreateWindow(char *tit1e)
The window thus created has a default size, a position on the screen, and characteristics such
as use of RGB color. But GLUT functions are also available to specify these parameters
before to the window creation.
Page 12
Computer Graphics
glutIn1tW1ndowSize(480, 640);
glutInitWindowPosition(0, 0);
specifies a 480 x 640 window in the top-left corner of the display.
The aspect ratio of a rectangle is the ratio of the rectangle's width to its height.
The independence of the object, viewing, and workstation window specifications can cause
undesirable side effects if the aspect ratio of the viewing rectangle, specified by glOrtho, is
not the same as the aspect ratio of the window specified by glutInitWindowSize.
By ensuring that the clipping rectangle and display window have the same aspect ratio.
Using the concept of a viewport: It is the flexible way.
A viewport is a rectangular area of the display window. By default, it is the entire window,
but it can be set to any smaller size in pixels via the function
void glViewport(GLint x, GLint y, GLsizei w, GLsizei h)
where (x. y) is the lower-left corner of the viewport (measured relative to the lower-left corner of
the window), and w and h give the height and width, respectively.
The types are all integers specifying positions and distances in pixels.
The viewport is part of the state. Hence changing the viewport between successive
rendering/redisplay gives the effect of multiple viewports with different images in different
parts of the window.
Priyanka H V
Page 13
Computer Graphics
The screen may clear immediately before the output primitive is seen. Solution to this problem
includes:
Inserting a delay, such as via a standard function such as sleep(enoughtime).
By event-processing:say using the GLUT function
void glutMainLoop(void)
which causes the program to begin an event-processing loop.
If there are no events to process, the program will sit in a wait state, with the graphics
on the screen, until the program is terminated through some external means, such as
by hitting the "kill" key.
Display callback A function which sends Graphics to the screen. It is specified through the
GLUT function
void glutDisplayFunc(void (*func)(void))
Here, func is the name of the function that will be called whenever the windowing system
determines that the OpenGL window needs to be redisplayed.
One of these times include:
when the window is first opened. Thus if all graphics are put into this function (for our
non-interactive example), func will be executed once and the graphics (say gasket) will
be drawn.
when the window is moved from one location on the screen to another and
when a window in front of the OpenGL window is destroyed, making visible the whole
OpenGL window.
myinit( ) sets the OpenGL state variables dealing with viewing and attributes. These
parameters are preferred to set once, independently of the display function.
Every program further written will have the same structure as the gasket program.
Priyanka H V
Page 14
Computer Graphics
The main will also name the required callbacks and callback functions. Every program
must have a display callback, and most will have other callbacks to set up interaction.
The myinit function will set up programmers options, usually through OpenGL functions
on the GL and GLU libraries. Although these options could be set in main, it is probably
clearer to keep GLUT functions separate from OpenGL functions. Most of the graphics
output will be generated in the display callback, in most programs.
Following the second approach, faces of a tetrahedron are the four triangles determined
Priyanka H V
Page 15
Computer Graphics
From the viewer's position, only the triangle A is seen clearly, but
triangle B is blocked from the view, and triangle C is only partially
visible.
If the position of the viewer and the triangles are known, then the triangles must be drawn to
get the correct image.
Algorithms for ordering objects so that they are drawn correctly are called visible-surface
algorithms or hidden-surface-removal algorithms, depending on problem under
consideration.
z-buffer algorithm:
Because the algorithm stores information in the depth buffer, this buffer must be cleared
whenever the display is to be redrawn. Thus, clear procedure in the display function is
modified as follows:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Priyanka H V
Page 16