Computer Graphics Lab Report
Computer Graphics Lab Report
Computer Graphics Lab Report
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
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
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
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 ] ;
int N = 0;
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) ;
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
}
4
1.1 Input and respective output for Serpinski Gasket algorithm
5
2 Write a porgram to demonstrate Liang Barsky line clip-
ping algorithm
INPUT(To be specified by user) : None
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
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 ;
}
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 ( ) ;
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 ) ;
}
8
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.
INPUT(To be specified by user) : None
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 ( ) ;
}
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 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 ) ;
}
return 0;
}
12
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
INPUT(To be specified by user) : The angle of rotation
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}
};
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 ( ) ;
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 ( ) ;
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 ) ;
}
16
4.1 Output: Rotated house figure.
17
5 Write a program to implement the Cohen-Sutherland
line-clipping algorithm.
INPUT: coordinates of starting and ending point.
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>
#d e f i n e outcode i n t
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 ;
r e t u r n code ;
}
outcode0 = ComputeOutcode ( x0 , y0 ) ;
outcode1 = ComputeOutcode ( x1 , y1 ) ;
do
{
i f ( ! ( outcode0 | outcode1 ) )
{
accept = true ;
18
done = t r u e ;
}
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 ) ;
void display ( )
{
g l C l e a r (GL COLOR BUFFER BIT ) ;
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 ( ) ;
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 ) ;
}
return 0;
}
21
5.1 output of Cohen-Sutherland line clipping algorithm
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
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 ( ) ;
}
23
}
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);
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 ) ;
}
return 0;
}
25
6.1 output of extruding a circle and 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
#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 ( ) ;
}
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 };
gl Ortho (−winHt ∗ 64 / 4 8 . 0 ,
winHt ∗ 64 / 4 8 . 0 , −winHt ,
winHt , 0 . 1 , 1 0 0 . 0 ) ;
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 ( ) ;
}
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
#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 ( ) ;
}
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 } ;
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);
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 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 ( ) ;
}
return 0;
}
33
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.
INPUT(To be specified by user) : None
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 ;
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;
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 ) ;
}
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
#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 ) ;
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 ( ) ;
}
return 0;
}
40
10.1 output of a rectangular mesh generated using 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