CGV Lab Manual 2021-1
CGV Lab Manual 2021-1
CGV Lab Manual 2021-1
LABORATORY MANUAL
“COMPUTER GRAPHICS LABORATORY WITH MINI
PROJECT – 17CSL68 & 18CSL67”
Prepared By
Prof. JAYASHREE N
Assistant Professor, Dept. of CSE.
Scrutinized By
Dr. S N CHANDRASHEKARA
Professor & HOD, Dept. of CSE, CBIT
Lab Experiments
PART A
Design, develop, and implement the following programs using OpenGL API
1. Implement Brenham’s line drawing algorithm for all types of slope.
Refer:Text-1: Chapter 3.5 Refer:Text-2: Chapter 8
2. Create and rotate a triangle about the origin and a fixed point.
Refer:Text-1: Chapter 5-4
3. Draw a color cube and spin it using OpenGL transformation matrices.
Refer:Text-2: Modeling a Colored Cube
4. Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing.
Refer:Text-2: Topic: Positioning of Camera
5. Clip a lines using Cohen-Sutherland algorithm.
Refer:Text-1: Chapter 6.7 Refer:Text-2: Chapter 8
6. To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surfaces of
the solid object used in the scene.
Refer:Text-2: Topic: Lighting and Shading
7. Design, develop and implement recursively subdivide a tetrahedron to form 3D
Sierpinski gasket. The number of recursive steps is to be specified by the user.
Refer: Text-2: Topic: Sierpinski gasket.
8. Develop a menu driven program to animate a flag using Bezier Curve algorithm.
Refer: Text-1: Chapter 8-10
9. Develop a menu driven program to fill the polygon using scan line algorithm.
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT (17CSL68/18CSL67)
Project
PART –B ( MINI-PROJECT)
Student should develop mini project on the topics mentioned below or similar applications
using OpenGL API. Consider all types of attributes like color, thickness, styles, font,
background, speed etc., while doing mini project.
(During the practical exam: the students should demonstrate and answer Viva-Voce)
Sample Topics:
Simulation of concepts of OS, Data structures, algorithms etc.
Course outcomes: The students should be able to:
• Apply the concepts of computer graphics
• Implement computer graphics applications using OpenGL
• Animate real world problems using OpenGL
Conduction of Practical Examination:
1. All laboratory experiments from part A are to be included for practical examination.
2. Mini project has to be evaluated for 30 Marks as per 6(b)
3. Report should be prepared in a standard format prescribed for project work.
4. Students are allowed to pick one experiment from the lot.
5. Strictly follow the instructions as printed on the cover page of answer script.
6. Marks distribution:
a) Part A: Procedure + Conduction + Viva:10 + 35 +5 =50 Marks
b) Part B: Demonstration + Report + Viva voce = 15+10+05 = 30 Marks
7. Change of experiment is allowed only once and marks allotted to the procedure part
to be made zero.
Reference books
1. Donald Hearn & Pauline Baker: Computer Graphics-OpenGL Version,3rd Edition,
Pearson Education,2011
2. Edward Angel: Interactive computer graphics- A Top Down approach with OpenGL,
5th edition. Pearson Education, 2011.
3. M M Raikar, Computer Graphics using OpenGL, Fillip Learning / Elsevier,
Bangalore / New Delhi (2013).
INTRODUCTION
OpenGL
OpenGL (Open Graphics Library) is an application program interface (API) that is used to define
2D and 3D computer graphics.
The interface consists of over 250 different function calls which can be used to draw
complex three-dimensional scenes from simple primitives. OpenGL was developed by Silicon
Graphics Inc. (SGI) in 1992 and is widely used in CAD, virtual reality, scientific visualization,
information visualization, and flight simulation.
OpenGL's basic operation is to accept primitives such as points, lines and polygons, and
convert them into pixels. This is done by a graphics pipeline known as the OpenGL state
machine.
FEATURES OF OpenGL
• Geometric Primitives Allow you to construct mathematical descriptions of objects.
• Color coding in RGBA (Red-Green-Blue-Alpha) or in color index mode.
• Viewing and Modeling permits arranging objects in a 3-dimensional scene, move our camera
around space and select the desired vantage point for viewing the scene to be rendered.
• Texture mapping helps to bring realism into our models by rendering images of realistic
looking surfaces on to the faces of the polygon in our model.
• Materials lighting OpenGL provides commands to compute the color of any point given the
properties of the material and the sources of light in the room.
• Double buffering helps to eliminate flickering from animations. Each successive frame in an
animation is built in a separate memory buffer and displayed only when rendering of the
frame is complete.
• Anti-aliasing reduces jagged edges in lines drawn on a computer display. Jagged lines often
appear when lines are drawn at low resolution. Anti-aliasing is a common computer graphics
technique that modifies the color and intensity of the pixels near the line in order to reduce the
artificial zig-zag.
• Gouraud shading is a technique used to apply smooth shading to a 3D object and provide
subtle color differences across its surfaces.
• Z-buffering keeps track of the Z coordinate of a 3D object. The Z-buffer is used to keep track
of the proximity of the viewer's object. It is also crucial for hidden surface removal
• Transformations: rotation, scaling, translations, perspectives in 3D, etc.
GLUT FUNCTIONS
The OpenGL Utility Toolkit (GLUT) is a programming interface with ANSI C and FORTRAN
bindings for writing window system independent OpenGL programs.
argcp
A pointer to the program's unmodified argc variable from main. Upon return, the value
pointed to by argcp will be updated, because glutInit extracts any command line options intended
for the GLUT library.
Dept. of CSE, CBIT, Kolar. 3 2020-21
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT (17CSL68/18CSL67)
argv
The program's unmodified argv variable from main. Like argcp, the data for argv will be
updated because glutInit extracts any command line options understood by the GLUT library.
glutInitWindowPosition, glutInitWindowSize :-
glutInitWindowPosition and glutInitWindowSize set the initial window position and size
respectively.
Syntax:- void glutInitWindowSize(int width, int height);
void glutInitWindowPosition(int x, int y);
mode :- Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. Mode can
take following values.
Values Meaning
Bit mask to select an RGBA mode window. This is the
GLUT_RGBA default if neither GLUT_RGBA nor GLUT_INDEX
are specified.
GLUT_RGB An alias for GLUT_RGBA.
GLUT_INDEX Bit mask to select a color index mode window. This
overrides GLUT_RGBA if it is also specified.
GLUT_SINGLE Bit mask to select a single buffered window. This is
the default if neither
GLUT_DOUBLE Bit mask to select a double buffered window. This
overrides GLUT_SINGLE if it is also specified.
GLUT_DEPTH Bit mask to select a window with a depth buffer.
glutReshapeFunc :- glutReshapeFunc sets the reshape callback for the current window.
Syntax:- void glutReshapeFunc(void (*func)(int width, int height));
func :- The new reshape callback function.
glutKeyboardFunc :- glutKeyboardFunc sets the keyboard callback for the current window.
Syntax:- void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
func :- The new keyboard callback function.
glutMouseFunc:- glutMouseFunc sets the mouse callback for the current window.
Syntax:- void glutMouseFunc(void (*func)(int button, int state, int x, int y));
func :- The new mouse callback function.
GL FUCTIONS
glBegin & glEnd :- The glBegin and glend functions delimit the vertices of a primitive or a
group of like primitives.
Syntax :- void glBegin( GLenum mode );
mode :- The primitive or primitives that will be created from vertices presented
between glBeginand the subsequent glEnd. The following are accepted symbolic constants and
their meanings:
Value Meaning
GL_POINTS Treats each vertex as a single point. Vertex n defines
point n. N points are drawn.
GL_LINES Treats each pair of vertices as an independent line
segment. Vertices 2n - 1 and 2ndefine line n. N/2 lines are
drawn.
GL_LINE_STRIP Draws a connected group of line segments from the first
vertex to the last. Vertices n and n+1 define line n. N -
1 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. The last line, however, is defined by
vertices N and 1. N lines are drawn.
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.
GL_TRIANGLE_STRIP Draws a connected group of triangles. One triangle is
defined for each vertex presented after the first two
vertices. For odd n, vertices n, n + 1, and n + 2 define
triangle n. For even n, vertices n + 1, n, and n + 2 define
triangle n. N - 2 triangles are drawn.
GL_TRIANGLE_FAN Draws a connected group of triangles. one triangle is
defined for each vertex presented after the first two
vertices. Vertices 1, n + 1, n + 2 define triangle n. N -
2triangles are drawn.
GL_QUADS Treats each group of four vertices as an independent
quadrilateral. Vertices 4n - 3, 4n - 2, 4n - 1, and 4n define
Value Meaning
GL_COLOR_BUFFER_BIT The buffers currently enabled for color writing.
GL_DEPTH_BUFFER_BIT The depth buffer.
GL_ACCUM_BUFFER_BIT The accumulation buffer.
GL_STENCIL_BUFFER_BIT The stencil buffer.
glClearColor:- The glClearColor function specifies clear values for the color buffers.
Syntax :-void glClearColor(red, green, blue, alpha);
red :- The red value that glClear uses to clear the color buffers. The default value is zero.
green :-The green value that glClear uses to clear the color buffers. The default value is zero.
blue :- The blue value that glClear uses to clear the color buffers. The default value is zero.
alpha :-The alpha value that glClear uses to clear the color buffers. The default value is zero.
glColor3fv:- Sets the current color from an already existing array of color values.
Syntax :- void glColor3fv(const GLfloat *v);
V:- A pointer to an array that contains red, green, and blue values.
glEnable, glDisable :- The glEnable and glDisable functions enable or disable OpenGL
capabilities.
Syntax :- void glEnable(GLenum cap);
void glDisable(GLenum cap);
cap :- Both glEnable and glDisable take a single argument, cap, which can assume one of the
following values:
Value Meaning
GL_DEPTH_TEST If enabled, do depth comparisons and update the depth buffer.
See glDepthFuncand glDepthRange.
GL_LINE_SMOOTH If enabled, draw lines with correct filtering. If disabled, draw
aliased lines. See glLineWidth.
GL_LINE_STIPPLE If enabled, use the current line stipple pattern when drawing lines.
See glLineStipple.
GL_NORMALIZE If enabled, normal vectors specified with glNormal are scaled to
unit length after transformation. See glNormal.
GL_POINT_SMOOTH If enabled, draw points with proper filtering. If disabled, draw
aliased points. See glPointSize.
glFlush:- The glFlush function forces execution of OpenGL functions in finite time.
Syntax:- void glFlush(void);
This function has no parameters.
glFrustum:- The glFrustum function multiplies the current matrix by a perspective matrix.
Syntax :- void glFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar);
left :- The coordinate for the left-vertical clipping plane.
right :- The coordinate for the right-vertical clipping plane.
bottom :- The coordinate for the bottom-horizontal clipping plane.
top :- The coordinate for the bottom-horizontal clipping plane.
zNear :-The distances to the near-depth clipping plane. Must be positive.
zFar :- The distances to the far-depth clipping planes. Must be positive.
Value Meaning
GL_AMBIENT The params parameter contains four floating-point
values that specify the ambient RGBA intensity of the
light. Floating-point values are mapped directly.
Neither integer nor floating-point values are clamped.
The default ambient light intensity is (0.0, 0.0, 0.0,
1.0).
GL_DIFFUSE The params parameter contains four floating-point
values that specify the diffuse RGBA intensity of the
light. Floating-point values are mapped directly.
Neither integer nor floating-point values are clamped.
The default diffuse intensity is (0.0, 0.0, 0.0, 1.0) for
param :- Specifies the value that parameter pname of light source light will be set to.
glLoadIdentity :- The glLoadIdentity function replaces the current matrix with the identity
matrix.
Syntax :- void WINAPI glLoadIdentity(void);
glMatrixMode:- The glMatrixMode function specifies which matrix is the current matrix.
Syntax:- void glMatrixMode(GLenum mode);
mode :- The matrix stack that is the target for subsequent matrix operations. The mode parameter
can assume one of three values.
Value Meaning
GL_MODELVIEW Applies subsequent matrix operations to the modelview matrix
stack.
GL_PROJECTION Applies subsequent matrix operations to the projection matrix
stack.
GL_TEXTURE Applies subsequent matrix operations to the texture matrix
stack.
glOrtho:- The glOrtho function multiplies the current matrix by an orthographic matrix.
Syntax:- void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble zNear, GLdouble zFar);
left :-The coordinates for the left vertical clipping plane.
right :- The coordinates for theright vertical clipping plane.
Bottom:- The coordinates for the bottom horizontal clipping plane.
top :- The coordinates for the top horizontal clipping plans.
zNear :- The distances to the nearer depth clipping plane. This distance is negative if the plane is
to be behind the viewer.
zFar :- The distances to the farther depth clipping plane. This distance is negative if the plane is
to be behind the viewer.
glScalef :- The glScaled and glScalef functions multiply the current matrix by a general scaling
matrix.
Syntax :- void glScalef(GLfloat x, GLfloat y, GLfloat z);
x :- Scale factors along the x axis.
y :- Scale factors along the y axis.
z :- Scale factors along the z axis.
glTranslatef :- The glTranslatef function multiplies the current matrix by a translation matrix.
Syntax:- void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
x :- The x coordinate of a translation vector.
y :- The y coordinate of a translation vector.
z :- The z coordinate of a translation vector.
EXECUTION STEPS
1. Open TERMINAL.
gedit program.cpp
3. Command to compile
4. To execute
./PrgExec
Lab Experiment-1
Implement Bresenham’s line drawing algorithm for all types of slope.
F(M) = F(xp + 1, yp + ½ )
d=F(xp + 1, yp + ½ )
By definition
D=a(xp + 1) + b( yp + ½ )+c
Therefore, the value of d for next grid line depends on whether E is chosen or NE.
Location of M!
dnew - dold = a
dnew = dold + a
ΔE = a = dy
The value of decision variable is derived at the next step incrementally from the value at
the current step without having to compute F(M) directly by merely adding ΔE.
ii. If NE is chosen, M is incremented by one step each in both the x and y directions.
dnew - dold = a + b
dnew = dold + a + b
ΔNE = dy – dx.
Summary and Algorithm
At each step, the algorithm chooses between two pixels, based on sign of the decision variable
and then updates d by wither adding ΔE or ΔNE.
First pixel (x0, y0)
First midpoint (x0+1, y0 + ½ )
F(x0 + 1, y0 + 1/2) = a(x0 + 2) + b( y0 + 1/2 )+c
= ax0 + by0 +c + a + b/2
= 0 + a + b/2
dstart = a + b/2
dstart = dy – dx/2
3. Horizontal→Slope=0
4. Vertical→Slope undefined
LAB EXPERIMENT-2
Create and rotate a triangle about the origin and a fixed point
Homogenous co-ordinates representation for rotation of (x,y) is
𝑐𝑜𝑠𝜃 −𝑠𝑖𝑛𝜃 𝑚
[ 𝑠𝑖𝑛𝜃 𝑐𝑜𝑠𝜃 𝑛]
0 0 1
CODE
#include <iostream>
#include<GL/glut.h>
#include<math.h>
GLfloat h,k,theta;
int ch;
void multiply()
{
int i, j, k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
result[i][j]=0;
for(k=0;k<3;k++)
result[i][j]=result[i][j]+rot_mat[i][k]*triangle[k][j];
}
}
void drawTriangle()
{
int i;
glColor3f(0,0,1);
void drawrotatedTriangle()
{
int i;
glColor3f(1,1,0);
glBegin(GL_TRIANGLES);
for(i=0;i<=2;i++)
glVertex2f(result[0][i],result[1][i]);
glEnd();
}
void drawPoint(int h, int k)
{
//display the point about which the triangle is rotated
glColor3f(0,0,0);
glPointSize(7);
glBegin(GL_POINTS);
glVertex2f(h,k);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle();
rotate(h,k);
drawrotatedTriangle();
drawPoint(h,k);
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,499,0,499);
}
int main(int argc, char *argv[])
{
cout<<"Enter rotation angle: ";
cin>>theta;
cout<<"Do you want to rotate about \n 1.Origin or \n2.Fixed
point? Enter your choice:";
cin>>ch;
if(ch==2)
{
cout<<"Enter the vale of x and y";
cin>>h>>k;
}
else
{
h=0;
k=0;
LAB EXPERIMENT-3
Draw a color cube and spin it using OpenGL transformation matrices
ALGORITHM
i. Choose 8 three-dimensional coordinate points such that will make a cube.
ii. Select the axes to rotate
iii. Rotate the cube by a small angle every small interval of time.
glRotate*(angle,x,y,z)
is used to rotate an object by an angle in degrees about vecor x,y,z.
to rotate about x-axis, x=1, y=0, and z=0. Example, glRotatef(angle,1,0,0)
iv. Repeat step iii as long as axis is not changed.
CODE
#include <iostream>
#include<stdlib.h>
#include<GL/glut.h>
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-
2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-
2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
LAB EXPERIMENT-4
Draw a color cube and allow the user to move the camera suitably to experiment with
perspective viewing
PROCEDURE
i. Choose appropriate coordinates to make a cube
ii. Rotate or move the cube by a small variation on user input provided using keyboard and
mouse.
iii. Repeat step 2.
CODE
#include <iostream>
#include<stdlib.h>
#include<GL/glut.h>
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glFrustum(-2.0,2.0,-
2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,2.0,20.0);
else
glFrustum(-2.0,2.0,-
2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,2.0,20.0);
glMatrixMode(GL_MODELVIEW);
}
OUTPUT
LAB EXPERIMENT-5
Clip a lines using Cohen-Sutherland algorithm
In computer graphics, line clipping is the process of removing lines or portions of lines
outside an area of interest. Typically, any line or part thereof which is outside of the
viewing area is removed.
The Cohen–Sutherland algorithm is a computer graphics algorithm used for line
clipping. The algorithm divides a two-dimensional space into 9 regions and then
efficiently determines the lines and portions of lines that are visible in the center region
of interest (the viewport). The algorithm quickly detects and dispenses with two common
trivial cases. To clip a line, only its end points are to be considered. If both end points of
a line lie inside the window, the entire line lies inside the window. It is trivially accepted
and needs no clipping. On the other hand, if both end points of a line, lie entirely to one
side of a window, the line must lie entirely outside the window. It is trivially rejected and
needs to be neither clipped nor displayed.
ALGORITHM
i. End-points pairs are check for trivial acceptance or trivial rejected using the outcode.
ii. If not trivial-accepance or trivial-rejected, divided into two segments at a clip edge.
iii. Iteratively clipped by testing trivial-acceptance or trivial-rejected, and divided into two
segments until completely inside or trivial-rejected
NOTE:
Outcodes: Region and Outcode
Each region of the nine regions is assigned a 4-bit code, the nine regions and their outcodes
are shown below:
iv. To perform trivial accept and reject tests, we extend the edges of the clip
rectangle to divide the plane of the clip rectangle into nine regions. Each
region is assigned a 4-bit code determined by where the region lies with
CODE
#include<stdio.h>
#include<iostream>
#include<GL/glut.h>
double xmin=50,ymin=50,xmax=100,ymax=100;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
OUTPUT
LAB EXPERIMENT-6
To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surfaces of the
solid object used in the scene
PROCEDURE
i. Use the function glutSolidCube() to draw wall and table.
ii. Use the same function to draw 4 cubes and then scale it in downward direction to
make it look like table legs.
iii. glutSolidTeapot() is used to draw teapot.
CODE
#include <iostream>
#include <GL/glut.h>
//start drawing
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
table(0.6,0.02,0.02,0.3);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(90,0,0,1);
wall(0.02);
glPopMatrix();
glPushMatrix();
glRotated(-90,1,0,0);
wall(0.02);
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
glFlush();
‘OUTPUT
LAB EXPERIMENT-7
Design, develop and implement recursively subdivide a tetrahedron to form 3D
Sierpinski gasket. The number of recursive steps is to be specified by the user
A geometric method of creating the gasket is to start with a triangle and cut out the middle
piece as shown in the generator below. This results in three smaller triangles to which the
process is continued. The nine resulting smaller triangles are cut in the same way, and so
on, indefinitely. The gasket is perfectly self-similar, an attribute of many fractal images.
Any triangular portion is an exact replica of the whole gasket. The construction of the 3
dimensional version of the gasket follows similar rules for the 2D case except that the
building blocks are square based pyramids instead of triangles.
ALGORITHM
CODE
#include <iostream>
#include<stdlib.h>
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
tetrahedron(n);
glFlush();
}
LAB EXPERIMENT-8
Develop a menu driven program to animate a flag using Bezier Curve algorithm
The Bezier curve was originally used by Pierre Bezier for CAD/CAM operations are Renault
motor car company. Bezier curves are the basis of the entire Adobe PostScript drawing model.
2. The degree of the polynomial defining the curve segment is one less than the number of
defining polygon points
4. The first and last points on the curve are coincident with the first and last points of the
defining polygon.
5. The tangent vectors at the ends of the curve have the same direction as the first and the
last polygon spans respectively.
6. The curve is contained within the convex hull of the defining polygon, i.e., within the
largest convex polygon obtainable with the polygon vertices. (dashed lines in figure →
convex hull)
7. The curve exhibits the variation diminishing property. Basically this means that the
curve does not oscillate about any straight line more often that the defining polygon.
Bernstein basis function: here, n, the degree of the defining Bernstein basis function and thus of
the polynomial curve segment, is one less than the number of points in the defining Bezier
polygon. The vertices of the Bezier polygon are numbered from 0 to n as shown above.
A cubic Bezier curve is defined by 4 points. Two of the points are endpoints to the curve, (x 0,y0)
if the starting point and (x3,y3) is the destination point. The points (x1,y1) and (x2,y2) are control
points or handles.
LAB EXPERIMENT-9
Develop a menu driven program to fill the polygon using scan line algorithm
To make the addition of edges to the Active Edge Table (AET) efficient, initially create
global Edge Table (ET) containing all the edges sorted by their smaller y co-ordinates. The ET is
typically built by using a bucket sort with as many buckets as there are scan lines. Within each
bucket, edges are kept in order of increasing x co-ordinate of the ower end point. Each entry
in the ET contains the ymax co-ordinate of the edge, the x co-ordinate of the bottom end point
(xmin), and the x increment 1/m.
1. Set y to the smallest y co-ordinate that has an entry in ET; i.e., y for the first nonempty
bucket.
a. Move from ET bucket y to the AET those edges whose ymin=y (entering edge).
b. Remove from the AET those entries for which y=ymax (edges not involved in the next
scan line), the sort the AET on x.
c. Fill in desired pixel values on scan line y by using pairs of x coordinate from the
AET.
e. For each non-vertical edge remaining in the AET, update x for the new y.
ALGORITHM
For each scan line
i. Find the intersections of the scan line with all edges of the polygon
Department of CSE, CBIT, Kolar. Page | 44 2020-21
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT (17/18CSL68)
ii. Sort the intersections by increasing x-coordinate
iii. Fill in all pixels between pairs of intersections
CODE
#include <iostream>
#include <GL/glut.h>
using namespace std;
float x1,x2,x3,x4,y1,y2,y3,y4;
int red=0;
int green=0;
int blue=0;
void myinit()
{
glClearColor(1,1,1,1);
glColor3f(1,0,0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,499,0,499);
}
void getVal()
{
cout<<"Enter enter point 1: ";
cin>>x1>>y1;
cout<<"Enter enter point 2: ";
cin>>x2>>y2;
cout<<"Enter enter point 3: ";
cin>>x3>>y3;
cout<<"Enter enter point 4: ";
cin>>x4>>y4;
}
void setcolor(bool r, bool g, bool b)
{
red=r;
green=g;
blue=b;
}
int sub_menu;
void color_menu(int id)
{
switch(id)
{
case 2:
setcolor(1,0,0);
break;
case 3:
setcolor(0,1,0);
break;
case 4:
setcolor(0,0,1);
break;
case 5:
setcolor(1,1,0);
break;
case 6:
setcolor(1,0,1);
break;
case 7:
Department of CSE, CBIT, Kolar. Page | 45 2020-21
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT (17/18CSL68)
setcolor(0,1,1);
break;
}
glutPostRedisplay();
}
void top_menu(int id)
{
switch(id)
{
case 1:
exit(0);
break;
default:color_menu(id);
break;
}
}
void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
float mx,x,temp;
int i;
if((y2-y1)<0)
{
temp=y1; y1=y2; y2=temp;
temp=x1; x1=x2; x2=temp;
}
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else
mx=x2-x1;
x=x1;
for(i=y1;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void draw_pixel(int x, int y)
{
glColor3f(red,green,blue);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void scanfill(float x1,float y1,float x2,float y2,float x3,float
y3,float x4,float y4)
{
int le[500], re[500];
int i,y;
for(i=0;i<500;i++)
{
le[i]=500;
re[i]=0;
}
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
1. What is OpenGL?
2. What are OpenGL utilities and Libraries?
3. What is the use of glut.h?
4. What is the method generation of sierpinski gasket?
5. Write the code to generate the sierpinski gasket in a 2D co-ordinate system.
6. Differentiate additive color model from subtractive color model
7. What are the two classes of primitives openGL supports?
8. What is openGL interface?
9. List out different openGL primitives, giving examples.
10. What are different openGLframes?
11. Define the following 2 dimensional transformations
1) translation 2)rotation 3)scaling 4)reflection
12. What are the basic transformations in 3D?
13. What is concatenation?
14. What are the advantages of quaternion?
15. What is projection normalization?
16. Explain with the help of opengl functions perspective and parallel viewing opengl?
17. What is gluLookAt() function?
18. What are different types of light sources support by opengl?
19. What is aliasing?
20. What are four major tasks in a pipeline implementation?
21. What is Cohen Sutherland line clipping?
22. What do you understand by clipping?
23. What is Z-buffer algorithm for removing hidden faces?
24. What are orthographic projections? When do we need them?
25. What is antialiasing? Explain different methods of minimizing its effect? What is
polygon clipping?
26. What is polygon clipping?
27. What. are windowing and clipping?
28. List the advantages of interactive Graphics.
29. What do you mean by composite transformation? How it is useful?
30. What is a projection matrix?