CG Unit1
CG Unit1
CG Unit1
CONVERSION ALGORITHMS
1
CONTENT OF UNIT 1
Introduction, graphics primitives - pixel, resolution, aspect
ratio, frame buffer. Display devices, applications of
computer graphics.
Introduction to OpenGL - OpenGL architecture, primitives
and attributes, simple modelling and rendering of two-
and three-dimensional geometric objects, GLUT,
interaction, events and call-backs picking. (Simple
Interaction with the Mouse and Keyboard)
Scan conversion: Line drawing algorithms: Digital
Differential Analyzer (DDA), Bresenham. Circle drawing
algorithms: DDA, Bresenham, and Midpoint.
Computer Graphics
⚫ The computer is an information processing machine. It is a tool for
storing, manipulating and correlating data.
⚫ There are many ways to communicate the processed information to the
user.
⚫ The computer graphics is one of the most effective and commonly
used ways to communicate the processed information to the user.
⚫ It displays the information in the form of graphics objects such as
pictures, charts, graphs and diagrams instead of simple text.
⚫ Thus we can say that computer graphics makes it possible to express data
in pictorial form.
⚫ The picture or graphics object may be an engineering drawing, business
graphs, architectural structures, a single frame from an animated movie
or a machine parts illustrated for a service manual.
Advantages of Computer Graphics
⚫ Ahigh quality graphics displays of personal computer provide one
of the most natural means of communicating with a computer.
⚫ It has an ability to show moving pictures, and thus it is possible to
produce animations with computer graphics.
⚫ With computer graphics use can also control the animation by
adjusting the speed, the portion of the total scene in view, the
geometric relationship of the objects in the scene to one another,
the amount of detail shown and so on.
⚫ The computer graphics also provides facility called update
dynamics.
⚫ With update dynamics it is possible to change the shape, color
or other properties of the objects being viewed.
Applications of Computer Graphics
Monochrome
Color CRT
CRT
With OpenGL any commands that you execute are executed immediately.
That is, when you tell the program to draw something, it does it right away. You
also have the option of putting commands into display lists. A display list is a not-
editable list of OpenGL commands stored for later execution. You can execute the
same display list more than once. For example, you can use display lists to
redraw the graphics whenever the user resizes the window. You can use a display
list to draw the same shape more than once if it repeats as an element of the
picture.
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ization ment
opns. &
ands opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
buffer
OpenGL Process geometric primitives -
points, line segments, and
Operation polygons as vertices and are
transformed, lit, and clipped
Display to the viewport in preparation
Lists for the next stage.
Per- Per-
Comm- Eval- Raster- frag-
ands Vertex
uator opns. & ization ment
primitive opns.
assembly
Texture
Pixel memory
opns.
Frame
buffer
OpenGL Produces a series of
frame buffer addresses
Operation and associated values
using a two-dimensional
Display description of a point,
Lists line segment, or polygon
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ization ment
ands opns &
primitive opns.
assembly
Texture
Pixel memory
opns.
Frame
buffer
OpenGL Z-buffering, and blending
of incoming pixel colors
Operation with stored colors, and
masking and other logical
Display
operations on pixel values
Lists
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ization ment
opns &
ands opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
buffer
OpenGL Input data can be in
the form of pixels
Operation (image for texture
mapping) is processed
Display
in the pixel operations
Lists
stage.
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ops & ization ment
ands primitive opns
assembly
Texture
Pixel memory
opns
Frame
buffer
⚫ Commands: what is to be drawn? How it is to be drawn?
⚫ Display list: Can accumulate some commands in a display
list for processing at alater time (Batch mode). Or can
proceed immediately through the pipeline.
⚫ Evaluators: Provides an efficient means for approximating
curve and surface geometry by evaluating polynomial
commands of input values.
⚫ Per- Vertex opns. & Primitive assembly: Process
geometric primitives -points, line segments, and polygons as
vertices and are transformed, lit, and clipped to the viewport
in preparation for the next stage.
⚫ Rasterization: Produces a series of frame buffer addresses
and associated values using a two-dimensional description of
a point, line segment, or polygon.
⚫ Perfragment oprations:Z-buffering, and blending of
incoming pixel colors with stored colors, and masking and
other logical operations on pixel values.
⚫ Pixel oprations:Input data can be in the form of pixels
(image for texture mapping) is processed in the pixel
operations stage.
Abstractions
• Windowing toolkit (key, mouse
GLUT handler, window events)
• Viewing –perspective/orthographic
GLU • Image scaling, polygon tessellation
• Sphere, cylinders, quadratic surfaces
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();
glBegin(GL_POINTS);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();
The numbers indicate the order in which the vertices have been specified.
Note that for the GL_LINES primitive only every second vertex causes a line
segment to be drawn. Similarly, for the GL_TRIANGLES primitive, every third vertex
causes a triangle to be drawn. Note that for the GL_TRIANGLE_STRIP and
GL_TRIANGLE_FAN primitives, a new triangle is produced for every additional
vertex. All of the closed primitives shown below are solid-filled, with the exception
of GL_LINE_LOOP, which only draws lines connecting the vertices.
The following code fragment illustrates an example of how the
primitive type is specified and how the sequence of vertices are
passed to OpenGL. It assumes that a window has already been opened
and that an appropriate 2D coordinate system has already been
established.
glBegin(GL_POLYGON);
glVertex3fv(p1);
glVertex3fv(p2);
glVertex3fv(p3);
glVertex3fv(p4);
glEnd();
3D Viewing Pipeline
object world clip
coordinates coordinates coordinates
VM P
v1,v2,…, vN clip
modelview projection
matrix matrix
normalized device window
coordinates coordinates
VP
perspective
viewport matrix
division
From F. S. Hill Jr., Computer Graphics using OpenGL
OpenGL functions for setting up transformations
modelling transformation
(modelview matrix)
glTranslatef()
glRotatef()
glScalef()
viewing transformation
(modelview matrix)
gluLookAt()
projection transformation
(projection matrix)
glFrustum()
gluPerspective()
glOrtho()
gluOrtho2D()
viewing transformation
glViewport()
Structure of a GLUT Program
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Interactive rotating
cube"); // with size & position
glutDisplayFunc(display);
// display callback, routines for drawing
glutKeyboardFunc(myKeyHandler);
// keyboard callback
glutMouseFunc(myMouseClickHandler);
// mouse callback
glutMotionFunc(myMouseMotionHandler);
// mouse move callback
init();
glutMainLoop();
}
void display() {...}
void myKeyHandler( unsigned char key, int x,
int y) {...}
void myMouseClickHandler( int button, int
state, int x, int y ) {...}
void myMouseMotionHandler( int x, int y) {...}
glutInitDisplaymode()
These calls assign an initial position, size, and name to the window
and create the window itself.
glClearColor() sets the colour to be used when clearing the window. The
remaining calls are used to define the type of camera projection. In this
case, an orthographic projection is specified using a call to
glOrtho(x1,x2,y1,y2,z1,z2). This defines the field of view of the camera, in
this case 0<=x<=10, 0<=y<=10, -1<=z<=1.
glutDisplayFunc(display), glutMainLoop()
This provides the name of the function you would like to have
called whenever glut thinks the window needs to be redrawn. Thus, when
the window is first created and whenever the window is uncovered or
moved, the user-defined display() function will be called.
gluOrtho2D(
0.0, // left
screenWidth, // right
0.0, // bottom
screenHeight); // top
}
Drawing in 2D
GL_LINES
glBegin(GL_POINTS); glVertex2d(x1, y1);
GL_LINE_STRIP
glVertex2d(x2, y2); GL_LINE_LOOP
. GL_POLYGON
.
.
glVertex2d(xn, yn); glEnd();
Drawing a square in OpenGL
glutInit()
Following the initial print statement, the glutInit() call initializes the
GLUT library and also processes any command line options related to
glut. These command line options are window-system dependent.
display()
xi + 1 := xi + 1
yi + 1 := yi + m 0 m 1
or Why?
yi + 1 := yi + 1
1
xi + 1 := xi + 1 m
m
discontinuity !!
DDA Algorithm - Generic solution
What is m<0 ?
We must see the sign of Dx and Dy
8. Limitations
Rounding integers takes time
3.0 0.99 { 3, 1 }
But we Round Off to the nearest whole
number just before we draw the pixel. 4.0 1.32 { 4, 1 }
e.g. if m=.333 …
Bresenham’s Line Algorithm
1. Introduction
One disadvantage of DDA is the ROUNDing part which can be
expensive
Developed by Jack Bresenham
at IBM in the early 1960s
One of the earliest algorithms in
computer graphics
The Algorithm is based on essentially
the same principles but is completely
based on integer variables
Bresenham’s Line Algorithm
2. Basic Concept
Find the closest integer coordinates to the actual line path using
only integer arithmetic
Candidate for the next pixel position
Specified
Line Path
0 m 1
Specified
No division, Efficient comparison, No floating point operations
Line Path
Bresenham’s Line Algorithm
3. Derivation
The algorithm is derived for a line having slope 0< m < 1 in
the first quadrant.
Pixel position along the line are plotted by taking unit step
increments along the x-direction and determining y-
coordinate value of the nearest pixel to the line at each step.
Can be generalized for all the cases
Bresenham’s Line Algorithm
Let us assume that P(xk, yk ) is the currently plotted pixel. Q(xk+1,
yk+1 ) (xk+1, y ) is the next point along the actual path of line.
We need to decide next pixel to be plotted from two candidate
positions Q1(xk+1, yk ) or Q2(xk+1, yk+1)
yk + 1 Q2
d2
yk + 2 y Q
yk + 1 Q2 d1
yk P
Q
Q1
y = mx + b yk P
Q1
xk + 1
xk xk + 1 xk + 2
0 m 1, xk xl , k l
Bresenham’s Line Algorithm
Given the equation of line
y = mx + b
Thus actual value of y at x = xk+1 is given by
y = mxk+1 + b = m(xk + 1) + b
Let d1 = | QQ1|= distance of yk from actual value of y
= y – yk = m(xk + 1) + b – yk
d2 = | QQ2| = distance of actual value of y from yk +1
= yk+1 – y = (yk + 1)– [m(xk + 1) + b]
The difference between these 2 separations is
d1-d2 = 2m(xk + 1) + 2b –yk– (yk+ 1)
= 2m(xk + 1) – 2 yk + 2b – 1
Bresenham’s Line Algorithm
we can define a decision parameter pk for the kth step to by
simplifying above equation such that the sign of Pk is the same
as the sign of d1-d2, but involves only integer calculations.
Define Pk = Δx ( d1-d2)
= Dx(2m( xk + 1) − 2 yk + 2b − 1)
Dy
= Dx(2 ( xk + 1) − 2 yk + 2b − 1)
Dx
= 2Dy xk − 2Dx yk + 2Dy + Dx(2b − 1)
= 2Dy xk − 2Dx yk + c
where c = 2 Δy + Δx( 2 b − 1 ) a constant
Bresenham’s Line Algorithm
If pk< 0
( d1-d2) <0
distance d1 is less than d2
yk is closer to line-path
Hence Q1(xk+1, yk ) is the better choice
else
Q2(xk+1, yk+1) is the better choice
k pk (xk+1, yk+1)
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
1. (x0, y0) = (12,10)
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
3. p0 = 2Dy – Dx =3
k pk (xk+1, yk+1)
0 3
2
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
1. (x0, y0) = (12,10)
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
3. p0 = 2Dy – Dx =3
k pk (xk+1, yk+1)
0 3 (13, 11)
1 1 (14, 12)
2 -1 (15, 12)
3 7 (16, 13)
4 5 (17, 14)
Bresenham’s Line Algorithm
5. Exercise: Trace for (20,10) to (30,18)
k pk (xk+1,yk+1)
18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30
Bresenham’s Line Algorithm
Answer
k pk (xk+1,yk+1)
18
0 6 (21,11)
17 1 2 (22,12)
16 2 -2 (23,12)
15 3 14 (24,13)
14 4 10 (25,14)
13 5 6 (26,15)
12 6 2 (27,16)
11 7 -2 (28,16)
10 8 14 (29,17)
20 21 22 23 24 25 26 27 28 29 30 9 10 (30,18)
Circle Drawing Algorithms
1 2 3 4
Midpoint Circle Algorithm
M
5
1 2 3 4
Midpoint Circle Algorithm
6
M
5
1 2 3 4
Midpoint Circle Algorithm
3. Derivation
Let us assume that P(xk, yk ) is the
currently plotted pixel. Q(xk+1,
yk+1 ) (xk+1, y ) is the next
point along the actual circle path.
We need to decide next pixel to
be plotted from among candidate
positions Q1(xk+1, yk ) or
Q2(xk+1, yk-1)
Midpoint Circle Algorithm
Our decision parameter is the circle function evaluated at the
midpoint between these two pixels
If pk < 0 ,
this midpoint is inside the circle and
the pixel on the 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 the scan line yk-1
Midpoint Circle Algorithm
Successive decision parameters are obtained using incremental
calculations
pk = (xk +1)2 + (yk – ½ )2 – r2
Put k = k+1
pk+1 = [(xk+1)+1]2 + (yk+1 – ½ )2 – r2
= (xk+2)2 + (yk+1 – ½ )2 – r2
subtracting pk from pk+1
pk+1 – Pk = (xk+2)2 + (yk+1 – ½ )2 – [(xk +1)2 + (yk – ½ )2] or
pk+1 = Pk+2(xk+1) + (yk+1 – ½)2 – (yk – ½)2+1
Where yk+1 is either yk or yk-1, depending on the sign of pk
Midpoint Circle Algorithm
If pk< 0
Q1(xk+1, yk ) was the next choice
yk+1 = yk
(yk+1 – ½)2 – (yk – ½)2= 0
pk+1 = pk + 2.xk +3
else
Q2(xk+1, yk-1) was the next choice
yk+1 = yk+1
(yk+1 – ½)2 – (yk – ½)2 = – 2.yk +2
pk+1 = pk + 2(xk – yk )+ 5
Midpoint circle algorithm