Вы находитесь на странице: 1из 36

Page 1 of 36

CSC 3224: Computer Graphics Lab Manual




Fall 013-14
American International University
Bangladesh (AIUB)


















































[COMPUTER GRAPHICS:
LAB MANUAL]
This Lab Manual has been developed for the undergraduate students of Computer Science department for the
fulfillment of the course CSC3224: Computer Graphics and partial fulfillment for the graduation. This manual
will be followed in the lab classes as primary reference material. Other supporting documents beside this
manual can be consulted for the better understanding of the topics.
Page 2 of 36
CSC3224: Computer Graphics Lab Manual



Table of contents

Lab Assignment 1: Introduction to OpenGL Programming.................................................... 3

Lab Assignment 2: Basic Transformation .............................................................................. 6

Lab Assignment 3: Keyboard and Mouse Interaction ............................................................ 8

Lab Assignment 4: Composite Transformation.................................................................... 13

Lab Assignment 5: Line Drawing Algorithm ....................................................................... 15

Lab Assignment 6: Circle Drawing Algorithm .................................................................... 17

Lab Assignment 7: Ellipse Drawing Algorithm ................................................................... 19

Lab Assignment 8: Polygon Filling Algorithms................................................................... 21

Lab Assignment 9: Line Clipping Algorithms ..................................................................... 23

Lab Assignment 10: Polygon Clipping Algorithms.............................................................. 25

Lab Assignment 11: Curve Generation ................................................................................ 27

Lab Assignment 12: Lighting in OpenGL............................................................................ 30

Lab Assignment 13: Color in OpenGL ................................................................................ 33

Lab Assignment 14: Animation ........................................................................................... 35
Page 3 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 1: Introduction to
OpenGL Programming


Objective:

1. Becoming familiar with OpenGL programming structure using OpenGL Utility Toolkit (GLUT).
2. Learning how to initialize windows and objects inside a graphics window


Reference:

1. OpenGL Programming Guide, Red Book, Chapter-1


Prerequisite:

Knowledge of:

C/C++ Programming
Basic Coordinate Geometry


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 4 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:
Draw some basic shapes like Triangle, Polygon and fill them with various colors. The display window should be
titled with the name of the assignment.


Example Code: Drawing a Rectangle

void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);
/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
/* dont wait!
* start processing buffered OpenGL routines
*/
glFlush();
}
void init(void)
{
/* select clearing (background) color */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with hello
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/


Post Lab Assignment:

1. Draw all the shapes in the same window.
2. Make the window size 1024*768 px.
3. Set the window position at upper left corner of the monitor.
4. Draw an 8X8 chess board using loop.
Page 5 of 36
CSC3224: Computer Graphics Lab Manual



Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 6 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 2: Basic Transformation


Objective:

To implement set of Basic Transformations on Polygon i.e. Translation, Rotation and Scaling


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Basic Transformation
o Translation
o Rotation
o Scaling
Basic Coordinate Geometry
C/C++ Programming


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 7 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

Scaling Transformations:

A 2D point can be scaled by multiplication of the coordinate values (x,y) by scaling factors Sx and Sy to
produce the transformed coordinates (x',y').

Translation Transformations:

A 2D point can be translated by adding the coordinate values (x,y) by Translation distances Tx and Ty to
produce the transformed coordinates (x',y').

Rotation Transformations:

A 2D point can be rotated by repositioning it along a circular path in the xy plane. We specify the rotation angle
and the position of the rotation point about which the object is to be rotated. Multiplication of the coordinate
values (x,y) by rotation matrix produce the transformed coordinates (x',y').

Now you have to design a program which will provide you with the option that which type of transformation
you want to do. Like the following,

Enter your choice:

1. Translation
2. Rotation
3. Scaling
4. Exit

After providing the choice you have to enter the number of edges of a polygon and then you have to input the
coordinates of each vertex. Like the following,



Enter the no. of edges:-4
Enter the co-ordinates of vertex 1:- 30 30
Enter the co-ordinates of vertex 2:- 30 90
Enter the co-ordinates of vertex 3:- 90 90
Enter the co-ordinates of vertex 4:- 90 30
Enter the Translation factor for x and y:-20 20

After that you have to draw two polygons, one is the original polygon with supplied vertices and another is the
transformed one.


Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 8 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 3: Keyboard and
Mouse Interaction


Objective:

1. To study and implement Mouse and Keyboard interaction with OpenGL programs.
2. To study events in OpenGL, How to handle them and use them inside an OpenGL Program.


Reference:

1. OpenGL Programming Guide, Red Book, Chapter-1


Prerequisite:

Knowledge of:

C/C++ Programming
Basic Coordinate Geometry


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 9 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

Draw some basic shapes like Triangle, Polygon and fill them with various colors. The display window should be
titled as the name of the assignment. Then use the provided code to spin the shape and also zoom in and zoom
out effect on the shape.


Example Code: Showing How to use Keyboard and Mouse events inside a program

This Code describes spinning and zooming for a particular object drawn inside the display function.


static GLfloat spin = 0.0;
static GLfloat spin_speed = 1.0;
float spin_x = 0;
float spin_y = 1;
float spin_z = 0;

float translate_x = 0.0;
float translate_y = 0.0;
float translate_z = -30.0;

// assuming work-window width=50unit, height=25unit;
void init()
{
glClearColor(0,0,0,0);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearDepth(1.0f);
// Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
}

//------- custom functions starts -------


void setSpin(float x, float y, float z)
{
spin_x = x;
spin_y = y;
spin_z = z;
}

void reset()
{
spin_x = 0;
spin_y = 1;
spin_z = 0;
translate_x = 0.0;
translate_y = 0.0;
translate_z = -30.0;
}

//------- custom functions ends -------
Page 10 of 36
CSC3224: Computer Graphics Lab Manual



void reshape(int w,int h)
{
glViewport(0,0, (GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100.0f, (GLfloat)w/(GLfloat)h, 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(translate_x, translate_y, translate_z);
glRotatef(spin,spin_x,spin_y,spin_z);


//******************************************//
//------- custom code starts -------


//------- custom code ends -------
//******************************************//

glutSwapBuffers();
}

void spinDisplay(void)
{
spin=spin+spin_speed;
if(spin>360.0)
{
spin=spin-360.0;
}
glutPostRedisplay();
}

void spinDisplayReverse(void)
{
spin=spin-spin_speed;
if(spin<360.0)
{
spin=spin+360.0;
}
glutPostRedisplay();
}

void mouse(int button,int state,int x,int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
Page 11 of 36
CSC3224: Computer Graphics Lab Manual





break;
if(state==GLUT_DOWN)
glutIdleFunc(spinDisplay);
case GLUT_MIDDLE_BUTTON:
if(state==GLUT_DOWN)
{
glutIdleFunc(NULL);

}
break;
case GLUT_RIGHT_BUTTON:
if(state==GLUT_DOWN)
glutIdleFunc(spinDisplayReverse);
break;

default:
break;
}
}

void keyboard(unsigned char key, int x, int y)
{
//-------- spin --------
if(key=='x')
{
setSpin(1.0,0.0,0.0);
glutPostRedisplay();
}
else if(key=='y')
{
setSpin(0.0,1.0,0.0);
glutPostRedisplay();
}
else if(key=='z')
{
setSpin(0.0,0.0,1.0);
glutPostRedisplay();
}
else if(key=='a')
{
setSpin(1.0,1.0,1.0);
glutPostRedisplay();
}
//-------- spin --------


//-------- zoom --------
else if(key=='i')
{
translate_z++;
glutPostRedisplay();
}
Page 12 of 36
CSC3224: Computer Graphics Lab Manual



else if(key=='o')
{
translate_z--;
glutPostRedisplay();
}
//-------- zoom --------


//-------- reset -------
else if(key=='r')
{
reset();
glutPostRedisplay();
}
//-------- reset -------
}




Post Lab Assignment:


1. Draw all the shapes in the same window.
2. Make the window size 1024*768 px.
3. Set the window position at upper left corner of the monitor.
4. Check whether all the shapes in the same window can be spinned or zoomed separately or not. If not
then state why it is not possible, If yes then submit the code within the next lab.





Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 13 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 4: Composite
Transformation


Objective:

To study and Implement set of Composite Transformations on Polygon i.e. Reflection, Shear (x &y), and
Rotation about an arbitrary point.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Composite Transformation on Polygon
o Reflection
o Shear
o Rotation about an arbitrary point
Basic Coordinate Geometry
C/C++ Programming


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 14 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

Rotation about an arbitrary point:
This is done by three transformation steps: translation of the arbitrary point (Xc,Yc) to the origin, rotate about
the origin, and then translate the center of rotation back to where it belongs.
To transform a point, we would multiply all the transformation matrices together to form an overall
transformation matrix.

Now you have to design a program which will provide you with the option that which type of transformation
you want to do. Like the following,
Enter your choice:

1. Reflection
2. Shearing
3. Rotation about an arbitrary point
4. Exit

After providing the choice you have to enter the number of edges of a polygon and then you have to input the
coordinates of each vertex. Like the following,



Enter the no. of edges:-4
Enter the co-ordinates of vertex 1:- 30 30
Enter the co-ordinates of vertex 2:- 30 90
Enter the co-ordinates of vertex 3:- 90 90
Enter the co-ordinates of vertex 4:- 90 30
Enter the Translation factor for x and y:-20 20
a. Reflection along X-axis
b. Reflection along Y-axis
c. Exit
Enter your choice:

After that you have to draw two polygons, one is the original polygon with supplied vertices and another is the
transformed one.




Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 15 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 5: Line Drawing
Algorithm


Objective:

1. To study and implement DDA Algorithm
2. To study and implement Bresenhams Algorithm


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Point plotting Methods
DDA Algorithm
Bresenhams Algorithm


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 16 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

You have to design a program which will provide you with the option that which type of Algorithm you want to
use for drawing a line. Like the following,
Enter your choice:

1. DDA Algorithm
2. Bresenham
3. Exit

After providing the choice you have to enter the coordinates of start point and end point. Like the following,

Enter a Initial Point: - 100 200
Enter the Final Point: - 200 300


After that you have to draw the line with the supplied coordinates.


Algorithm: DDA

Input to the function is two endpoints (x1,y1) and (x2,y2)

1. length abs(x2-x1);
2. if (abs(y2-y1) > length) then length abs(y2-y1);
3. xincrement (x2-x1) / length;
4. yincrement (y2-y1) / length;
5. x x + 0.5; y Y + 0.5;
6. for i 1 to length follow steps 7 to 9
7. plot (trunc(x),trunc(y));
8. x x + xincrement ;
9. y y + yincrement ;
10. stop.


Post Lab Assignment:


1. Implement Bresenhams Algorithm for drawing a line.
2. Modify the Bresenhams Algorithm so that it will produce a dashed-line.


Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 17 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 6: Circle Drawing
Algorithm


Objective:

To study and Implement Midpoint circle algorithm given the points of the centre and the radius.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Midpoint circle Algorithm


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 18 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

You have to design a program which will take input the coordinates of the center of the circle as well as the
radius. Like following,
Enter the coordinates of the centre:

X-coordinate = 350
Y-coordinate = 250
Enter the radius: 50


After that you have to draw the circle with the supplied coordinates and radius.


Algorithm: Midpoint circle algorithm

1. Input radius r and circle center (x
0
,y
0
), and obtain the first point on the circumference of a circle centered on
the origin as(x
0
,y
0
)= (0 , r)
2. Calculate the initial value of the decision parameter asp
0
= 5 / 4 r
3. At each X
k
position , starting at k=0, perform the following test: If pk < 0 , the next point along the circle
centered on (0,0) is (X
k
+ 1 ,Y
k
) and pk+1 = pk + 2 X
k +1
+ 1 Otherwise ,the next point along the circle is (X
k
+
1, Y
k
-1) and pk+1 = pk + 2 X
k +1
+ 1 2Y
k+1
Where 2 X
k +1
= 2x
k
+ 2 and
2Y
k+1
= 2 y
k
-2.
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x,y) onto the circular path centered on (x
0
,y
0
) and plot the coordinate
values: x = x + x
c
, y = y + y
c

6. Repeat step 3 through 5 until x>= y


Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 19 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 7: Ellipse Drawing
Algorithm


Objective:

To study and Implement Midpoint Ellipse algorithm.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Midpoint Ellipse Algorithm


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 20 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

You have to design a program which will take input the coordinates of the center of the ellipse as well as the
major and minor axis. Like following,
Enter the coordinates of the centre:

X-coordinate = 350
Y-coordinate = 250
Enter the Minor axis: 50
Enter the Major axis: 100


After that you have to draw the ellipse with the supplied parameters.


Algorithm: Midpoint ellipse algorithm

1. Get parameters a, b, h, k for center coordinate h and k and major & minor axis lengths 2a and 2b.
2. Calculate the initial decision parameter value in the first region:
3. Use the formulas above to iterate px
k+1
until b
2
x>a
2
y.
4. Rename the current (x
k
,y
k
) as (x
0
,y
0
) and calculate the initial decision parameter value in the 2nd
region:

5. Use the formulas above to iterate py
k+1
until y <= 0.
6. For both regions plot the other three symmetry points.
7. Shift to center at h, k.


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 21 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 8: Polygon Filling
Algorithms


Objective:

To study and Implement Polygon Filling algorithm.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Boundary Filling Algorithm (4 and 8 connected)


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 22 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:
You have to design a program which will take input the coordinates of the interior points and then you have to
use the following algorithm to fill up those points.




Algorithm: Boundary Fill algorithm

(x,y) are the interior points, boundary is the boundary color and fill_color is the color to be filled. Following is a
recursive method for boundary fill.
1. present_color = getcolor() // a function which returns the current color of (x,y)
2. if present_color <> boundary and if present_color <> fill_color then repeat steps 3-7
3. set_pixel (x,y, fill_color)
4. call the algorithm recursively for points (x + 1, y)
5. call the algorithm recursively for points (x 1,y)
6. call the algorithm recursively for points (x,y + 1)
7. call the algorithm recursively for points (x,y - 1)
8. stop


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 23 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 9: Line Clipping
Algorithms


Objective:

To study and Implement Line Clipping with Cohen- Sutherland algorithm


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Cohen-Sutherland algorithm


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 24 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

You have to design a program which will take input the coordinates of two end points of a line and two window
coordinates. Then you have to draw the line and clipped window. Like,

Enter Minimum window co-ordinates:- 200 250
Enter Maximum window co-ordinates:- 300 350
Enter co-ordinates of first point of line: - 180 250
Enter co-ordinates of second point of line: - 200 300


Algorithm: Cohen-Sutherland algorithm

The Cohen-Sutherland algorithm uses a divide-and-conquer strategy. The line segment's endpoints are tested to
see if the line can be trivially accepted or rejected. If the line cannot be trivially accepted or rejected, an
intersection of the line with a window edge is determined and the trivial reject/accept test is repeated. This
process is continued until the line is accepted.

To perform the trivial acceptance and rejection tests, we extend the edges of the window to divide the plane of
the window into the nine regions. Each end point of the line segment is then assigned the code of the region in
which it lies.

1. Given a line segment with endpoint P
1
=(X
1
,Y
1
) and P
2
=(X
2
,Y
2
)
2. Compute the 4-bit codes for each endpoint.
If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies completely inside the window:
pass the endpoints to the draw function.
If both codes have a 1 in the same bit position (bitwise AND of the codes is not 0000), the line lies
outside the window. It can be trivially rejected.
3. If a line cannot be trivially accepted or rejected, at least one of the two endpoints must lie outside the
window and the line segment crosses a window edge. This line must be clipped at the window edge
before being passed to the drawing routine.
4. Examine one of the endpoints, say P
1
=(X
1
,Y
1
). Read P
1
's 4-bit code in order: Left-to-Right, Bottom-to-
Top.
5. When a set bit (1) is found, compute the intersection I of the corresponding window edge with the line
from P1 to P2. Replace P1 with I and repeat the algorithm.


Post Lab Assignment:

1. Implement the Liang-Barsky Algorithm in the same manner for clipping a line.


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 25 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 10: Polygon Clipping
Algorithms


Objective:

To study and Implement Polygon Clipping Algorithm using Sutherland - Hodgeman Algorithm


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Sutherland Hodgeman Algorithm


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 26 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:

You have to design a program which will take input the coordinates of two end points of a line and two window
coordinates. Then you have to draw the line and clipped window. Like,

Enter Minimum window co-ordinates:- 200 250
Enter Maximum window co-ordinates:- 300 350
Enter co-ordinates of first point of line: - 180 250
Enter co-ordinates of second point of line: - 200 300


Algorithm: Sutherland-Hodgeman algorithm

1. Input Coordinates of all vertices of the polygon
2. Input coordinates of the clipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually with the clipping plane
5. Save the resulting intersections and vertices in the new list of vertices according to four possible
relationships between the edge and the clipping boundary discussed earlier
6. Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the resultant list of vertices
is successively passed to process the next edge of the clipping window
7. Stop


Post Lab Assignment:

1. Explain why Sutherland-Hodgeman algorithm works only for convex clipping regions?


Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 27 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 11: Curve Generation


Objective:

To study and Implement Curves Generation using Bezeir Curves.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

Bezier Curve


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.



Problem Description:

You have to design a program which will take input of the coordinates of control point which is also determined
from user input. Then you have to draw the curve using the following algorithm. Like,
Enter the no. of control points: 3
Enter the control point1:- 20 50
Enter the control point2:- 30 10
Page 28 of 36
CSC3224: Computer Graphics Lab Manual



Enter the control point3:- 40 50
Page 29 of 36
CSC3224: Computer Graphics Lab Manual



Algorithm: Bezier Curve

1. Get Three control points say A(x A , yA ), B(x B , yB), C(xC , yC)
2. Divide the curves represented by point A,B,C
x AB = (xA + xB ) / 2
y AB = (yA + yB ) / 2
xBC = (xB+ xC ) / 2
yBC = (yB+ yC ) / 2
xABC = (xAB + xBC ) / 2
yABC = (yAB + yBC ) / 2
3. Repeat the step 2 for section A AB, ABC
4. Repeat step 3 until we have sections so short that they can be replaced by straight lines
5. Replace small sections by straight lines
6. Stop


Post Lab Assignment:
Now implement the above curve by getting control points from mouse click that means each mouse click will
supply the coordinates of the control points.


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 30 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 12: Lighting in OpenGL


Objective:

1. Understand how real-world lighting conditions are approximated by OpenGL
2. Render illuminated objects by defining light source, material, and lighting model properties


Reference:

1. OpenGL Programming Guide, Red Book


Prerequisite:

Knowledge of:

Basic Lighting and shading


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 31 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:
You have to design a program in which you have to draw a solid sphere and place three light sources from three
directions and those colors should be Red, Green and Blue.


Example code: A Lit Sphere

void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(1.0, 20, 16);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Page 32 of 36
CSC3224: Computer Graphics Lab Manual



Post Lab Assignment:

1. Implement a light source into a triangle and rectangle.


Evaluation Policy:

Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 33 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 13: Color in OpenGL


Objective:

To study and Implement Colors and shading, using color index in OpenGL


Reference:

1. OpenGL Programming Guide, Red book


Prerequisite:

Knowledge of:

Color model, color space


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 34 of 36
CSC3224: Computer Graphics Lab Manual



Problem Description:
You have to design a program which will produce colored rectangles of variation of RGB colors with all
possible combination of RGB within a range of .05


Example code: Smooth Shaded Triangle

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void triangle(void)
{ glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(5.0, 5.0);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(25.0, 5.0);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(5.0, 25.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
triangle();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 30.0, 0.0, 30.0*(GLfloat) h/(GLfloat) w);
else
gluOrtho2D(0.0, 30.0*(GLfloat) w/(GLfloat) h, 0.0, 30.0);
glMatrixMode(GL_MODELVIEW);
}


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?
Page 35 of 36
CSC3224: Computer Graphics Lab Manual




Lab Assignment 14: Animation


Objective:

To implement a program with animation of objects (segments) obtained by scan conversion.


Reference:

1. Xiang and Plastock , Schaum's Outline Computer Graphics, Second Edition


Prerequisite:

Knowledge of:

All Raster Scan Algorithms
o Segmentation and its properties


Academic Honesty:


All work that you do toward fulfillment of this course's expectations must be your own unless collaboration is
explicitly allowed (e.g., by some problem set or the final project). Viewing or copying another individual's work
(even if left by a printer, stored in an executable directory, or accidentally shared in the course's virtual
classroom) or lifting material from a book, magazine, website, or other sourceeven in partand presenting it
as your own constitutes academic dishonesty, as does showing or giving your work, even in part, to another
student.
Similarly is dual submission academic dishonesty: you may not submit the same or similar work to this course
that you have submitted or will submit to another. Nor may you provide or make available your or other
students' solutions to individuals who take or may take this course in the future.
You are welcome to discuss the course's material with others in order to better understand it. You may even
discuss problem sets with classmates, but you may not share code. You may also turn to the Web for instruction
beyond the course's lectures and sections, for references, and for solutions to technical difficulties, but not for
outright solutions to problems on projects. However, failure to cite (as with comments) the origin of any code or
technique that you do discover outside of the course's lectures and sections (even while respecting these
constraints) and then integrate into your own work may be considered academic dishonesty.
All forms of academic dishonesty are dealt with harshly.
Page 36 of 36
CSC3224: Computer Graphics Lab Manual



Description:

Animation:

Sequences of pictures at educate or explain may require images of 3D objects. Although animation uses
graphics as much for art as for realism, it depends heavily on motions to substitute for realism of an individual
image. Animation is done by photographing a sequence of drawings, each slightly different from the previous.
This can be achieved by segmentation.

For example to show a person moving his arm, a series of drawings is photographed, each drawing showing the
arm at a different position. When the images are displayed one after another from the frame buffer, we perceive
the arm as moving through the sequence.


Post Lab Assignment:


1. How renaming operations of segments is useful for animation?
2. What do you mean by posting and unposting of segments?
3. Explain the use of display and segmentation in graphics.


Evaluation Policy:


Your code will be evaluated along the following axes.
Correctness. To what extent is your code consistent with our specifications and free of bugs?
Design. To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?
Style. To what extent is your code readable (commented and indented with variables aptly named)?

Вам также может понравиться