Lab Manual - CG .PDF Filename - Lab Manual - CG
Lab Manual - CG .PDF Filename - Lab Manual - CG
LAB MANUAL
Computer Graphics(CSL402)
DEPARTMENT’S VISION
To be a centre of Excellence in Computer Engineering to fulfill the rapidly growing needs of the
Society.
DEPARTMENT’S MISSION
M1: To Impart quality education to meet the professional challenges in the area of Computer
Engineering.
M2: To create an environment for research, innovation, professional and social development.
M3: To nurture lifelong learning skills for achieving professional growth.
M4 To strengthen the alumni and industrial interaction for overall development of students.
PSO1: To apply computational and logical skills to solve Computer engineering problems.
PSO2: To develop interdisciplinary skills and acquint with cutting edge technologies in
software industries.
LO1: Explore the working principle, utility of various input/ output devices and graphical tools
LO2:Implement various output and filled area primitive algorithms using c/ opengl.
LO3: Apply transformation on graphical objects.
LO4: Apply clipping on graphical objects.
LO5: Implementation of curve and fractal generation
LO6: Develop a graphical application based on learned concept
Lab Outcome PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
Explore the working 3
principle, utility of
various input/ output
devices and graphical
tools
LO1
Implement various 3 2 3
output and filled area
primitive algorithms
using c/ opengl. 1 3 3 3 1
LO2
Apply transformation 3 2
on graphical objects. 3 3 3 3
LO3
Apply clipping on 3 2
graphical objects. 3 3 3
LO4
Implementation of 3
curve and fractal
generation 3 3 3
LO5
Develop a graphical 3
application based on
learned concept.
LO6
Laboratory Assessment
3: Well prepared and puts 2: Not prepared but puts 1: Neither prepared nor puts
efforts. efforts or prepared but doesn't efforts
put efforts
3: Uses all perfect Instructions 2: Uses some perfect 1: Doesn't use any of the
/ Interrupts/Presented well. Instructions / Interrupts/ proper Instruction / Interrupt/
moderate presentation. Not presented properly.
3: Participate and gets proper 2: Participate but doesn't get 1: Neither Participate nor gets
results proper result or gets result but the results
with the help of faculty in-
charge
4. Punctuality
3: Get the experiment 2: Some time delays the 1: Most of the time delays
checked in-time and is always experiment checking or is late experiment checking and / or
in-time to the lab sessions to the lab sessions for few comes late for lab sessions
times
5. Lab Ethics
3: Follows proper lab ethics 2: Sometimes doesn't follow 1: Most of the times makes
by keeping the lab clean and
placing things at their right the lab ethics the lab untidy and keeps
place things at wrong place
5. Lab Ethics
Total
Average
EVALUATION:
Faculty In-charge
SR NO EXPERIMENT TITLE
5 Implement 2D Transformations.
Theory: - The DDA is a scan conversion line algorithm based on calculating either dy or dx. We
sample the line at unit intervals either in one coordinate and describe corresponding integer
values nearest the line path for the other coordinates. An accurate and efficient faster line
generating algorithm developed by Bresenhams can convert line only using incremental integer
calculations that can be adopted to display circles and other curves sampling at unit x intervals
we need to decide which of the two possible pixel positions is closer to line path.
Algorithm:
1. Input x1, y1, x2, y2, dx, dy, steps and k are of type integer and xinc, yinc, x, y of type
float.
2. dx = x2-x1
dy = y2-y1
4. xinc = dx / steps;
5. yinc = dy / steps
7. x=x1;
y=y1;
y= y + yinc;
set pixel(round(x),round(y));
9. STOP.
Conclusion: DDA Line drawing algo. Contains floating point arithmetic and round up operation.
Bresenham’s Line Drawing algo. Contains only integer arithmetic.
Theory:-
As in the raster line algorithm we sample at unit intervals & determine the closest pixel position
to the specified circle path for each step. For a given radius r and screen center position (xc,yc).
We can first setup our algorithm to calculate pixel position around a circle path centered at
coordinate origin(0,0). Then each calculated position (x,y) is moved to its proper position by
adding xc to x & yc to y. Positions in other seven octants are obtained later by using symmetry.
1. Input radius r and circle centre (xc, yc) and obtain the first point on circle centered on
origin (0,r).
2. Calculate Initial Decision parameter as p = (5/4) – r.
3. At each xk position starting at k=0 perform the following test:
If(pk<0)
pk+1 = pk + 2xk+1 +1
Else
Conclusion: Mid Point Circle drawing algo use Symmetry concept. Circle is symmetric about
Octant and Circle function act as a decision parameter.
Theory: -
Mid point Ellipse drawing algorithm uses quad symmetry of an ellipse.
An ellipse is considered to consist of two regions R1 & R2. The slope at the intersection point of
these regions is +1 or -1.
dy / dx = -ry2x / rx2y
Algorithm:
1. Input rx, ry and ellipse center (xc,yc), and obtain the first point on an ellipse centered on the
origin as
(x0, y0) = (0, ry)
2. Calculate the initial value of the decision parameter in region 1 as
Otherwise, the next point along the circle is (xk+1, yk-1) and
4. Calculate the initial value of the decision parameter in region 2 using the last point (x0, y0)
calculated in region 1 as
7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot
the coordinate values:
x = x + xc, y = y + yc
Conclusion:
Mid Point ellipse drawing implemented using concept of symmetry and for region1 and region2
pixel positions are calculated.
Theory: -
In this method, edges of the polygon are drawn. Then starting with some seed, any point inside
the polygon we examine the neighboring pixels to check whether the process is continued until
boundary pixels are reached. Since this process involves checking of boundaries. This method is
also called Boundary Fill Method.
The following procedure illustrates the recursive method for filling a 4 connected region with
color specified in parameter fill color (f_color) up to boundary color specified with parameter
boundary color (b_color).
Algorithm:
We can paint such areas by replacing a specified interior color instead of searching for a
boundary color value. This approach is called a flood-fill algorithm.
We start from a specified interior point (x, y) and reassign all pixel values that are currently set
to a given interior color with the desired fill color.
If the area we want to paint has more than one interior color, we can first reassign pixel values so
that all interior points have the same color.
Using either a 4-connected or 8-connected approach, we then step through pixel positions until
all interior points have been repainted.
The following procedure flood fills a 4-connected region recursively, starting from the input
position.
Theory:-
Translation:
Translation is a process of changing the position of an object from one coordinate
Location to another. We can translate a two dimensional point by adding translation distances Tx
and Ty to the original coordinate position (x,y) to move the new position (x',y').
x' = x + Tx
y' = y + Ty
The translation distance pair (Tx, Ty) is called a Translation Vector or Shift Vector.
The homogeneous coordinate for translation are given as:
P' = P * T
x' 1 0 Tx x
y' = 0 1 Ty y
1 0 0 1 1
x' x + Tx
y' = y +Ty
1 1
2. Rotation:
To generate rotation, we specify a rotation angle theta and the position of the rotation point about
which the object
is to be rotated.
The transformation equation for rotating a point(x,y) through an angle theta about the origin as:
3. Scaling:
Scaling transformation changes the size of an object. This operation can be carried out for
polygons by multiplying
the coordinate values (x,y) of each vertex by scaling factors Sx and Sy to produce the
transformed coordinate (x',y').
x' = x * Sx
y' = y * Sy
Scaling factor scales the object in the x direction and scaling factor scales the object in the y
direction.
Sx 0 0
S= 0 sy 0
0 0 1
Therefore
x' Sx 0 0 x
y' = 0 Sy 0 y
1 0 0 1 1
x' x * Sx
y' = Y * Sy
1 1
X Shear:
The x shear preserves the y coordinates, but changes the x values which causes vertical
lines to tilt right or left.
1 Shx 0
x_sh = 0 1 0
0 0 1
x' = x + Shx * y
y' = y
Y Shear:
The Y shear preserves the x coordinates, but changes the y values which causes
horizontal lines to transform into lines which slope up or down.
1 0 0
Y-sh = Shy 1 0
0 0 1
x' = x
y' = y + Shy * x
5. Reflection:
A Reflection is a transformation that produces a mirror image of an object relative to an axis of
reflection.
We can choose an axis of reflection in the xy plane or perpendicular to the xy plane.
x' = x
y' = -y
1 0 0
0 -1 0
0 0 1
x' = -x
y' = y
-1 0 0
0 1 0
0 0 1
x' = -x
y' = -y
-1 0 0
0 -1 0
0 0 1
0 1 0
1 0 0
0 0 1
0 -1 0
-1 0 0
0 0 1
Conclusion :- Transformations like translation, rotation, scaling, reflection about x=y and Shear
X and Shear Y are implemented.
Theory: - In computer graphics, line clipping is the process of removing lines or portions of lines
outside of an area of interest. Typically, any line or part thereof which is outside of the viewing
area is removed.
In Liang-Barsky algorithm, we first write the point-clipping conditions in parametric form as
Each of these for inequalities can be expressed as, k = 1, 2, 3, ….. Where parameter p and q are
defined as
Any line that is parallel to one of the clipping boundaries has pk = 0 for the value of k
corresponding to that boundary. If, for that value of k, we also find qk < 0, then the line is
completely outside the boundary and can be eliminated from further consideration. If qk >=0, the
line is inside the parallel clipping boundary.
When pk <0, the infinite extension of the line proceeds from the outside to the inside of the
inside of the infinite extension of this particular boundary. If pk > 0, the line proceeds from the
insides to the outside. For a nonzero value of pk, we can calculate the value of u that corresponds
to the point where the infinitely extended line intersects the extension of boundary k as u = qk /
pk.
Algorithm:
and
,
which can be expressed as the 4 inequalities
,
where
(left)
(right)
(bottom)
(top)
To compute the final line segment:
A line parallel to a clipping window edge has for that boundary.
If for that , , the line is completely outside and can be eliminated.
When the line proceeds outside to inside the clip window and when , the line
proceeds inside to outside.
.
b)For , look at boundaries for which (inside -> out). Take to be the minimum of
.
c) If , the line is outside and therefore rejected.
Theory:-
Most of the times characters re built into the graphics display devices usually as hardware but
sometimes through software.
1. Stroke Method:
- This method uses a small line segment to generate a character.
- The small series of line segments are drawn like a strokes of a pen to form a character.
- We can build our own stroke method character generator by calls to the line drawing algorithm.
- Here, it is necessary to decide which line segments are needed for each character and then
drawing these segments using line drawing algorithm. We can draw characters on the display.
-The stroke method supports scaling of the character.
It does this by changing the length of line segments used for character drawing.
2. Bitmap Method:
- It is also called as dot matrix because in this method characters represented by array of dots in
the matrix form.
- It is a two dimensional array having columns & rows.
- An 5 *7 array is commonly used to represent a character.
- An 7*9 & 9*13 arrays are also used.
- Higher resolution devices such as inkjet printer or laser printer may use character array that are
over 100 * 100.
3. Starbust Method:
- In this method a fix pattern of line segments are used to generate characters.
- There are 24 line segments.
- Out of these 24 line segments, segments required to display for particular character are
highlighted.
- This method of character generation is called starbust method because of its character
appearance.
- The pattern for particular character are stored in the form of 24 bit code.
- Each bit representing one line segment.
- The bit is set to 1 to highlight the line segment, otherwise set to zero.
- For e.g.: 24 bit code for
This method of character generation is not used. Now a days because of following
disadvantages:
The 24 bits are required to represent a character. Hence more memory is required.
Conclusion:-
Character generation methods like Stroke method and Bitmap method are implemented.
THEORY:-
Bezier curves use a different way of specifying the curve. The cubic Bezier curve requires four
sample points which completely specify the curve. The curve begins at first sample points and
end at fourth sample point. If we require two connected Bezier curves, it can be achieved by six
sample points where third and fourth point of first curve acts as first and second points of second
curve.
Equation of Bezier Curve is:
x = x4a3+3x3 a2 (1-a) +3x2a (1-a) 2+x1(1-a) 3
y = y4a3+3y3a2 (1-a) +3y2a (1-a) 2+y1 (1-a) 3
z = z4a3+3z3a2 (1-a) +3z2a (1-a) 2+z1 (1-a) 3
Here the a value of ‘a’ moves from 0 to 1 as the curve travel from first to fourth sample point.
Other method of constructing Bezier curve is by taking midpoints. By taking midpoints, we can
find a point on the curve and also split the curve into two sections. We can continue to split the
curve into smaller sections, until we have sections so short that they cannot be repeated by
straight lines or till size of section is not greater than size of pixel.
Theory:-
Given a set of n points, curve generation algorithms can generate a smooth curve passing through
by them. But to draw naturally realistic object we need a rough curve. E.g. Lightning.
Dimensional D of fractal can be given by N = SD
Where S is scaling factor and N is no. Of units which make up whole object.
Algorithm:
FRACTAL SUBDIVIDE(x1, y1, z1,x2, y2, z2, s, n)
Arguments:
X1, y1, z1
X2, y2, z2
S – Offset scale factor
N – Depth of recursion
Local : xmid, ymid, zmid coordinates at which to break the line.
Conclusion:-
Koch Curve is implemented using concept of fractals.
Theory:
OpenGL is the software interface to the graphic hardware.
OpenGL's main purpose is to render two- and three-dimensional objects into a frame buffer.
These objects are described as sequences of vertices (which define geometric objects) or pixels
(which define images).
OpenGL performs several processing steps on this data to convert it to pixels to form the final
desired image in the frame buffer.
OpenGL can render:
Geometric primitives such as Lines, points, polygons, etc.
Bitmaps and Images
OpenGL Primitives:
Point:
GL_POINTS:
Treats each vertex as a single point.
Vertex n defines a point n.
N points are drawn.
Sample:
glBegin(GLPOINTS);
glVertex2f(x1,y1);
glVertex2f(x1,y1);
glEnd();
Line:
GL_LINES:
Treats each pair of vertices as an independent line segment.
Vertices 2n-1 and 2n define a line n.
N/2 lines are drawn.
GL_LINE_LOOP:
Draws a connected group of line segments from the first vertex to the last, then back to the first.
Vertices n and n+1 define line n.
N lines are drawn.
Polygon:
GL_TRIANGLES:
Treats each triplet of vertices as an independent triangle.
Vertices 3n-2, 3n-1, and 3n define triangle n.
N/3 triangles are drawn.
Sample:
glBegin(GLTRIANGLES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glEnd();