CG 18CS62 Module1
CG 18CS62 Module1
VISUALIZATION
(18CS62)
INTRODUCTION TO COMPUTER GRAPHICS
• Graphics are visual images or designs on some
surface, such as a wall, canvas, screen, paper, or
stone to inform, illustrate, or entertain.
• Computer graphics are pictures created using
computers.
• The phrase was coined in 1960, by computer
graphics researchers Verne Hudson and William
Fetter.
• Computer graphics is concerned with all aspects
of producing pictures or images using a computer.
IMPORTANCE OF COMPUTER GRAPHICS
• One of the most useful ways of presenting the processed
information by a computer system.
• We humans use visual system more extensively to perceive the
world as it is.
• Interaction with the computer is possible with the help of GUIs
(Graphical User Interfaces).
• Computer graphics provide a way of visualizing different type
of complex information which is easily understood by humans.
COURSE OBJECTIVES
• Explain hardware, software and OpenGL Graphics
Primitives.
• Illustrate interactive computer graphic using the OpenGL.
• Design and implementation of algorithms for 2D graphics
Primitives and attributes.
• Demonstrate Geometric transformations, viewing on both 2D
and 3D objects.
• Infer the representation of curves, surfaces, Color and
Illumination models.
OVERVIEW OF MODULES
MODULE DESCRIPTION
Module-1 Overview: Computer Graphics and OpenGL
• As the GKS and PHIGS packages were being developed, the graphics
workstations from Silicon Graphics, Inc. (SGI), became increasingly
popular. These workstations came with a set of routines called GL
(Graphics Library).
• The GL routines were designed for fast, real-time rendering, and soon this
package was being extended to other hardware systems. As a result,
OpenGL was developed as a hardware independent version of GL in the
early 1990s.
• This graphics package is now maintained and updated by the
OpenGL Architecture Review Board, which is a consortium of
representatives from many graphics companies and organizations.
• Graphics functions in any package are typically defined as a set of
specifications independent of any programming language.
• A language binding is then defined for a particular high-level
programming language.
• This binding gives the syntax for accessing the various graphics
functions from that language.
• The OpenGL bindings for the C and C++ languages are the same.
Other OpenGL bindings are also available, such as those for Java
and Python.
INTRODUCTION TO OPENGL
• Basic library of functions in OpenGL - graphics primitives,
attributes, geometric transformation, viewing transformation etc.
• OpenGL is hardware independent – many operations such as input
and output routines are not included in the basic library.
• Input and output routines and many additional functions are
available in auxiliary libraries that has been developed for OpenGL
programs.
Basic OpenGL Syntax
• Function names in the OpenGL basic library are prefixed with gl. Ex:
glBegin, glClear.
• Certain functions require arguments to be assigned a symbolic constant
specifying, a parameter name, a value for a parameter or a particular
mode.
• All such constants begin with uppercase letters GL.
• Component words within a constant name are written in capital letters,
and the underscore is used as a separator between all component
words in the name. Ex: GL_POLYGON
• To indicate a specific data type, OpenGL uses special built-in, data type
names such as GLshort, GLint, GLfloat, Gldouble
Related Libraries
• The OpenGL Utility (GLU) provides routines for setting up viewing
and projection matrices, describing complex objects with line and
polygon approximations and other complex tasks.
• Every OpenGL implementation includes the GLU library, and all
GLU function names start with the prefix glu.
• To create a graphics display using OpenGL, we first need to set up
a display window on our video screen.
• We cannot create the display window directly with the basic
OpenGL functions, since this library contains only device
independent graphics functions and the window- management
operations depend on the computer we are using.
• OpenGL Utility Toolkit (GLUT) provides a library of functions for
interacting with any screen-windowing system.
• The GLUT library functions are prefixed with glut, this library also
contains methods for describing and rendering quadric curves and
surfaces.
Header Files
• If we use GLUT to handle the window-managing operations, we do not
need to include gl.h and glu.h because GLUT ensures that these will be
included correctly.
• We can replace the header files for OpenGL and GLU with
#include<GL/glut.h>
• We could include gl.h and glu.h as well, but doing so would be
redundant and could affect program portability.
• In addition, we will often need to include header files that are required
by the C++ code
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
Display-Window Management using GLUT
• First step is to initialize GLUT. This initialization function could
process any command-line arguments.
• We perform the GLUT initialization with the statement:
glutInit (&argc, argv);
• We can state that a display window is to be created on the screen
with a given caption for a title bar using the function
glutCreate Window(“An example OpenGL program”);
• Then we need to specify what the display window is to contain.
• For this, we create a picture using OpenGL functions and pass the
picture definition to the GLUT routine glutDisplayFunc, which
assigns our picture to the display window.
glutDisplayFunc(linesegment) ;
• But the display window is not yet on the screen. We need one
more function to complete the window-processing operations.
glutMainLoop();
• This function must be the last one in our program.
• It displays the initial graphics and puts the program into an
infinite loop that checks for input from devices such as mouse or
keyboard.
• Although the display window will be in some default position and
size, we set these parameters using additional GLUT functions.
• We use glutInitWindowPosition function to give an initial location
for the top left corner of the display window.
• glutInitWindowPosition(50,100) specifies that the top-left corner
of the display window should be placed 50 pixels to the right of
the left edge of the screen and 100 pixels down from the top edge
of the screen.
• glutInitWindowSize function is used to set the initial pixel width
and height of the display window.
• We can set a number of other options for the display window, such as
buffering and choice of color modes, with the glutInitDisplayMode
function.
• Arguments for this routine are symbolic glut constants.
• Using RGB color values, we set the background color for the display
window to be white, with the OpenGL function
glClearColor(1.0, 1.0, 1.0, 0.0);
• The first three arguments in this function set each of the red, green and
blue component colors to the value 1.0.
• The fourth parameter in the glClearcolor function is called the alpha
value for the specified color.
• When we activate the OpenGL blending operations, alpha values can be
used to determine the resulting color of two overlapping objects.
• Alpha value 0 indicates totally transparent object, 1 indicates an opaque
object.
• Although the glClearColor assigns color to the display window, it
does not put it on the screen.
• To get the assigned window color displayed, we need to invoke the
following OpenGL function
glClear(GL_COLOR_BUFFER_BIT);
• GL_COLOR_BUFFER_BIT is an OpenGL symbolic constant
specifying that the bit values in the color buffer are to be set to the
values indicated in the glClearColor function.
Chapter -2
GRAPHICS OUTPUT
PRIMITIVES
Coordinate Reference Frames
• To describe a picture, we first decide upon a convenient Cartesian
coordinate system, called the world-coordinate reference frame,
which could be either two dimensional or three-dimensional.
• We then describe the objects in our picture by giving their geometric
specifications in terms of positions in world coordinates.
• For instance, we define a straight-line segment with two end point
positions, and a polygon is specified with a set of positions for its
vertices.
• These coordinate positions are stored in the scene description along
with other information about the objects, such as their color and their
coordinate extents, which are the minimum and maximum x, y, and z
values for each object.
• Objects are then displayed by passing the scene information to the
viewing routines, which identify visible surfaces and ultimately map
the objects to positions on the video monitor.
• The scan-conversion process stores information about the scene,
such as color values, at the appropriate locations in the frame buffer,
and the objects in the scene are displayed on the output device.
Screen Coordinates
• Locations on a video monitor are referenced in integer screen
coordinates, which correspond to the pixel positions in the frame
buffer.
• Pixel coordinate values give the scan line number (the y value) and
the column number (the x value along a scan line).
• Hardware processes, such as screen refreshing, typically address
pixel positions with respect to the top-left corner of the screen.
• Scan lines are then referenced from 0, at the top of the screen, to
some integer value, ymax, at the bottom of the screen, and pixel
positions along each scan line are numbered from 0 to xmax, left to
right.
• However, with software commands, we can set up any convenient
reference frame for screen positions.
• The coordinate values we use to describe the geometry of a scene are
then converted by the viewing routines to integer pixel positions
within the frame buffer.
• Scan-line algorithms for the graphics primitives use the defining
coordinate descriptions to determine the locations of pixels that are
to be displayed.
• For example, given the endpoint coordinates for a line segment, a
display algorithm must calculate the positions for those pixels that lie
along the line path between the endpoints.
• We assume that each integer screen position references the center of
a pixel area.
• Once pixel positions have been identified for an object, the
appropriate color values must be stored in the frame buffer.
• For this purpose, we will assume that we have a low-level procedure
of the form setPixel (x, y);
• This procedure stores the current color setting into the frame buffer
at integer position(x, y), relative to the selected position of the
screen-coordinate origin
• We sometimes also will want to be able to retrieve the current frame-
buffer setting for a pixel location.
• So we will assume that we have the following low-level function for
obtaining a frame-buffer color value: getPixel (x, y, color);
• In this function, parameter color receives an integer value
corresponding to the combined red, green, and blue (RGB) bit codes
stored for the specified pixel at position (x, y).
• Although we only specify color values at (x, y) positions for a two
dimensional picture, additional screen-coordinate information is
needed for three-dimensional scenes.
• In this case, screen coordinates are stored as three dimensional
values, where the third dimension references the depth of object
positions relative to a viewing position.
Absolute and Relative Coordinate
Specifications
• Absolute coordinate values - the values specified are the actual
positions within the coordinate system in use.
• Relative coordinate values - we can specify a coordinate position
as an offset from the last position that was referenced (called the
current position).
• Useful for various graphics applications, such as producing
drawings with pen plotters, artist’s drawing and painting systems,
and graphics packages for publishing and printing applications.
• For example, if location (3, 8) is the last position that has been
referenced in an application program, a relative coordinate
specification of (2,−1) corresponds to an absolute position of (5, 7).
• Options can be provided in a graphics system to allow the
specification of locations using either relative or absolute
coordinates.
Specifying A 2D World-Coordinate Reference Frame
in OpenGL
• The gluOrtho2D command is a function we can use to set up any two
dimensional Cartesian reference frame.
• The arguments for this function are the four values defining the x and y
coordinate limits for the picture we want to display.
• Since the gluOrtho2D function specifies an orthogonal projection, we
also need to be sure that the coordinate values are placed in the
OpenGL projection matrix.
• In addition, we could assign the identity matrix as the projection matrix
before defining the world-coordinate range to ensure that the
coordinate values were not accumulated with any values we may have
previously set for the projection matrix.
• We can define the coordinate frame for the screen display window
with the following statements:
glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
gluOrtho2D (xmin, xmax, ymin, ymax);
• The display window will then be referenced by coordinates(xmin,
ymin) at the lower-left corner and by coordinates(xmax, ymax) at
the upper-right corner.
• If the coordinate extents of a primitive are within the coordinate
range of the display window, all of the primitive will be displayed.
• Otherwise, only those parts of the primitive within the display-
window coordinate limits will be shown.
• When we set up the geometry describing a picture, all positions for
the OpenGL primitives must be given in absolute coordinates, with
respect to the reference frame defined in the gluOrtho2D function.
OpenGL Point Functions
• To specify the geometry of a point, we simply give a coordinate
position in the world reference frame.
• Then this coordinate position, along with other geometric
descriptions we may have in our scene, is passed to the viewing
routines.
• Unless we specify other attribute values, OpenGL primitives are
displayed with a default size and color.
• The default color for primitives is white, and the default point size
is equal to the size of a single screen pixel.
• We use the following OpenGL function to state the coordinate values for a
single position:
glVertex* ( );
• The asterisk (*) indicates that suffix codes are required for this function
which are used to identify the spatial dimension, the numerical data type
to be used for the coordinate values, and a possible vector form for the
coordinate specification.
• Calls to glVertex functions must be placed between a glBegin function and a
glEnd function.
• The argument of the glBegin function is used to identify the kind of output
primitive that is to be displayed, and glEnd takes no arguments.
• For point plotting, the argument of the glBegin function is the symbolic
constant GL_ POINTS.
• Coordinate positions in OpenGL can be given in two, three, or four
dimensions.
• We use a suffix value of 2, 3 on the glVertex function to indicate the
dimensionality of a coordinate position.
• Because OpenGL treats two-dimensions as a special case of three
dimensions, any (x, y) coordinate specification is equivalent to a
three-dimensional specification of (x, y, 0).
• To state which data type is to be used for the numerical value
specifications of the coordinates we use second suffix code on the
glVertex function.
• Suffix codes for specifying a numerical data type are i (integer), s
(short), f (float), and d (double).
• Coordinate values can be listed explicitly in the glVertex function, or a
single argument can be used that references a coordinate position as
an array.
• If we use an array specification for a coordinate position, we need to
append v (for “vector”) as a third suffix code.
OpenGL Line Functions
• Graphics packages typically provide a function for specifying one
or more straight-line segments, where each line segment is
defined by two endpoint coordinate positions.
• In OpenGL, we select a single end point coordinate position using
the glVertexfunction, just as we did for a point position.
• And we enclose a list of glVertexfunctions between the glBegin /
glEnd pair.
• There are three symbolic constants in OpenGL that we can use to
specify how a list of end point positions should be connected to form
a set of straight-line segments.
• By default, each symbolic constant displays solid, white lines.
• A set of straight-line segments between each successive pair of end
points in a list is generated using the primitive line constant GL_
LINES.
• With the OpenGL primitive constant GL_LINE_STRIP, we obtain a
polyline.
• In this case, the display is a sequence of connected line segments
between the first end point in the list and the last end point.
• The first line segment in the polyline is displayed between the first
endpoint and the second endpoint; the second line segment is
between the second and third end points; and so forth, up to the last
line endpoint.
• The third OpenGL line primitive is GL_LINE_LOOP, which produces
a closed polyline.
• Lines are drawn as with GL _LINE_STRIP, but an additional line is
drawn to connect the last coordinate position and the first
coordinate position.
LINE-DRAWING ALGORITHMS
• A straight-line segment in a scene is defined by coordinate
positions for the endpoints of the segment.
• To display the line on a raster monitor, the graphics system must
first project the endpoints to integer screen coordinates and
determine the nearest pixel positions along the line path between
the two endpoints.
• Next the line color is loaded into the frame buffer at the
corresponding pixel coordinates.
• A computed line positions of (10.48,20.51) is converted to pixel
position (10,21).
• This rounding of coordinates values to integers causes all but
horizontal and vertical lines to be displayed with a stair-step
appearance ("the jaggies").
Line Equations
• Determine the pixel position along a straight-line path from the
geometric properties of the line.
• The Cartesian slope-intercept equation for a straight line is
y = m . x + b (1)
with m as the slope of the line and b as the y intercept.
• Given the two endpoints of a line segment at positions (x0,y0) and
(xend ,yend), we can determine values for the slope m and y intercept b
with the following calculations:
• For any given x interval δx along a line, we can compute the
corresponding y and interval δy from equation 2 as
δy = m. δx (4)
• Similarly, we can obtain the x interval δx corresponding to a specified
δy as
δx = δy/m (5)
• These equations form the basis for determining deflection voltages in
analog displays, such as vector-scan system, where arbitrarily small
changes in deflection voltage are possible.
• On raster systems, lines are plotted with pixels, and the step sizes in
the horizontal and vertical directions are constrained by pixel
separations.
• That is we must "sample" a line at discrete positions and determine
the nearest pixel to the line at each sample position.
• The scan-conversion process for straight lines is illustrated in fig.
with discrete sample positions along the x -axis.
Straight –line segment
with five sampling
positions along the x
axis between x0 and
xend
DDA Algorithm
• Digital Differential Analyzer is a scan–conversion line algorithm
based on calculating either δx or δy using eq 4 and 5.
• A line is sampled at unit intervals in one coordinate and the
corresponding integer values nearest the line path are determined
for the other coordinate.
• If the slope is less than or equal to 1, we sample at unit x intervals (δx
= 1) and compute successive y values as
Y k+1 = yk +m (6)
• Subscript k takes integer values starting from 0, for the first point and
increases by 1 until the final endpoint is reached.
• Since m can be any real number between 0.0 and 1.0, each calculated
y value must be rounded to the nearest integer corresponding to a
screen pixel position in the x column we are processing.
• For lines with positive slope greater than 1.0, we reverse the roles of x
and y.
• We sample at unit y intervals (δy = 1) and calculate the consecutive x
values as
x k+1 = xk + 1/m (7)
• In this case, each computed x value is rounded to the nearest pixel
position along the current y scan line.
• Equation (6) and (7) are based on the assumption that lines are to be
processed from the left endpoint to the right endpoint.
• If this processing is reversed, so that the starting endpoint is at the
right, then either we have δx = -1 and
y k+1 = yk – m (8)
• Or when the slope is greater than 1 we have δy = -1 with
x k+1 = xk - 1/m (9)
• This algorithm accepts as input two integer screen positions for the
endpoints of a line segment.
• Horizontal and vertical differences between the endpoint positions are
assigned to parameters dx and dy. The difference with the greater
magnitude determines the value of parameter steps.
• We draw the starting pixel at position (x0, y0), and then draw the
remaining pixels iteratively, adjusting x and y at each step to obtain the
next pixel position before drawing it.
• If the magnitude of dx is greater than the magnitude of dy and x0 is less
than xEnd, the values for the increments in the x and y directions are 1 and
m, respectively.
• If the greater change is in the x direction, but x0 is greater than xEnd, then
the decrements −1 and −m are used to generate each new point on the line.
• Otherwise, we use a unit increment (or decrement) in the y direction and
an x increment (or decrement) of 1/m
#include <stdlib.h> xIncrement = float (dx) / float (steps);
#include <math.h> yIncrement = float (dy) / float (steps);
inline int round (const float a) { return setPixel (round (x), round (y));
int (a + 0.5); } for (k = 0; k < steps; k++)
void lineDDA (int x0, int y0, int xEnd, int {
yEnd)
{ x += xIncrement;
int dx = xEnd - x0, dy = yEnd - y0, steps, k; y += yIncrement;
float xIncrement, yIncrement, x = x0, y = setPixel (round (x), round (y));
y0; }
if (fabs (dx) > fabs (dy)) }
steps = fabs (dx);
else
steps = fabs (dy);
• Faster method for calculating pixel position than one that directly
implements eq 1 .
• Eliminates the multiplication by making use of appropriate
increments applied in the x or y directions to step from one pixel
position to another along the line path.
• Round off error in successive additions of the floating point
increment, can cause the calculated pixel positions to drift away from
the true line path for long line segments.
• Rounding operations and floating point arithmetic in this procedure
are still time consuming.
Consider two points (2,3) and (12,8) solve by using DDA
Algorithm.
Bresenham’s Line Drawing Algorithm
• A decision parameter pk for the kth step in the line algorithm can be
obtained by rearranging Equation 13 so that it involves only integer
calculations.
• We accomplish this by substituting m = Δy/Δx, where Δy and Δx are the
vertical and horizontal separations of the endpoint positions, and
defining the decision parameter as
• The sign of pk is the same as the sign of dlower −dupper, because Δx
>0 for our example.
• Parameter c is constant and has the value 2Δy + Δx(2b − 1), which is
independent of the pixel position and will be eliminated in the
recursive calculations for pk .
• If the pixel at yk is “closer” to the line path than the pixel at yk + 1
(that is, dlower < dupper), then decision parameter pk is negative.
• In that case, we plot the lower pixel; otherwise, we plot the upper
pixel.
• Coordinate changes along the line occur in unit steps in either the x
and y directions.
• We can obtain the values of successive decision parameters using
incremental integer calculations.
• At step k+1, the decision parameter is evaluated from (14) as
• Subtracting (14) from the preceding equation, we have,
• However xk+1=xk+1
• Any point (x,y) on the boundary of the circle with radius r satisfies
the equation fcirc(x,y)=0.
• If the point is in the interior of the circle, the circle function is
negative.
• If the point is outside the circle, the function is positive.
• The figure shows the midpoint between the two candidate pixels at
sampling position xk+1 .
• Assuming that we have just plotted the pixel at (xk, yk), we next
need to determine whether the pixel at (xk +1, yk ) or the one at
position (xk +1, yk -1) is closer to the circle.
• Our decision parameter is the circle function (29) evaluated at the mid
point between these two pixels:
• If pk<0, this midpoint is inside the circle and pixel on scan line yk is
closer to the circle boundary.
• Otherwise the mid position is outside or on the circle boundary, and
we select the pixel on scan line yk -1.
• Successive decision parameters are obtained using incremental
calculations.
• The initial decision parameter is obtained by evaluating the circle
function at the start position (x0 , y0 )= (0 ,r).