CG Module 1 Fully Updated 18
CG Module 1 Fully Updated 18
(18CS62)
• Entertainment
• Image Processing CMOS Circuit Design
Contd…
• Graphical User Interfaces
Such animations are often used to train heavy-equipment operators, analyze the
effectiveness of various cabin configuration and control placements, users can
interact with the objects in a 3D scene.
Contd…
• Graphical User Interfaces
Computer generated models of physical, financial, political, social, economic, and other
systems are often used as educational aids. Eg: color-coded diagram in operation of a
nuclear reactor, aircraft, naval, spacecraft simulators
• Graphs and charts
• Computer-aided design
• Virtual-Reality Environments
• Scientific visualization
Education and Training Flight Simulation Flight Simulation
• Computer art
• Entertainment
• Image Processing
• Graphical User Interfaces
Driving Simulation
Graphics are used in Fine Art, Commercial Art applications and Mathematical Art. The artist uses a
combination of 3D modeling packages, texture mapping, drawing programs and CAD software.
Mathematical Art is produced using mathematical functions and fractal procedures. Pen plotters create
Automatic Art.
Entertainment
• Image Processing
• Graphical User Interfaces
Image processing applies techniques to modify or interpret existing pictures (photographs, TV
scans).to apply image processing methods, the image must be digitized first. Medical
applications also make extensive use of image processing techniques for picture enhancement,
simulations of surgical operations. It used to improve picture quality, analyze or recognize
visual• patterns.
Graphs and charts
• Computer-aided design
• Virtual-Reality Environments
• Scientific visualization
• Education and Training
• Computer art
• Entertainment
Image Processing
• Graphical User Interfaces
User is provided with point and click facility to select menu items, icons and objects on the
screen. User can manage multiple windows simultaneously. Word-processing spreadsheets and
desktop-publishing programs are typical applications that take advantage of such user
interface techniques.
• Graphs and charts
• Computer-aided design
• Virtual-Reality Environments
• Scientific visualization
• Education and Training
• Computer art
• Entertainment
• Image Processing
Graphical User Interfaces
Basic Graphics System
Output device
Input devices
Image formed in FB
From model to image
Graphics pipeline
Cylinder:
x2 y2 r 2
0 zh
Geometric modeling Local or modeling coordinates
From model to image
World coordinates
From model to image
Look at cylinders,
project on virtual screen
Viewing coordinates
Visible surfaces, shading
From model to image
1
Display:
0
1024
Display on screen:
:
JMP
display buffer
Raster Display (since 1970s)
• Raster system consists of display processor (input, refreshing, scan converting),
video controller, buffer memory (frame buffer), CRT.
• The refresh buffer stores the display primitives(lines char and solid shaded or
patterned areas), rather than display list or display program.
• Raster: set of horizontal raster lines each a row of individual pixels and thus
stored as a matrix of pixels representing entire screen.
• Video controller reads these pixel contents to produce the actual image on the
screen, one raster line at a time, from top to bottom and then back to top.
• need refresh the raster display (e.g., 60Hz)
Raster Display
Host Computer
Display Interaction
cmds data
Display Processor
Lucy
Video Controller Lucy
glVertex3f(x,y,z)
glVertex3fv(p)
p is a pointer to an array
OpenGL #defines
• Most constants are defined in the include files gl.h, glu.h and
glut.h
• Note #include <glut.h> should automatically include the others
• Examples
• glBegin(GL_PLOYGON)
• glClear(GL_COLOR_BUFFER_BIT)
• Include files also define OpenGL data types: Glfloat, Gldouble,….
Program Structure
• Most OpenGL programs have a similar structure that consists of the following functions
• main():
• defines the callback functions
• opens one or more windows with the required properties
• enters event loop (last executable statement)
• init(): sets the state variables
• viewing
• Attributes
• callbacks
• Display function
• Input and window functions
Header Files
• In all graphics programs, we will need to include the header file for the
OpenGL core library.
• #include <GL/glut.h>//GL in windows
• Step 1: initialization of GLUT
• We are using the OpenGL Utility Toolkit, our first step is to initialize
GLUT. This initialization function could also process any command line
arguments, but we will not need to use these parameters for our first
example programs. We perform the GLUT initialization with the
statement
• glutInit (&argc, argv);
• Step 2: title
• We can state that a display window is to be created on the screen with a
given caption for the title bar. This is accomplished with the function
glutCreateWindow ("An Example OpenGL Program");
• where the single argument for this function can be any character string
that we want to use for the display-window title.
• Step 3: Specification of the display window
• 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.
• Example: suppose we have the OpenGL code for describing a line segment in a
procedure called lineSegment. Then the following function call passes the
line-segment description to the display window: glutDisplayFunc
(lineSegment);
• Step 4: one more GLUT function
• But the display window is not yet on the screen. We need one more GLUT
function to complete the window-processing operations. After execution
of the following statement, all display windows that we have created,
including their graphic content, are now activated: 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 a mouse or keyboard.
• Step 5: these parameters using
additional GLUT functions
• Although the display window that we
created will be in some default location
and size, we can set these parameters
using additional GLUT functions.
• GLUT Function 1: ➔ We use the
glutInitWindowPosition function to give
an initial location for the upper left corner
of the display window.
• ➔ This position is specified in integer
screen coordinates, whose origin is at the
upper-left corner of the screen.
• GLUT Function 2: After the display window is on the screen, we can
reposition and resize it.
• GLUT Function 3: ➔ We can also set a number of other options for the
display window, such as buffering and a choice of color modes, with the
glutInitDisplayMode function.
• ➔ Arguments for this routine are assigned symbolic GLUT constants.
• ➔ Example: the following command specifies that a single refresh buffer is to
be used for the display window and that we want to use the color mode which
uses red, green, and blue (RGB) components to select color values:
• glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
• ➔ The values of the constants passed to this function are combined using a
logical or operation.
A Complete OpenGL Program ➔ There are still a few
more tasks to perform before we have all the parts that
we need for a complete program.
• Step 1: to set background color
• ➔ For the display window, we can choose a background color.
• ➔ 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 the red, green, and blue component colors
to the value 1.0, giving us a white background color for the display window. ➔ If, instead
of 1.0, we set each of the component colors to 0.0, we would get a black background.
• ➔ The fourth parameter in the glClearColor function is called the alpha value for the
specified color. One use for the alpha value is as a “blending” parameter.
• Step 2: to set window color ➔ To get the assigned window color displayed, we
need to invoke the following OpenGL function:
• glClear (GL_COLOR_BUFFER_BIT); ➔ The argument GL COLOR BUFFER
BIT is an OpenGL symbolic constant specifying that it is the bit values in the color
buffer (refresh buffer) that are to be set to the values indicated in the glClearColor
function. (OpenGL has several different kinds of buffers that can be manipulated.
• Step 3: to set color to object ➔ In addition to setting the background color for the
display window, we can choose a variety of color schemes for the objects we want
to display in a scene.
• ➔ For our initial programming example, we will simply set the object color to be a
dark green
• glColor3f (0.0, 0.4, 0.2);
• ➔ We can set the projection type (mode) and other viewing parameters
that we need with the following two functions:
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
➔ This specifies that an orthogonal projection is to be used to map the
contents of a twodimensional rectangular area of world coordinates to the
screen, and that the xcoordinate values within this rectangle range from 0.0
to 200.0 with y-coordinate values ranging from 0.0 to 150.0.
• ➔ Finally, we need to call the appropriate OpenGL routines to create our
line segment.
• ➔ The following code defines a two-dimensional, straight-line segment
with integer,
• ➔ Cartesian endpoint coordinates (180, 15) and (10, 145).
• glBegin (GL_LINES);
• glVertex2i (180, 15);
• glVertex2i (10, 145);
• glEnd ( );
• ➔ Now we are ready to put all the pieces together:
OpenGL Primitives
GL_POINTS GL_POLYGON
GL_LINES GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLES
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
Attributes
• Attributes are part of the OpenGL state and determine the appearance of objects
• Color (points, lines, polygons)
• Size and width (points, lines)
• Stipple pattern (lines, polygons)
• Polygon mode
• Display as filled: solid color or stipple pattern
• Display edges
• Display vertices
Graphics Pipelines
• Graphics processes generally execute sequentially
• Typical ‘pipeline’ model
• There are two ‘graphics’ pipelines
• The Geometry or 3D pipeline
• The Imaging or 2D pipeline
7/5/23
Narendra V G CSE MIT 60
10:13:49 AM
2D Graphics Pipeline
Clipping
Object window to
Object Applying
subset viewport
World Coordinates world window
mapping
Object
Display Rasterization
Screen coordinates
Rasterization (Scan Conversion)
6
5
? Which intermediate
4
pixels to turn on?
3
2
1
0 1 2 3 4 5 6 7 8 9 10 11 12
Line Drawing Algorithm
• Slope-intercept line equation
• y = mx + b Equation of Line where m is slope & b is intercept
• Given two end points (x0,y0), (x1, y1), how to compute m and b?
dy y1 y 0 b y 0 m * x0
m
dx x1 x0
(x1,y1)
dy
(x0,y0)
dx
Line Drawing Algorithm
• Numerical example of finding slope m:
• (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
By Ay 96 41 55
m 0.5392
Bx Ax 125 23 102
Digital Differential Analyzer (DDA): Line
Drawing Algorithm
Walk through the line, starting at (x0,y0)
x=x+1 y=y+1*m
Until x == x1
(x0, y0)
DDA Line Drawing Algorithm (Case b: m >
1)
1 x = x0 y = y0
x k 1 xk (x1,y1)
m Illuminate pixel (round(x), y)
y = y0 + 1 x = x0 + 1 * 1/m
y=y+1 x = x + 1 /m
(x0,y0) Until y == y1
DDA Line Drawing Algorithm Pseudocode
compute m;
if m < 1:
{
float y = y0; // initial value
for(int x = x0;x <= x1; x++, y += m)
setPixel(x, round(y));
}
else // m > 1 or m = 1
{
float x = x0; // initial value
for(int y = y0;y <= y1; y++, x += 1/m)
setPixel(round(x), y);
} Note: setPixel(x, y) writes current color into pixel in column x and row y in
frame buffer
Draw a line from (1,1) to (8,7)
Assignment
DDA Line Drawing Algorithm
Drawbacks
• DDA is the simplest line drawing algorithm
• Not very efficient
• Round operation is expensive
• Optimized algorithms typically used.
• Integer DDA
• Eg. Bresenham algorithm (Hill, 10.4.1)
• Bresenham algorithm
• Incremental algorithm: current value uses previous value
• Integers only: avoid floating point arithmetic
• Several versions of algorithm: we’ll describe midpoint version of
algorithm
Bresenham’s Line-Drawing Algorithm
• Accurate and efficient
• Uses only incremental integer calculations
The method is described for a line segment with a positive slope less
than one
The method generalizes to line segments of other slopes by
considering the symmetry between the various octants and quadrants
of the xy plane
Bresenham’s Line-Drawing Algorithm
11
10
10 11 12 13
Bresenham’s Line-Drawing Algorithm
• dupper=(yk+1)-y
xk+1
= yk+1 -m(xk + 1)-b
• dlower- dupper= 2m(xk + 1)-2yk+2b-1
• Rearrange it to have integer calculations:
m=Δy/Δx
Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c
The Decision Parameter
Decision parameter: pk= Δx(dlower- dupper)=2Δy.xk - 2Δx. yk + c
1. Input the twoline endpoints and store the left endpoint in (x0 ,y0).
2. Load (x0 ,y0) into the frame buffer; that is, plot the first point.
3. Calculate constants Δx, Δy, 2Δy, and 2Δy - 2Δx, and obtain the starting value for the decision
parameter as
p0=2 Δy - Δx
4. At each xk along the line, starting at k = 0, perform the following test:
If pk < 0, the next point to plot is (xk+1, yk) and
pk+1=pk + 2Δy
Otherwise, the next point to plot is (xk+1, yk+1) and
pk+1=pk + 2Δy - 2Δx
5. Repeat step 4 Δx -1 times.
Trivial Situations: Do not need Bresenham
• Decision variable d is x 2 + y 2 – r 2
• Implicit formulation F(x,y) = x 2 + y 2 – r 2
SE
F (M) < 0 E
E
ideal
curve
M
SE
As in the Bresenham line algorithm we use a decision
variable to direct the selection of pixels.
Use the implicit form of the circle equation
p = F (M ) = x 2 + y 2 – r 2
Candidate pixels are () and (- 1) Midpoint coordinates are (-
1/2)
Assuming we have just plotted point at (xk,yk) we determine whether move E or SE by
evaluating the circle function at the midpoint between the two candidate pixel positions
1
𝑝 𝑘=𝐹 𝑐𝑖𝑟𝑐 (𝑥𝑘 +1, 𝑦 𝑘 − )
2
pk is the decision variable
if pk <0 the midpoint is inside the circle
Thus the pixel above the midpoint is closer to the ideal circle, and we select pixel on
scan line yk. i.e. Go E
If pk >0 the midpoint is outside the circle.
Thus the pixel below the midpoint is closer to the ideal circle, and we select pixel
on scan line yk-1. i.e. Go SE
1
𝑝 𝑘+1=𝐹 𝑐𝑖𝑟𝑐 (𝑥 𝑘+1+1, 𝑦 𝑘+1 − )
2
recursive definition for successive decision parameter values p
( )
x0 = 0, y0 = r
Initial decision variable found by evaluating circle function at first midpoint test
position
p0 Fcirc (1, r 1 )
2
1 (r 1 ) 2 r 2
2
5
r
4
For integer radius r p0 can be rounded to p0 =1-r
since all increments are integer.
• r=10
Let us see one
example
For Circle drawing keep this table handy.
• End of Module 1