Computer Graphics Lab Report

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Computer graphics laboratory report

Adulla Sai Teja Reddy

Submited on November 12th, 2018

Student ID = 2015IEN01
Semester = VII
Program = Computer Science and Technology
——————————————————————————
Academic year : 2018-19
Submitted as supplemntary writing for the lab work done in subject of
Computer Graphics
Subject faculty : Mrs. Asha Lata

Credits = 3 Maximum marks =


Marks Scored =
Contents
1 Write a program to recursively subdivide a tetrahedron to from 3D Sierpinski
gasket. 2
1.1 Input and respective output for Serpinski Gasket algorithm . . . . . . . . . . . . . . 5

2 Write a porgram to demonstrate Liang Barsky line clipping algorithm 6


2.1 Output of Liang-Barsky line clipping algorithm . . . . . . . . . . . . . . . . . . . . 9

3 Write a program to draw a color cube and spin it using OpenGL transformation
matrices. 10
3.1 Color cube generated and made to spin using OpenGL functions . . . . . . . . . . . 13

4 Write a program to create a house like figure and rotate it about a given fixed
point using OpenGL functions 14
4.1 Output: Rotated house figure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

5 Write a program to implement the Cohen-Sutherland line-clipping algorithm. 18


5.1 output of Cohen-Sutherland line clipping algorithm . . . . . . . . . . . . . . . . . . 22

6 Write a program to create a cylinder and a parallelepiped by extruding a circle


and quadrilateral respectively. 23
6.1 output of extruding a circle and rectangle. . . . . . . . . . . . . . . . . . . . . . . . 26

7 Write a program using OpenGL functions, to draw a simple shaded scene con-
sisting of a tea pot on a table. Define suitably the position and properties of
the light source along with the properties of the properties of the surfaces of
the solid object used in the scene. 27
7.1 output of using a shader to render a scene . . . . . . . . . . . . . . . . . . . . . . . 30

8 Write a program to draw a color cube and allow the user to move the camera
suitably to experiment with perspective viewing. Use OpenGL functions 31
8.1 output of spinning a cube with perspective shift . . . . . . . . . . . . . . . . . . . . 34

9 write a program to fill any given polygon using scan-line area filling algorithm.
Make use of appropriate data structures. 35
9.1 output of filling a ploygon using scan line algorithm . . . . . . . . . . . . . . . . . . 38

10 Write a program to display a set of values fij as a rectangular mesh. 39


10.1 output of a rectangular mesh generated using fij values . . . . . . . . . . . . . . . . 41

1
1 Write a program to recursively subdivide a tetrahedron
to from 3D Sierpinski gasket.
INPUT(To be specified by user) : Number of recursive steps to subdivide
J

the tetrahedron
OUTPUT : A serpinski gasket.
J

#i n c l u d e <i o s t r e a m >
#i n c l u d e <GL/ g l u t . h>

u s i n g namespace s t d ;

typedef f l o a t point [ 3 ] ;

point v [ ] = {{0.0 , 0.0 , 1.0} ,


{ 0 . 0 , 0 . 9 4 2 8 0 9 , −0.333333} ,
{ −0.816497 , −0.471405 , −0.333333} ,
{ 0 . 8 1 6 4 9 7 , −0.471405 , −0.333333}
};

int N = 0;

void t r i a n g l e ( point a , point b , point c )


{
g l B e g i n (GL POLYGON) ;
glNormal3fv ( a ) ;
glVertex3fv (a ) ;
glVertex3fv (b ) ;
glVertex3fv ( c ) ;
glEnd ( ) ;
}

v o i d d i v i d e t r i a n g l e ( p o i n t a , p o i n t b , p o i n t c , i n t m)
{
p o i n t v1 , v2 , v3 ;
i f (m > 0 )
{
f o r ( i n t i = 0 ; i < 3 ; i ++)
{
v1 [ i ] = ( a [ i ] + b [ i ] ) / 2 ;
v2 [ i ] = ( a [ i ] + c [ i ] ) / 2 ;
v3 [ i ] = ( b [ i ] + c [ i ] ) / 2 ;
}
d i v i d e t r i a n g l e ( a , v1 , v2 , m − 1 ) ;
d i v i d e t r i a n g l e ( c , v2 , v3 , m − 1 ) ;
d i v i d e t r i a n g l e ( b , v3 , v1 , m − 1 ) ;
}

2
else
{
triangle (a , b , c );
}
}

v o i d t e t r a h e d r o n ( i n t m)
{
glColor3f (1.0 , 0.0 , 0.0);
d i v i d e t r i a n g l e ( v [ 0 ] , v [ 1 ] , v [ 2 ] , m) ;

glColor3f (0.0 , 1.0 , 0.0);


d i v i d e t r i a n g l e ( v [ 3 ] , v [ 2 ] , v [ 1 ] , m) ;

glColor3f (0.0 , 0.0 , 1.0);


d i v i d e t r i a n g l e ( v [ 0 ] , v [ 3 ] , v [ 1 ] , m) ;

glColor3f (0.0 , 0.0 , 0.0);


d i v i d e t r i a n g l e ( v [ 0 ] , v [ 2 ] , v [ 3 ] , m) ;
}

void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ;
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glLoadIdentity ( ) ;

t e t r a h e d r o n (N) ;

glFlush ( ) ;
}

v o i d myReshape ( i n t w, i n t h )
{
g l V i e w p o r t ( 0 , 0 , w, h ) ;
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
i f (w <= h ){
gl Ortho ( −2.0 , 2 . 0 , −2.0 ∗
( G Lfloat ) h / ( GLfl oat ) w, 2 . 0 ∗
( G Lfloat ) h /
( G Lfloat ) w, −10.0 , 1 0 . 0 ) ;

else{
glOrtho ( −2.0 ∗
( G Lfloat ) w / ( GLfloa t ) h , 2 . 0 ∗ ( GLfloat ) w
/ ( GL float ) h , −2.0 , 2 . 0 , −10.0 , 1 0 . 0 ) ;

3
}

glMatrixMode (GL MODELVIEW) ;


glutPostRedisplay ( ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{
c o ut << ” Enter number o f r e c u r s i v e s t e p s ” << e n d l ;
c i n >> N;

g l u t I n i t (& argc , argv ) ;


g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB | GLUT DEPTH) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” S i e r p i n s k i Gasket ” ) ;
glutFullScreen ( ) ;
glutReshapeFunc ( myReshape ) ;
glutDisplayFunc ( display ) ;
g l E n a b l e (GL DEPTH TEST ) ;
glutMainLoop ( ) ;
return 0;
}

4
1.1 Input and respective output for Serpinski Gasket algorithm

Enter number of Recursive steps:


4

Figure 1: Output of Serpinski Gasket algoritm

5
2 Write a porgram to demonstrate Liang Barsky line clip-
ping algorithm
INPUT(To be specified by user) : None
J

OUTPUT : Two windows showing the unclipped and clipped line


J

respectively

#i n c l u d e <s t d i o . h>
#i n c l u d e <GL/ g l u t . h>

#d e f i n e t r u e 1
#d e f i n e f a l s e 0

do ub le xmin = 5 0 , xmax = 1 0 0 , ymax = 1 0 0 , ymin = 5 0 ;


do ub le xvmin = 2 0 0 , yvmin = 2 0 0 , xvmax = 3 0 0 , yvmax = 3 0 0 ;

i n t c l i p t e s t ( double p , double q , double ∗ t1 , double ∗ t 2 )


{
do uble t = q / p ;
i f (p < 0.0)
{
i f ( t > ∗ t1 )
∗ t1 = t ;
i f ( t > ∗ t2 )
return f a l s e ;
}
e l s e i f ( p >0.0)
{
i f ( t < ∗ t2 )
∗ t2 = t ;
i f ( t < ∗ t1 )
return f a l s e ;
}
e l s e i f ( p == 0 . 0 )
i f (q < 0.0)
return f a l s e ;
return true ;
}

v o i d l i a n g ( double x0 , double y0 , double x1 , double y1 )


{
do uble dx = x1 − x0 , dy = y1 − y0 , t c = 0 . 0 , t 1 = 1 . 0 ;
i f ( c l i p t e s t (−dx , x0 − xmin , &tc , &t 1 ) )
i f ( c l i p t e s t ( dx , xmax − x0 , &tc , &t 1 ) )
i f ( c l i p t e s t (−dy , y0 − ymin , &tc , &t 1 ) )
i f ( c l i p t e s t ( dy , ymax − y0 , &tc , &t 1 ) )
{

6
if ( t1 < 1 . 0 )
{
x1 = x0 + t 1 ∗ dx ;
y1 = y0 + t 1 ∗ dy ;
}
if ( tc > 0.0)
{
x0 = x0 + t c ∗ dx ;
y0 = y0 + t c ∗ dy ;
}

do uble sx = ( xvmax − xvmin ) / ( xmax − xmin ) ;


do uble sy = ( yvmax − yvmin ) / ( ymax − ymin ) ;
do uble vx0 = xvmin + ( x0 − xmin ) ∗ sx ;
do uble vy0 = yvmin + ( y0 − ymin ) ∗ sy ;
do uble vx1 = xvmin + ( x1 − xmin ) ∗ sx ;
do uble vy1 = yvmin + ( y1 − ymin ) ∗ sy ;

glColor3f (1.0 , 0.0 , 0.0);


g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( xvmin , yvmin ) ;
g l V e r t e x 2 f ( xvmax , yvmin ) ;
g l V e r t e x 2 f ( xvmax , yvmax ) ;
g l V e r t e x 2 f ( xvmin , yvmax ) ;
glEnd ( ) ;
glColor3f (0.0 , 0.0 , 1.0);
g l B e g i n ( GL LINES ) ;
g l V e r t e x 2 d ( vx0 , vy0 ) ;
g l V e r t e x 2 d ( vx1 , vy1 ) ;
glEnd ( ) ;
}
}

void display ( )
{
double x0 = 6 0 , y0 = 2 0 , x1 = 8 0 , y1 = 1 2 0 ;
g l C l e a r (GL COLOR BUFFER BIT ) ;
glColor3f (1.0 , 0.0 , 0.0);

g l B e g i n ( GL LINES ) ;
g l V e r t e x 2 d ( x0 , y0 ) ;
g l V e r t e x 2 d ( x1 , y1 ) ;
glEnd ( ) ;

glColor3f (0.0 , 0.0 , 1.0);

g l B e g i n (GL LINE LOOP ) ;


g l V e r t e x 2 f ( xmin , ymin ) ;
g l V e r t e x 2 f ( xmax , ymin ) ;

7
g l V e r t e x 2 f ( xmax , ymax ) ;
g l V e r t e x 2 f ( xmin , ymax ) ;
glEnd ( ) ;

l i a n g ( x0 , y0 , x1 , y1 ) ;
glFlush ( ) ;
}

void myinit ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glColor3f (1.0 , 0.0 , 0.0);
glPointSize (1.0);
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
gluOrtho2D ( 0 . 0 , 4 9 9 . 0 , 0 . 0 , 4 9 9 . 0 ) ;
}

i n t main ( i n t argc , ch ar ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” Liang Barksy Line C l i p p i n g a l g o r i t h m ” ) ;
glutDisplayFunc ( display ) ;
glutFullScreen ( ) ;
myinit ( ) ;
glutMainLoop ( ) ;
return 0;
}

8
2.1 Output of Liang-Barsky line clipping algorithm

Figure 2: Output of Liang Barksy line clipping algorithm

9
3 Write a program to draw a color cube and spin it using
OpenGL transformation matrices.
INPUT(To be specified by user) : None
J

OUTPUT : A color cube which spins by click events


J

#i n c l u d e <i o s t r e a m >
#i n c l u d e <GL/ g l u t . h>

u s i n g namespace s t d ;

typedef f l o a t point [ 3 ] ;

GLfloat v e r t i c e s [ ] [ 3 ] = {
{ −1.0 , −1.0 , −1.0} ,
{ 1.0 , −1.0 , −1.0} ,
{ 1.0 , 1.0 , −1.0} ,
{ −1.0 , 1.0 , −1.0} ,
{ −1.0 , −1.0 , 1.0} ,
{ 1.0 , −1.0 , 1.0} ,
{ 1.0 , 1.0 , 1.0} ,
{ −1.0 , 1.0 , 1.0}
};

GLfloat c o l o r s [ ] [ 3 ] = {
{0.0 , 0.0 , 0.0} ,
{1.0 , 0.0 , 0.0} ,
{1.0 , 1.0 , 0.0} ,
{0.0 , 1.0 , 0.0} ,
{0.0 , 0.0 , 1.0} ,
{1.0 , 0.0 , 1.0} ,
{1.0 , 1.0 , 1.0} ,
{0.0 , 1.0 , 1.0}
};

v o i d polygon ( i n t a , i n t b , i n t c , i n t d )
{
g l B e g i n (GL POLYGON) ;
glColor3fv ( colors [ a ] ) ;
glVertex3fv ( vertices [ a ] ) ;

glColor3fv ( colors [ b ] ) ;
glVertex3fv ( vertices [ b ] ) ;

10
glColor3fv ( colors [ c ] ) ;
glVertex3fv ( vertices [ c ] ) ;

glColor3fv ( colors [ d ] ) ;
glVertex3fv ( vertices [ d ] ) ;
glEnd ( ) ;
}

void colorcube ( void )


{
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);
}

s t a t i c G L float t h e t a [ ] = { 0 . 0 , 0 . 0 , 0 . 0 } ;
s t a t i c GLint a x i s = 2 ;

void display ( void )


{
g l C l e a r (GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ;
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glLoadIdentity ( ) ;
glRotatef ( theta [ 0 ] , 1.0 , 0.0 , 0 . 0 ) ;
glRotatef ( theta [ 1 ] , 0.0 , 1.0 , 0 . 0 ) ;
glRotatef ( theta [ 2 ] , 0.0 , 0.0 , 1 . 0 ) ;
colorcube ( ) ;
glFlush ( ) ;
glutSwapBuffers ( ) ;
}

void spincube ( )
{
t h e t a [ a x i s ] += 2 . 0 ;
i f ( theta [ axis ] > 360.0)
t h e t a [ a x i s ] −= 3 6 0 ;
glutPostRedisplay ( ) ;
}
v o i d mouse ( i n t btn , i n t s t a t e , i n t x , i n t y )
{
i f ( btn == GLUT LEFT BUTTON && s t a t e == GLUT DOWN)
axis = 0;
i f ( btn == GLUT MIDDLE BUTTON && s t a t e == GLUT DOWN)
axis = 1;
i f ( btn == GLUT RIGHT BUTTON && s t a t e == GLUT DOWN)
axis = 2;

11
spincube ( ) ;
}

v o i d myReshape ( i n t w, i n t h )
{
g l V i e w p o r t ( 0 , 0 , w, h ) ;
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;

i f (w <= h ){
glOrtho ( −2.0 , 2 . 0 , −2.0 ∗
( G Lfloat ) h / ( GLfl oat ) w, 2 . 0 ∗ (
GLfl oat ) h / ( G Lfloat ) w, −10.0 , 1 0 . 0 ) ;
}

else{
glOrtho ( −2.0 ∗ (
GLfl oat ) w / ( GL float ) h , 2 . 0 ∗
( GLfloat ) w / ( GLflo at ) h , −2.0 , 2 . 0 , −10.0 , 1 0 . 0 ) ;
}

glMatrixMode (GL MODELVIEW) ;


glutPostRedisplay ( ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT DOUBLE | GLUT RGB | GLUT DEPTH) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” Cube Spin ” ) ;
glutFullScreen ( ) ;
glutReshapeFunc ( myReshape ) ;
glutDisplayFunc ( display ) ;
glutIdleFunc ( spincube ) ;
glutMouseFunc ( mouse ) ;
g l E n a b l e (GL DEPTH TEST ) ;
glutMainLoop ( ) ;

return 0;
}

12
3.1 Color cube generated and made to spin using OpenGL functions

Figure 3: A snapshot of the cube spinning

13
4 Write a program to create a house like figure and rotate
it about a given fixed point using OpenGL functions
INPUT(To be specified by user) : The angle of rotation
J

OUTPUT : A rotated figure of house compared to a house situated


J

at 0◦ angle

#i n c l u d e <i o s t r e a m >
#i n c l u d e <math . h>
#i n c l u d e <GL/ g l u t . h>

u s i n g namespace s t d ;

G L f l o a t house [ 3 ] [ 9 ] = {
{100.0 , 100.0 , 175.0 , 250.0 , 250.0 , 150.0 , 150.0 , 200.0 , 200.0} ,
{100.0 , 300.0 , 400.0 , 300.0 , 100.0 , 100.0 , 150.0 , 150.0 , 100.0} ,
{1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0 , 1.0}
};

GLfloat r o t m a t [ 3 ] [ 3 ] = {{0} , {0} , { 0 } } ;


GLfloat r e s u l t [ 3 ] [ 9 ] = {{0} , {0} , { 0 } } ;
GLfloat h = 100.0;
GLfloat k = 100.0;
GLfloat theta ;

void multiply ( )
{
int i , j , l ;
for ( int i = 0; i < 3 ; i ++)
for ( int j = 0; j < 9 ; j ++)
{
result [ i ] [ j ] = 0;
for ( int l = 0 ; l < 3 ; l ++)
result [ i ] [ j ] = r e s u l t [ i ] [ j ] + r o t m a t [ i ] [ l ] ∗ house [ l ] [ j ] ;
}
}

void r o t a t e ( )
{
G Lfl oat m, n ;
m = −h ∗ ( c o s ( t h e t a ) −1) + k ∗ ( s i n ( t h e t a ) ) ;
n = −k ∗ ( c o s ( t h e t a ) −1) − h ∗ ( s i n ( t h e t a ) ) ;
rot mat [ 0 ] [ 0 ] = cos ( theta ) ;
r o t m a t [ 0 ] [ 1 ] = −s i n ( t h e t a ) ;
r o t m a t [ 0 ] [ 2 ] = m;
rot mat [ 1 ] [ 0 ] = s i n ( theta ) ;
rot mat [ 1 ] [ 1 ] = cos ( theta ) ;

14
rot mat [ 1 ] [ 2 ] = n;
rot mat [ 2 ] [ 0 ] = 0;
rot mat [ 2 ] [ 1 ] = 0;
rot mat [ 2 ] [ 2 ] = 1;
multiply ( ) ;
}
v o i d drawhouse ( ) {
glColor3f (0.0 ,0.0 ,1.0);
g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( house [ 0 ] [ 0 ] , house [1][0]);
g l V e r t e x 2 f ( house [ 0 ] [ 1 ] , house [1][1]);
g l V e r t e x 2 f ( house [ 0 ] [ 3 ] , house [1][3]);
g l V e r t e x 2 f ( house [ 0 ] [ 4 ] , house [1][4]);
glEnd ( ) ;

glColor3f (1.0 ,0.0 ,0.0);


g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( house [ 0 ] [ 5 ] , house [1][5]);
g l V e r t e x 2 f ( house [ 0 ] [ 6 ] , house [1][6]);
g l V e r t e x 2 f ( house [ 0 ] [ 7 ] , house [1][7]);
g l V e r t e x 2 f ( house [ 0 ] [ 8 ] , house [1][8]);
glEnd ( ) ;

glColor3f (0.0 ,0.0 ,1.0);


g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( house [ 0 ] [ 1 ] , house [ 1 ] [ 1 ] ) ;
g l V e r t e x 2 f ( house [ 0 ] [ 2 ] , house [ 1 ] [ 2 ] ) ;
g l V e r t e x 2 f ( house [ 0 ] [ 3 ] , house [ 1 ] [ 3 ] ) ;
glEnd ( ) ;
}

void drawrotatedhouse ( )
{
glColor3f (0.0 ,0.0 ,1.0);
g l B e g i n (GL LINE LOOP ) ;
glVertex2f ( result [0][0] , result [1][0]);
glVertex2f ( result [0][1] , result [1][1]);
glVertex2f ( result [0][3] , result [1][3]);
glVertex2f ( result [0][4] , result [1][4]);
glEnd ( ) ;

glColor3f (1.0 ,0.0 ,0.0);


g l B e g i n (GL LINE LOOP ) ;
glVertex2f ( result [0][5] , result [1][5]);
glVertex2f ( result [0][6] , result [1][6]);
glVertex2f ( result [0][7] , result [1][7]);
glVertex2f ( result [0][8] , result [1][8]);
glEnd ( ) ;

15
glColor3f (0.0 ,0.0 ,1.0);
g l B e g i n (GL LINE LOOP ) ;
glVertex2f ( result [ 0 ] [ 1 ] , result [ 1 ] [ 1 ] ) ;
glVertex2f ( result [ 0 ] [ 2 ] , result [ 1 ] [ 2 ] ) ;
glVertex2f ( result [ 0 ] [ 3 ] , result [ 1 ] [ 3 ] ) ;
glEnd ( ) ;
}

void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT ) ;
drawhouse ( ) ;
rotate ( ) ;
drawrotatedhouse ( ) ;
glFlush ( ) ;
}

void myinit ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glColor3f (1.0 , 1.0 , 0.0);
glPointSize (10.0);
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
gluOrtho2D ( 0 . 0 , 4 9 9 . 0 , 0 . 0 , 4 9 9 . 0 ) ;
}

i n t main ( i n t argc , ch ar ∗∗ argv )


{
c o ut << ” Enter r o t a t i o n a n g l e ” << e n d l ;
c i n >> t h e t a ;
theta = theta ∗ 3.14 / 180.0;

g l u t I n i t (& argc , argv ) ;


g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (800 , 800);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” House r o t a t i o n ” ) ;
glutDisplayFunc ( display ) ;
myinit ( ) ;
glutMainLoop ( ) ;
}

16
4.1 Output: Rotated house figure.

Enter Rotation angle:


45◦

Figure 4: Output of rotating the house by angle specified by user

17
5 Write a program to implement the Cohen-Sutherland
line-clipping algorithm.
INPUT: coordinates of starting and ending point.
J

OUTPUT :Two windows representing the clipping of line specified


J

by user. Window 1 shows the line passing through it and window


2 shows the clipped line.

#i n c l u d e <s t d i o . h>
#i n c l u d e <GL/ g l u t . h>

#d e f i n e outcode i n t

do ub le xmin = 5 0 , ymin = 5 0 , xmax = 1 0 0 , ymax = 1 0 0 ;

do ub le xvmin = 2 0 0 , yvmin = 2 0 0 , xvmax = 3 0 0 , yvmax=300;

c o n s t i n t RIGHT = 8 ;
c o n s t i n t LEFT = 2;
c o n s t i n t TOP = 4;
c o n s t i n t BOTTOM = 1 ;
i n t x1 , x2 , y1 , y2 ;

out cod e ComputeOutcode ( double x , double y )


{
ou tcode code = 0 ;
i f ( y > ymax ) code |= TOP;
e l s e i f ( y < ymin ) code |= BOTTOM;

i f ( x > xmax ) code |= RIGHT;


e l s e i f ( x < xmin ) code |= LEFT ;

r e t u r n code ;
}

v o i d CohenSutherlandLineClipAndDraw ( double x0 , double y0 , double x1 , double y1 )


{
ou tcode outcode0 , outcode1 , outcodeOut ;
b o o l a c c e p t = f a l s e , done = f a l s e ;

outcode0 = ComputeOutcode ( x0 , y0 ) ;
outcode1 = ComputeOutcode ( x1 , y1 ) ;
do
{
i f ( ! ( outcode0 | outcode1 ) )
{
accept = true ;

18
done = t r u e ;
}

e l s e i f ( outcode0 & outcode1 )


{
done = t r u e ;
}
else
{
double x , y ;
outcodeOut = outcode0 ? outcode0 : outcode1 ;

i f ( outcodeOut & TOP)


{
x = x0 + ( x1 − x0 ) ∗ ( ymax − y0 ) / ( y1 − y0 ) ;
y = ymax ;
}

e l s e i f ( outcodeOut & BOTTOM)


{
x = x0 + ( x1 − x0 ) ∗ ( ymin − y0 ) / ( y1 − y0 ) ;
y = ymin ;
}

e l s e i f ( outcodeOut & RIGHT)


{
y = y0 + ( y1 − y0 ) ∗ ( xmax − x0 ) / ( x1 − x0 ) ;
x = xmax ;
}

else
{
y = y0 + ( y1 − y0 ) ∗ ( xmin − x0 ) / ( x1 − x0 ) ;
x = xmin ;
}

i f ( outcodeOut == outcode0 )
{
x0 = x ;
y0 = y ;
outcode0 = ComputeOutcode ( x0 , y0 ) ;
}
else
{
x1 = x ;
y1 = y ;
outcode1 = ComputeOutcode ( x1 , y1 ) ;
}
}

19
} w h i l e ( ! done ) ;

i f ( accept )
{
double sx = ( xvmax − xvmin ) / ( xmax − xmin ) ;
double sy = ( yvmax − yvmin ) / ( ymax − ymin ) ;

double vx0 = xvmin + ( x0 − xmin ) ∗ sx ;


double vy0 = yvmin + ( y0 − ymin ) ∗ sy ;
double vx1 = xvmin + ( x1 − xmin ) ∗ sx ;
double vy1 = yvmin + ( y1 − ymin ) ∗ sy ;

glColor3f (1.0 , 0.0 , 0.0);


g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( xvmin , yvmin ) ;
g l V e r t e x 2 f ( xvmax , yvmin ) ;
g l V e r t e x 2 f ( xvmax , yvmax ) ;
g l V e r t e x 2 f ( xvmin , yvmax ) ;
glEnd ( ) ;

glColor3f (0.0 , 0.0 , 1.0);


g l B e g i n ( GL LINES ) ;
g l V e r t e x 2 d ( vx0 , vy0 ) ;
g l V e r t e x 2 d ( vx1 , vy1 ) ;
glEnd ( ) ;
}
}

void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT ) ;

glColor3f (1.0 , 0.0 , 0.0);

g l B e g i n ( GL LINES ) ;
g l V e r t e x 2 d ( x1 , y1 ) ;
g l V e r t e x 2 d ( x2 , y2 ) ;
glEnd ( ) ;

glColor3f (0.0 ,0.0 ,1.0);


g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( xmin , ymin ) ;
g l V e r t e x 2 f ( xmax , ymin ) ;
g l V e r t e x 2 f ( xmax , ymax ) ;
g l V e r t e x 2 f ( xmin , ymax ) ;
glEnd ( ) ;

CohenSutherlandLineClipAndDraw ( x1 , y1 , x2 , y2 ) ;
glFlush ( ) ;

20
}

void myinit ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glColor3f (1.0 , 0.0 , 0.0);
glPointSize (1.0);
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
gluOrtho2D ( 0 . 0 , 5 0 0 . 0 , 0 . 0 , 5 0 0 . 0 ) ;
}

i n t main ( i n t argc , ch ar ∗∗ argv )


{
p r i n t f ( ” Enter end p o i n t s \n ” ) ;
s c a n f (”%d%d%d%d ” , &x1 , &y1 , &x2 , &y2 ) ;
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” Cohen S u t h e r l a n d Line C l i p p i n g Algorithm ” ) ;
glutDisplayFunc ( display ) ;
glutFullScreen ( ) ;
myinit ( ) ;
glutMainLoop ( ) ;

return 0;
}

21
5.1 output of Cohen-Sutherland line clipping algorithm

Enter end points 10 20 200 370

Figure 5: Output showcasing the line clipped using Cohen Sutherland algorithm. The window in
the middle shows the clipped line.

22
6 Write a program to create a cylinder and a parallelepiped
by extruding a circle and quadrilateral respectively.
INPUT(To be specified by user) :NONE
J

OUTPUT : Extruded cylinder and parallelopiped from given circle


J

and quadrilateral

#i n c l u d e <GL/ g l u t . h>

v o i d d r a w p i x e l ( GLint cx , GLint cy )
{
glColor3f (1.0 ,0.0 ,0.0);

g l B e g i n (GL POINTS ) ;
g l V e r t e x 2 i ( cx , cy ) ;
glEnd ( ) ;
}

v o i d p l o t p i x e l s ( GLint h , GLint k , GLint x , GLint y )


{
draw pixel ( x + h, y + k);
d r a w p i x e l (−x + h, y + k);
draw pixel ( x + h , −y + k);
d r a w p i x e l (−x + h , −y + k);

draw pixel ( y + h, x + k);


draw p i x e l (−y + h, x + k);
draw pixel ( y + h , −x + k);
draw p i x e l (−y + h , −x + k);
}

v o i d d r a w c i r c l e ( GLint h , GLint k , GLint r )


{
GLint d = 1 − r , x = 0 , y = r ;
while (y > x)
{
p l o t p i x e l s (h , k , x , y ) ;
i f (d < 0)
d += 2 ∗ x + 3 ;
else
{
d += 2 ∗ ( x − y ) + 5 ;
−−y ;
}
++x ;
}
p l o t p i x e l s (h , k , x , y ) ;

23
}

void draw cylinder ( )


{
GLint xc = 1 0 0 , yc = 1 0 0 , r = 5 0 ;
GLint i , n = 5 0 ;
f o r ( i = 0 ; i < n ; i += 3 )
{
d r a w c i r c l e ( xc , yc + i , r ) ;
}
}

v o i d d r a w r e c t a n g l e ( i n t x1 , i n t y1 , i n t x2 , i n t y2 , i n t x3 , i n t y3 , i n t x4 , i n t
{
glColor3f (0.0 , 1.0 , 0.0);
glPointSize (2.0);

g l B e g i n (GL LINE LOOP ) ;


g l V e r t e x 2 i ( x1 , y1 ) ;
g l V e r t e x 2 i ( x2 , y2 ) ;
g l V e r t e x 2 i ( x3 , y3 ) ;
g l V e r t e x 2 i ( x4 , y4 ) ;
glEnd ( ) ;
}
void d r a w p a r a l l e l e p i p e d ( )
{
i n t x1 = 2 0 0 , y1 = 100;
i n t x2 = 3 0 0 , y2 = 100;
i n t x3 = 3 0 0 , y3 = 175;
i n t x4 = 2 0 0 , y4 = 175;

GLint i , n = 4 0 ;
f o r ( i = 0 ; i < n ; i += 2 )
{
d r a w r e c t a n g l e ( x1 + i , y1 + i ,
x2 + i , y2 + i ,
x3 + i , y3 + i ,
x4 + i , y4 + i
);
}
}

void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT ) ;
glColor3f (1.0 , 0.0 , 0.0);
glPointSize (2.0);

draw cylinder ( ) ;

24
draw parallelepiped ( ) ;

glFlush ( ) ;
}

void i n i t ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glMatrixMode (GL PROJECTION ) ;
gluOrtho2D ( 0 . 0 , 4 9 9 . 0 , 0 . 0 , 4 9 9 . 0 ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (1000 , 1000);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” R e c t a n g u l a r Mesh ” ) ;
glutFullScreen ( ) ;
glutDisplayFunc ( display ) ;
init ();
glutMainLoop ( ) ;

return 0;
}

25
6.1 output of extruding a circle and rectangle.

Figure 6: A cylinder and a parallelopiped created by extruding a circle adn rectangle.

26
7 Write a program using OpenGL functions, 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 properties of the
surfaces of the solid object used in the scene.
INPUT(To be specified by user) : NONE
J

OUTPUT :A simple scene showing a teapot on a table


J

#i n c l u d e <s t d i o . h>
#i n c l u d e <GL/ g l u t . h>

v o i d w a l l ( double t h i c k n e s s )
{
glPushMatrix ( ) ;
glTranslated (0.5 , 0.5 ∗ thickness , 0 . 5 ) ;
glScaled (1.0 , thickness , 1 . 0 ) ;
glutSolidCube ( 1 . 0 ) ;
glPopMatrix ( ) ;
}

v o i d t a b l e L e g ( double t h i c k , double l e n )
{
glPushMatrix ( ) ;
glTranslated (0 , len / 2 , 0);
g l S c a l e d ( thick , len , thick ) ;
glutSolidCube ( 1 . 0 ) ;
glPopMatrix ( ) ;
}

v o i d t a b l e ( double topWid , double topThick , double l e g T h i c k , double l e g L e n )


{
glPushMatrix ( ) ;
g l T r a n s l a t e d ( 0 , legLen , 0 ) ;
g l S c a l e d ( topWid , topThick , topWid ) ;
glutSolidCube ( 1 . 0 ) ;
glPopMatrix ( ) ;

do uble d i s t = 0 . 9 5 ∗ topWid / 2 . 0 − l e g T h i c k / 2 . 0 ;

glPushMatrix ( ) ;
glTranslated ( dist , 0 , dist ) ;
tableLeg ( legThick , legLen ) ;
g l T r a n s l a t e d ( 0 . 0 , 0 . 0 , −2 ∗ d i s t ) ;
tableLeg ( legThick , legLen ) ;
g l T r a n s l a t e d (−2 ∗ d i s t , 0 , 2 ∗ d i s t ) ;

27
tableLeg ( legThick , legLen ) ;
g l T r a n s l a t e d ( 0 , 0 , −2 ∗ d i s t ) ;
tableLeg ( legThick , legLen ) ;
glPopMatrix ( ) ;
}

void d i s p l a y S o l i d ( void )
{
G Lfl oat mat ambient [ ] = {0.7 f , 0.7 f , 0.7 f , 1.0 f };
G Lfl oat m a t d i f f u s e [ ] = {0.5 f , 0.5 f , 0.5 f , 1.0 f };
G Lfl oat m a t s p e c u l a r [ ] = {1.0 f , 1.0 f , 1.0 f , 1.0 f };
G Lfl oat m a t s h i n i n e s s [ ] = {50.0 f };

g l M a t e r i a l f v (GL FRONT, GL AMBIENT, mat ambient ) ;


g l M a t e r i a l f v (GL FRONT, GL DIFFUSE , mat diffuse );
g l M a t e r i a l f v (GL FRONT, GL SPECULAR, m a t s p e c u l a r ) ;
g l M a t e r i a l f v (GL FRONT, GL SHININESS , m a t s h i n i n e s s ) ;
G Lfl oat l i g h t I n t e n s i t y [ ] = {0.7 f , 0.7 f , 0.7 f , 0.7 f };
G Lfl oat l i g h t p o s i t i o n [ ] = {2.0 f , 6.0 f , 3.0 f , 0.0 f };

g l L i g h t f v (GL LIGHT0 , GL POSITION , l i g h t p o s i t i o n ) ;


g l L i g h t f v (GL LIGHT0 , GL DIFFUSE , l i g h t I n t e n s i t y ) ;

glMatrixMode (GL PROJECTION ) ;


glLoadIdentity ( ) ;
do uble winHt = 1 . 0 ;

gl Ortho (−winHt ∗ 64 / 4 8 . 0 ,
winHt ∗ 64 / 4 8 . 0 , −winHt ,
winHt , 0 . 1 , 1 0 0 . 0 ) ;

glMatrixMode (GL MODELVIEW) ;


glLoadIdentity ( ) ;
gluLookAt ( 2 . 3 , 1 . 3 , 2 . 0 , 0 . 0 , 0 . 2 5 , 0 . 0 , 0 . 0 , 1 . 0 , 0 . 0 ) ;
g l C l e a r (GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ;

glPushMatrix ( ) ;
glTranslated (0.4 , 0.4 , 0 . 6 ) ;
glRotated (45 , 0 , 0 , 1 ) ;
glScaled (0.08 , 0.08 , 0.08);
glPopMatrix ( ) ;

glPushMatrix ( ) ;
glTranslated (0.6 , 0.38 , 0 . 5 ) ;
glRotated (30 , 0 , 1 , 0 ) ;
glutSolidTeapot ( 0 . 0 8 ) ;
glPopMatrix ( ) ;

glPushMatrix ( ) ;

28
glTranslated (0.25 , 0.42 , 0.35);
glPopMatrix ( ) ;

glPushMatrix ( ) ;
glTranslated (0.4 , 0 , 0 . 4 ) ;
table (0.6 , 0.02 , 0.02 , 0 . 3 ) ;
glPopMatrix ( ) ;

wall ( 0 . 0 2 ) ;
glPushMatrix ( ) ;
glRotated ( 9 0 . 0 , 0.0 , 0.0 , 1 . 0 ) ;
wall ( 0 . 0 2 ) ;
glPopMatrix ( ) ;

glPushMatrix ( ) ;
g l R o t a t e d ( −90.0 , 1 . 0 , 0 . 0 , 0 . 0 ) ;
wall ( 0 . 0 2 ) ;
glPopMatrix ( ) ;
glFlush ( ) ;
}

i n t main ( i n t argc , ch ar ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE |GLUT RGB|GLUT DEPTH) ;
glutInitWindowSize (640 , 480);
glutInitWindowPosition (100 , 100);
glutCreateWindow ( ” Simple shaded s c e n e c o n s i s t i n g o f a t e a p o t ” ) ;
glutDisplayFunc ( d i s p l a y S o l i d ) ;
g l E n a b l e (GL LIGHTING ) ;
g l E n a b l e (GL LIGHT0 ) ;
glShadeModel (GL SMOOTH) ;
g l E n a b l e (GL DEPTH TEST ) ;
g l E n a b l e (GL NORMALIZE ) ;
glClearColor (0.1 , 0.1 , 0.1 , 0 . 0 ) ;
glViewport (0 , 0 , 640 , 4 8 0 ) ;
glutMainLoop ( ) ;
}

29
7.1 output of using a shader to render a scene

Figure 7: Output of using a simple shader to display a scene contatining a tea pot on a table.

30
8 Write a program to draw a color cube and allow the
user to move the camera suitably to experiment with
perspective viewing. Use OpenGL functions
INPUT(To be specified by user) :Left mouse click event to move
J

the cube. Right mouse click events to shift perspectives


OUTPUT : Different perspectives of a cube moving.
J

#i n c l u d e <i o s t r e a m >
#i n c l u d e <GL/ g l u t . h>

u s i n g namespace s t d ;

typedef f l o a t point [ 3 ] ;

GLfloat v e r t i c e s [ ] [ 3 ] = {
{ −1.0 , −1.0 , −1.0} ,
{ 1.0 , −1.0 , −1.0} ,
{ 1.0 , 1.0 , −1.0} ,
{ −1.0 , 1.0 , −1.0} ,
{ −1.0 , −1.0 , 1.0} ,
{ 1.0 , −1.0 , 1.0} ,
{ 1.0 , 1.0 , 1.0} ,
{ −1.0 , 1.0 , 1.0}
};

GLfloat c o l o r s [ ] [ 3 ] = {
{0.0 , 0.0 , 0.0} ,
{1.0 , 0.0 , 0.0} ,
{1.0 , 1.0 , 0.0} ,
{0.0 , 1.0 , 0.0} ,
{0.0 , 0.0 , 1.0} ,
{1.0 , 0.0 , 1.0} ,
{1.0 , 1.0 , 1.0} ,
{0.0 , 1.0 , 1.0}
};

v o i d polygon ( i n t a , i n t b , i n t c , i n t d )
{
g l B e g i n (GL POLYGON) ;
glColor3fv ( colors [ a ] ) ;
glVertex3fv ( vertices [ a ] ) ;

glColor3fv ( colors [ b ] ) ;

31
glVertex3fv ( vertices [ b ] ) ;

glColor3fv ( colors [ c ] ) ;
glVertex3fv ( vertices [ c ] ) ;

glColor3fv ( colors [ d ] ) ;
glVertex3fv ( vertices [ d ] ) ;
glEnd ( ) ;
}

void colorcube ( void )


{
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);
}

s t a t i c G L float t h e t a [ ] = { 0 . 0 , 0 . 0 , 0 . 0 } ;
s t a t i c GLint a x i s = 2 ;
s t a t i c GLdouble v i e w e r [ ] = { 0 . 0 , 0 . 0 , 5 . 0 } ;

void display ( void )


{
g l C l e a r (GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ;
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glLoadIdentity ( ) ;

gluLookAt ( v i e w e r [ 0 ] , v i e w e r [ 1 ] , v i e w e r [ 2 ] ,
0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0);

glRotatef ( theta [ 0 ] , 1.0 , 0.0 , 0 . 0 ) ;


glRotatef ( theta [ 1 ] , 0.0 , 1.0 , 0 . 0 ) ;
glRotatef ( theta [ 2 ] , 0.0 , 0.0 , 1 . 0 ) ;
colorcube ( ) ;
glFlush ( ) ;
glutSwapBuffers ( ) ;
}

v o i d mouse ( i n t btn , i n t s t a t e , i n t x , i n t y )
{
i f ( btn == GLUT LEFT BUTTON && s t a t e == GLUT DOWN)
axis = 0;
i f ( btn == GLUT MIDDLE BUTTON && s t a t e == GLUT DOWN)
axis = 1;
i f ( btn == GLUT RIGHT BUTTON && s t a t e == GLUT DOWN)
axis = 2;

32
t h e t a [ a x i s ] += 2 . 0 ;
i f ( theta [ axis ] > 360.0)
t h e t a [ a x i s ] −= 3 6 0 ;
display ( ) ;
}

v o i d keys ( u n s i g n e d char key , i n t x , i n t y )


{
i f ( key == ’x ’ ) viewer [ 0 ] −= 1.0;
i f ( key == ’X’ ) viewer [ 0 ] += 1.0;
i f ( key == ’y ’ ) viewer [ 1 ] −= 1.0;
i f ( key == ’Y’ ) viewer [ 1 ] += 1.0;
i f ( key == ’z ’) viewer [ 2 ] −= 1.0;
i f ( key == ’Z ’ ) viewer [ 2 ] += 1.0;
display ( ) ;
}

v o i d myReshape ( i n t w, i n t h )
{
g l V i e w p o r t ( 0 , 0 , w, h ) ;
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
i f (w <= h )
glOrtho ( −2.0 , 2 . 0 , −2.0 ∗ ( G Lfloat ) h / ( GLfl oat ) w, 2 . 0 ∗ ( GLf
else
glOrtho ( −2.0 ∗ ( G Lfloat ) w / ( GLfloa t ) h , 2 . 0 ∗ ( GLfloat ) w / (
glMatrixMode (GL MODELVIEW) ;
glutPostRedisplay ( ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT DOUBLE | GLUT RGB | GLUT DEPTH) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” Cube v i e w e r ” ) ;
glutFullScreen ( ) ;
glutReshapeFunc ( myReshape ) ;
glutDisplayFunc ( display ) ;
glutKeyboardFunc ( keys ) ;
glutMouseFunc ( mouse ) ;
g l E n a b l e (GL DEPTH TEST ) ;
glutMainLoop ( ) ;

return 0;
}

33
8.1 output of spinning a cube with perspective shift

• Left mouse click events to move the cube,


• Right mouse click events to shift the perspective.

Figure 8: A snapshot of moved cube with shifted perspective.

34
9 write a program to fill any given polygon using scan-
line area filling algorithm. Make use of appropriate data
structures.
INPUT(To be specified by user) : None
J

OUTPUT : A polygon filled using scan line algorithm.


J

#i n c l u d e <GL/ g l u t . h>

f l o a t x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ;

void draw pixel ( i n t x , i n t y )


{
glColor3f (1.0 , 0.0 , 0.0);
g l B e g i n (GL POINTS ) ;
glVertex2i (x , y ) ;
glEnd ( ) ;
}

v o i d e d g e d e t e c t ( f l o a t x1 , f l o a t y1 , f l o a t x2 , f l o a t y2 , i n t ∗ l e , i n t ∗ r e )
{
f l o a t mx, temp ;
i f ( y2 < y1 )
{
temp = x1 ; x1 = x2 ; x2 = temp ;
temp = y1 ; y1 = y2 ; y2 = temp ;
}
i f ( y1 != y2 ) mx = ( x2 − x1 ) / ( y2 − y1 ) ;
e l s e mx = ( x2 − x1 ) ;

f l o a t x = x1 ;
f o r ( i n t i = y1 ; i <= y2 ; i ++)
{
i f (x < ( float ) le [ i ]) le [ i ] = ( int ) x ;
i f (x > ( f l o a t ) re [ i ] ) re [ i ] = ( int ) x ;

x += mx ;
}
}

void s c a n f i l l ( )
{
int le [500] , re [ 5 0 0 ] ;
f o r ( i n t i = 0 ; i < 5 0 0 ; i ++)
{
le [ i ] = 500;
re [ i ] = 0;

35
}

edge d e t e c t ( x1 , y1 , x2 , y2 , le , re ) ;
edge d e t e c t ( x2 , y2 , x3 , y3 , le , re ) ;
edge d e t e c t ( x3 , y3 , x4 , y4 , le , re ) ;
edge d e t e c t ( x4 , y4 , x1 , y1 , le , re ) ;

f o r ( i n t y = 0 ; y < 5 0 0 ; y++)
{
i f ( l e [ y ] <= r e [ y ] )
{
f o r ( i n t i = ( i n t ) l e [ y ] ; i < ( i n t ) r e [ y ] ; i ++)
draw pixel ( i , y ) ;
}
}
}

void display ( )
{
x1 = 1 0 0 ; y1 = 100;
x2 = 3 0 0 ; y2 = 100;
x3 = 3 0 0 ; y3 = 400;
x4 = 1 0 0 ; y4 = 400;

g l C l e a r (GL COLOR BUFFER BIT ) ;


glColor3f (0.0 , 0.0 , 0.0);
g l B e g i n (GL LINE LOOP ) ;
g l V e r t e x 2 f ( x1 , y1 ) ;
g l V e r t e x 2 f ( x2 , y2 ) ;
g l V e r t e x 2 f ( x3 , y3 ) ;
g l V e r t e x 2 f ( x4 , y4 ) ;
glEnd ( ) ;

scanfill ();

glFlush ( ) ;
}

void i n i t ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glPointSize (1.0);
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;
gluOrtho2D ( 0 , 4 9 9 . 0 , 0 , 4 9 9 . 0 ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{

36
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (500 , 500);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” Scan Line ” ) ;
glutDisplayFunc ( display ) ;
init ();
glutMainLoop ( ) ;

return 0;
}

37
9.1 output of filling a ploygon using scan line algorithm

Figure 9: Output of a polygon (square) being filled using scan line algorithm

38
10 Write a program to display a set of values fij as a rect-
angular mesh.
INPUT(To be specified by user): None
J

OUTPUT : A rectangular mesh created using fij values.


J

#i n c l u d e <GL/ g l u t . h>

#d e f i n e X MAX 50
#d e f i n e Y MAX 50
#d e f i n e dx 5
#d e f i n e dy 5

G L f l o a t x [X MAX] = { 0 . 0 } ;
G L f l o a t y [X MAX] = { 0 . 0 } ;
GLfloat x s t a r t = 50 , y s t a r t = 50;

void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT ) ;

f o r ( i n t i = 0 ; i < X MAX; i ++) x [ i ] = x s t a r t + i ∗ dx ;


f o r ( i n t i = 0 ; i < Y MAX; i ++) y [ i ] = y s t a r t + i ∗ dy ;

f o r ( i n t i = 0 ; i < X MAX − 1 ; i ++)


{
f o r ( i n t j = 0 ; j < Y MAX − 1 ; j ++)
{
g l B e g i n (GL LINE LOOP ) ;
glVertex2f (x [ i ] , y [ j ]);
glVertex2f (x [ i ] , y [ j + 1]);
glVertex2f (x [ i + 1] , y[ j + 1]);
glVertex2f (x [ i + 1] , y[ j ]);
glEnd ( ) ;
}
}

glFlush ( ) ;
}

void i n i t ( )
{
glClearColor (1.0 , 1.0 , 1.0 , 1 . 0 ) ;
glColor3f (1.0 , 0.0 , 0.0);
glPointSize (5.0);
glMatrixMode (GL PROJECTION ) ;
glLoadIdentity ( ) ;

39
gluOrtho2D ( 0 . 0 , 4 9 9 . 0 , 0 . 0 , 4 9 9 . 0 ) ;
glutPostRedisplay ( ) ;
}

i n t main ( i n t argc , char ∗∗ argv )


{
g l u t I n i t (& argc , argv ) ;
g l u t I n i t D i s p l a y M o d e (GLUT SINGLE | GLUT RGB ) ;
glutInitWindowSize (1000 , 1000);
glutInitWindowPosition (0 , 0 ) ;
glutCreateWindow ( ” R e c t a n g u l a r Mesh ” ) ;
glutFullScreen ( ) ;
glutDisplayFunc ( display ) ;
init ();
glutMainLoop ( ) ;

return 0;
}

40
10.1 output of a rectangular mesh generated using fij values

Figure 10: A mesh created using a set of fij values.

41
List of Figures
1 Output of Serpinski Gasket algoritm . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Output of Liang Barksy line clipping algorithm . . . . . . . . . . . . . . . . . . . . 9
3 A snapshot of the cube spinning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Output of rotating the house by angle specified by user . . . . . . . . . . . . . . . . 17
5 Output showcasing the line clipped using Cohen Sutherland algorithm. The window
in the middle shows the clipped line. . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 A cylinder and a parallelopiped created by extruding a circle adn rectangle. . . . . . 26
7 Output of using a simple shader to display a scene contatining a tea pot on a table. 30
8 A snapshot of moved cube with shifted perspective. . . . . . . . . . . . . . . . . . . 34
9 Output of a polygon (square) being filled using scan line algorithm . . . . . . . . . 38
10 A mesh created using a set of fij values. . . . . . . . . . . . . . . . . . . . . . . . . 41

42

You might also like