0% found this document useful (0 votes)
49 views

CG & V Labmanual

The document contains code for several computer graphics programs: 1. A program that recursively subdivides a tetrahedron to generate a 3D Sierpinski gasket. 2. A program that implements the Liang-Barsky line clipping algorithm to clip and draw lines in OpenGL. 3. A program that draws a color cube and spins it using OpenGL transformation matrices. 4. A program that creates a 3D house model and rotates it about a pivot point using OpenGL functions.

Uploaded by

sri kbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

CG & V Labmanual

The document contains code for several computer graphics programs: 1. A program that recursively subdivides a tetrahedron to generate a 3D Sierpinski gasket. 2. A program that implements the Liang-Barsky line clipping algorithm to clip and draw lines in OpenGL. 3. A program that draws a color cube and spins it using OpenGL transformation matrices. 4. A program that creates a 3D house model and rotates it about a pivot point using OpenGL functions.

Uploaded by

sri kbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 44

CG & Visualization Lab Manual

1. Program to recursively subdivide a tetrahedron to form 3D Sierpinski gasket.


The number of recursive steps is to be specified by user.

#include<stdio.h>
#include<GL/glut.h>
typedef float point[3];

/* initial tetrahedron */
point v[]={{0.0,0.0,1.0},{0.0,0.942809,0.33333},
{-0.816497,-0.471405,0.33333},{0.816497,-0.471405,0.333333}};

int n;
void triangle(point a,point b,point c)
/* Display one triangle using a line loop for wire frame, a single normal for constant
shading, or three normals for interpolative shading */
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

void divide_triangle(point a,point b,point c,int m)


/* Triangle subdivision using vertex numbers righthand rule applied to create outward
pointing faces */
{
point v1,v2,v3;
int j;
if(m>0)
{
for(j=0;j<3;j++) v1[j]=(a[j]+b[j])/2;
for(j=0;j<3;j++) v2[j]=(a[j]+c[j])/2;
for(j=0;j<3;j++) v3[j]=(b[j]+c[j])/2;
divide_triangle(a,v1,v2,m-1);
divide_triangle(c,v2,v3,m-1);
divide_triangle(b,v3,v1,m-1);
}
else triangle(a,b,c);
}

void tetrahedron(int m)
/* Apply triangle subdivision to faces of tetrahedron */
{
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0],v[1],v[2],m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3],v[2],v[1],m);
glColor3f(0.0,0.0,1.0);

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

divide_triangle(v[0],v[3],v[1],m);
glColor3f(0.0,0.0,0.0);
divide_triangle(v[0],v[2],v[3],m);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}

void myReshape(int w,int h)


{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0 *(GLfloat) h/(GLfloat) w,
2.0*(GLfloat) h/ (GLfloat) w, -10.0,10.0);
else
glOrtho(-2.0 *(GLfloat) w/(GLfloat) h,
2.0*(GLfloat) w/ (GLfloat) h, -2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

void main(int argc, char **argv)


{
printf("No. of Divisions ?");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("3d Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

Out put :
Enter number of Divisions?
2

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

2. Program to implement Liang_barsky line clipping algorithm using openGL


Function.

#include<GL/glut.h>
#define false 0
#define true 1
double xmin=50,ymin=50,xmax=100,ymax=100; // Window boundaries
double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport boundaries

int cliptest(double p, double q,double *t1,double *t2)


{
double t=q/p;
if(p<0.0) //potentially entry point, update te
{
if(t>*t1)*t1=t;
if(t> *t2)
return (false); // line portion is outside
}
else
if(p>0.0) // Potentially leaving point, update t1
{
if(t<*t2) *t2=t;
if(t< *t1)
return(false);
}
else
if(p==0.0)
{
if(q<0.0)
return(false);
}
return(true);
}

void LiangBarskyLineClipAndDraw(double x0,double y0,double x1,double y1)


{
double dx=x1-x0,dy=y1-y0,te=0.0,t1=1.0;
if(cliptest(-dx,x0-xmin,&te,&t1)) // inside test wrt left edge
if(cliptest(dx,xmax-x0,&te,&t1)) // inside test wrt right edge
if(cliptest(-dy,y0-ymin,&te,&t1))//inside test wrt bottom edge
if(cliptest(dy,ymax-y0,&te,&t1))//inside test wrt top edge
{
if(t1<1.0)
{
x1=x0+t1*dx;
y1=y0+t1*dy;
}
if(te>0.0)
{

CSE Department, AIT, Bangalore -90 4


CG & Visualization Lab Manual

x0=x0+te*dx;
y0=y0+te*dy;
}
// Window to viewport mapings
double sx=(xvmax-xvmin)/(xmax-xmin); // Scale parameters
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;
// draw a red colored viewport
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();

glColor3f(0.0,0.0,1.0); // draw blue colored clipped line


glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2d(vx1,vy1);
glEnd();
}
} // end of the line clipping
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT); //draw the line with red color
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
// draw a blue colored window
glColor3f(0.0,0.0,1.0);

glBegin(GL_LINE_LOOP);
glVertex2d (x0,y0);
glVertex2d (x1,y1);
glEnd();
glColor3f(0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();

CSE Department, AIT, Bangalore -90 5


CG & Visualization Lab Manual

LiangBarskyLineClipAndDraw(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, 499.0, 0.0, 499.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Liang Barsky Line Clipping Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

Out put :

CSE Department, AIT, Bangalore -90 6


CG & Visualization Lab Manual

3. Program to draw a color cube and spin it using openGL transformation matrices.

#include<GL/glut.h>
GLfloat vertices[][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 colors[][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}};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);

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

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

glEnd();
}

void colorcube(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);
}

static GLfloat theta[]={0.0,0.0,0.0};


static GLint axis=2;

void display(void)
/* Display callback, clear frame buffer and Z buffer, rotate cube and draw, swap buffers
*/
{

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

CSE Department, AIT, Bangalore -90 7


CG & Visualization Lab Manual

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()
/* Idle callback, spin cube 1 degrees about selected axis */
{
theta[axis]+=1.0;
if(theta[axis]>360.0)theta[axis]-=360.0;
/*Display */
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
/* mouse callback, selects an axis about which to rotate */
{

if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) axis=0;


if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=1;
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) axis=2;
}

void myReshape(int w,int h)


{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0 *(GLfloat) h/(GLfloat) w,
2.0*(GLfloat) h/ (GLfloat) w, -10.0,10.0);
else
glOrtho(-2.0 *(GLfloat) w/(GLfloat) h,
2.0*(GLfloat) w/ (GLfloat) h, -2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

void main(int argc, char **argv)


{

glutInit(&argc,argv);
/* need both double buffering and Z buffer */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH);

CSE Department, AIT, Bangalore -90 8


CG & Visualization Lab Manual

glutInitWindowSize(500,500);
glutCreateWindow("Rotating a Color Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);

glEnable(GL_DEPTH_TEST); /* Enable hidden – surface—removal */


glutMainLoop();
}

Out Put:

CSE Department, AIT, Bangalore -90 9


CG & Visualization Lab Manual

4. Program to create a house and rotate about the pivot


point using openGL functions

#include <stdio.h>
#include <math.h>
#include <GL/glut.h>

GLfloat 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 rotatemat[3][3]={{0},
{0},
{0}
};
GLfloat result[3][9]={{0},
{0},
{0}
};
GLfloat arbitrary_x=100.0;
GLfloat arbitrary_y=100.0;
GLfloat rotation_angle;
void multiply()
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k++)
result[i][j]=result[i][j]+rotatemat[i]
[k]*house[k][j];
}
}

void rotate()
{
GLfloat m,n;
theta= rotation_angle *3.1415/180;
m=-arbitrary_x*(cos(theta)-
1)+arbitrary_y*(sin(theta));
n=-arbitrary_y*(cos(theta)-1)-
arbitrary_x*(sin(theta));
rotatemat[0][0]=cos(theta);
rotatemat[0][1]=-sin(theta);
rotatemat[0][2]=m;
rotatemat[1][0]=sin(theta);

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

rotatemat[1][1]=cos(theta);
rotatemat[1][2]=n;
rotatemat[2][0]=0;
rotatemat[2][1]=0;
rotatemat[2][2]=1;
//multiply the two matrices
multiply();
}

void drawhouse()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][0],house[1][0]);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][3],house[1][3]);
glVertex2f(house[0][4],house[1][4]);
glEnd();

glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][6],house[1][6]);
glVertex2f(house[0][7],house[1][7]);
glVertex2f(house[0][8],house[1][8]);
glEnd();

glColor3f(0.0, 0.0, 1.0);


glBegin(GL_LINE_LOOP);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][2],house[1][2]);
glVertex2f(house[0][3],house[1][3]);
glEnd();
}

void drawrotatedhouse()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(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);
glBegin(GL_LINE_LOOP);
glVertex2f(result[0][5],result[1][5]);
glVertex2f(result[0][6],result[1][6]);

CSE Department, AIT, Bangalore -90 11


CG & Visualization Lab Manual

glVertex2f(result[0][7],result[1][7]);
glVertex2f(result[0][8],result[1][8]);
glEnd();

glColor3f(0.0, 0.0, 1.0);


glBegin(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()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2f(100.0,100.0);
glEnd();
drawhouse();
rotate();
drawrotatedhouse();
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,499.0,0.0,499.0);
}

void main(int argc, char** argv)


{

printf("Enter the rotation angle\n");


scanf_s("%f", &rotation_angle);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("house rotation");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

Output:

Enter the Rotation angle: -45

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

5.Program to implement the Cohen-Suderland Line Clipping


Algorithm.
Making provision to specify the input,window for clipping
and viewport for displaying the clipped image.

#include <stdio.h>
#include <GL/glut.h>
#define bool int

#define true 1

#define false 0

#define outcode int


double xmin=50,ymin=50, xmax=100,ymax=100; // Window
boundaries
double xvmin=200,yvmin=200,xvmax=300,yvmax=300; // Viewport
boundaries
//bit codes for the right, left, top, & bottom
const int RIGHT = 8;
const int LEFT = 2;
const int TOP = 4;
const int BOTTOM = 1;

//used to compute bit codes of a point


outcode ComputeOutCode (double x, double y);

//Cohen-Sutherland clipping algorithm clips a line from


//P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with
//diagonal from (xmin, ymin) to (xmax, ymax).
void CohenSutherlandLineClipAndDraw (double x0, double
y0,double x1, double y1)
{
//Outcodes for P0, P1, and whatever point lies outside the
clip rectangle
outcode outcode0, outcode1, outcodeOut;
bool accept = false, done = false;

//compute outcodes
outcode0 = ComputeOutCode (x0, y0);
outcode1 = ComputeOutCode (x1, y1);

do{
if (!(outcode0 | outcode1)) //logical or is
0 Trivially accept & exit
{
accept = true;
done = true;
}
else if (outcode0 & outcode1) //logical and is
not 0. Trivially reject and exit

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

done = true;
else
{
//failed both tests, so calculate the line segment to clip
//from an outside point to an intersection with clip edge
double x, y;

//At least one endpoint is outside the clip rectangle; pick


it.
outcodeOut = outcode0? outcode0: outcode1;

//Now find the intersection point;


//use formulas y=y0+slope*(x-x0),x =x0+(1/slope)*(y-y0)
if (outcodeOut & TOP)
//point is above the clip rectangle
{
x = x0 + (x1 - x0) * (ymax - y0)/(y1 - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM)
//point is below the clip rectangle
{
x = x0 + (x1 - x0) * (ymin - y0)/(y1 - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
//point is to the right of clip rectangle
{
y = y0 + (y1 - y0) * (xmax - x0)/(x1 - x0);
x = xmax;
}
else
//point is to the left of clip rectangle
{
y = y0 + (y1 - y0) * (xmin - x0)/(x1 - x0);
x = xmin;
}

//Now we move outside point to intersection point to clip


//and get ready for next pass.
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode (x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode (x1, y1);

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

}
}
}while (!done);

if (accept)
{ // Window to viewport mappings
double sx=(xvmax-xvmin)/(xmax-xmin);
// Scale parameters
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;
//draw a red colored viewport
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
// draw blue colored clipped line
glBegin(GL_LINES);
glVertex2d (vx0, vy0);
glVertex2d (vx1, vy1);
glEnd();
}
}

//Compute the bit code for a point (x, y) using the clip
rectangle
//bounded diagonally by (xmin, ymin), and (xmax, ymax)
outcode ComputeOutCode (double x, double y)
{
outcode code = 0;
if (y > ymax) //above the clip window
code |= TOP;
else if (y < ymin) //below the clip window
code |= BOTTOM;
if (x > xmax) //to the right of clip window
code |= RIGHT;
else if (x < xmin) //to the left of clip window
code |= LEFT;
return code;
}

void display()
{
double x0=60,y0=20,x1=80,y1=120;

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

glClear(GL_COLOR_BUFFER_BIT);
//draw the line with red color
glColor3f(1.0,0.0,0.0);
//bres(120,20,340,250);
glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();

//draw a blue colored window


glColor3f(0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAndDraw(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,499.0,0.0,499.0);
}

void main(int argc, char** argv)


{
//int x1, x2, y1, y2;
//printf("Enter End points:");
//scanf("%d%d%d%d", &x1,&x2,&y1,&y2);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cohen Suderland Line Clipping
Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

Output:

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

6.Program to create a cylinder and a Parallelepiped by


extruding Circle and Quadrilateral respectively. Allow the
user to specify the circle and quadrilateral.

#include <math.h>
#include <GL/glut.h>
void objects(float cx,float cy,float r,float nums)
{
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
float j;
for(j=0;j<nums;j++)
{
float x,y,theta;
theta=((2.0*3.1415926)*(j))/(nums);
x=r*cos(theta);
y=r*sin(theta);
glVertex2f(x+cx,y+cy);
}
glEnd();
glColor3f(0,1,0);
glBegin(GL_LINE_LOOP); //Rectangle
glVertex2f(0.0,0.0);
glVertex2f(0.7,0.0);
glVertex2f(0.7,0.5);
glVertex2d(0.0,0.5);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0,700,700);
int i;
for(i=0;i<30;i++) //No. of extrudes
{
glTranslated(0.01,0.01,0);
objects(0.4,-0.7,0.3,56);
}
glFlush();
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(700,700);
glutCreateWindow("Cylinder & Paralellepiped");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

CSE Department, AIT, Bangalore -90 1


CG & Visualization Lab Manual

Output:

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

7. Program to draw a simple shaded scene consisting of a


teapot on a table.Define suitably the position and
properties of the light source along with the properties of
the surfaces of the solid object used in the scene, using
openGL functions.

#include<gl/glut.h>
void obj(double tx,double ty,double tz,double sx,double sy,double sz)
{
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(tx,ty,tz);
glScaled(sx,sy,sz);
glutSolidCube(1);
glLoadIdentity();
}
void display()
{
glViewport(0,0,700,700);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
obj(0,0,0.5,1,1,0.04); // three walls
obj(0,-0.5,0,1,0.04,1);
obj(-0.5,0,0,0.04,1,1);
obj(0,-0.3,0,0.02,0.2,0.02); // four table legs
obj(0,-0.3,-0.4,0.02,0.2,0.02);
obj(0.4,-0.3,0,0.02,0.2,0.02);
obj(0.4,-0.3,-0.4,0.02,0.2,0.02);
obj(0.2,-0.18,-0.2,0.6,0.02,0.6); // table top
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(0.3,-0.1,-0.3);
glutSolidTeapot(0.09);
glFlush();
glLoadIdentity();
}
void main()
{
float ambient[]={1,1,1,1};
float light_pos[]={27,80,2,3};
glutInitWindowSize(700,700);
glutCreateWindow("scene");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

}
Output:

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

8.Program to draw a color cube and allow the user to move


the camera suitably to experiment with perspective viewing
using OpenGL functions

/* We use the Lookat function in the display callback to


point
the viewer, whose position can be altered by the x,X,y,Y,z,
and Z keys.
The perspective view is set in the reshape callback */

#include <stdlib.h>
#include <GL/glut.h>

GLfloat vertices[][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 colors[][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}};

void polygon(int a, int b, int c , int d)


{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();

void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}

static GLfloat theta[] = {0.0,0.0,0.0};


static GLint axis = 2;
static GLdouble viewer[]= {0.0, 0.0, 5.0};

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

/* initial viewer location */

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update viewer position in modelview matrix */
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0,
0.0, 0.0, 1.0, 0.0);
/* rotate cube */
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 mouse(int btn, int state, int x, int y)


{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
display();
}

void keys(unsigned char key, int x, int y)


{
/* Use x, X, y, Y, z, and Z keys to move viewer */
if(key == 'x') viewer[0]-= 1.0;
if(key == 'X') viewer[0]+= 1.0;
if(key == 'y') viewer[1]-= 1.0;
if(key == 'Y') viewer[1]+= 1.0;
if(key == 'z') viewer[2]-= 1.0;
if(key == 'Z') viewer[2]+= 1.0;
display();
}

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
/* Use a perspective view */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/
(GLfloat) w,2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/


(GLfloat) h,2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
/* Or we can use gluPerspective */
/* gluPerspective(45.0, w/h, -10.0, 10.0); */
glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Colorcube Viewer");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

Output:

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

9. Program to fill a polygon using area filling algorithm

#define BLACK 0
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
void edgedetect(float x1,float y1,float x2,float y2,int
*le,int *re)
{
float mx,x,temp;
int i;
if((y2-y1)<0)
{
temp=y1;y1=y2;y2=temp;
temp=x1;x1=x2;x2=temp;
}
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else
mx=x2-x1;
x=x1;
for(i=y1;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void draw_pixel(int x,int y,int value)
{
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void scanfill(float x1,float y1,float x2,float y2,float
x3,float y3,float x4,float y4)
{
int le[500],re[500];
int i,y;
for(i=0;i<500;i++)
{
le[i]=500;
re[i]=0;
}
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
edgedetect(x3,y3,x4,y4,le,re);

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

edgedetect(x4,y4,x1,y1,le,re);
for(y=0;y<500;y++)
{
if(le[y]<=re[y])
for(i=(int)le[y];i<(int)re[y];i++)
draw_pixel(i,y,BLACK);

}
void display()
{
x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=30
0.0;y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
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,499.0,0.0,499.0);
}

void main(int argc, char** argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Filling a Polygon using Scan-line
Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

Output:

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

10.Program to display a set of values{fij} as a rectangular


mesh.
/*Program 10:Program to create a rectangular mesh*/
#include<gl/glut.h>
#include<stdio.h>
float r=1,c=1.3,i,j;
void dis()
{
glClear(GL_COLOR_BUFFER_BIT);
for(i=0.0;i<c;i=i+0.1)
{
glBegin(GL_LINE_STRIP);
for(j=0.0;j<r;j=j+0.1)
glVertex2f(i-0.9,j-0.9);
glEnd();
}
for(i=0.0;i<r;i=i+0.1)
{
glBegin(GL_LINE_STRIP);
for(j=0.0;j<c;j=j+0.1)
glVertex2f(j-0.9,i-0.9);
glEnd();
}
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitWindowSize(900,600);
glutCreateWindow("mesh");
glutDisplayFunc(dis);
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 2


CG & Visualization Lab Manual

EXTRA PROGRAMS
1.Program to plot points on the window using openGL
functions
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(10.0,10.0);
glVertex2f(150.0,80.0);
glVertex2f(100.0,20.0);
glVertex2f(200.0,100.0);
glEnd();
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
gluOrtho2D(0.0,550.0,0.0,550.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("points");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
Output:

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

2.Program to draw lines using openGL function


#include <GL/glut.h>
int cx=200;
int cy=200;
void init(void)
{
glClearColor (0.0, 0.0, 1.0, 0.0);
//glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,200.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glBegin(GL_LINES);
glVertex2i (cx,cy);
glVertex2i (75,25);
glVertex2i (75,75);
glVertex2i (25,75);
glEnd();
glFlush();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("LINE");
init();
glutDisplayFunc(display);
glutMainLoop();
}
Output:

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

3. Program to move a triangle left,right and top using


OpenGL functions
#include <GL/glut.h>
int a=0,b=0;
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
/* Establish viewing area to cover entire window. */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, 1, -1);
/* Map abstract coords directly to window coords. */
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) a-
=10;// to move left
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
b+=10; // to move top
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
a+=10; // to move right
glutPostRedisplay();
}

void display(void)
{
//gluOrtho2D(0.0,500.0,0.0,500.0);
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0); /* blue */
glVertex2i(200+a, 200+b);
glColor3f(0.0, 1.0, 0.0); /* green */
glVertex2i(300+a, 200+b);
glColor3f(1.0, 0.0, 0.0); /* red */
glVertex2i(250+a, 300+b);
glEnd();
glFlush();
}

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitWindowSize(500.0,500.0);
glutInitWindowPosition(0.0,0.0);
glutCreateWindow("single triangle");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
}

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

Output:

4.Program to diplay list of teapots with different color


using openGL functions
#include <stdlib.h>
#include <GL/glut.h>
GLuint teapotList;
/*
* Initialize depth buffer, projection matrix, light
source, and lighting
* model. Do not specify a material property here.
*/
void init(void)
{
GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat position[] = {0.0, 3.0, 3.0, 0.0};

GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};


GLfloat local_view[] = {0.0};

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);


glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

glFrontFace(GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
/* be efficient--make teapot display list */
teapotList = glGenLists(1);
glNewList (teapotList, GL_COMPILE);
glutSolidTeapot(1.0);
glEndList ();
}

/*
* Move object into position. Use 3rd through 12th
* parameters to specify the material property. Draw a
teapot.
*/
void renderTeapot(GLfloat x, GLfloat y,
GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb, GLfloat
shine)
{
GLfloat mat[4];

glPushMatrix();
glTranslatef(x, y, 0.0);
mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] =
1.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat[0] = difr; mat[1] = difg; mat[2] = difb;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
mat[0] = specr; mat[1] = specg; mat[2] = specb;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
glCallList(teapotList);
glPopMatrix();
}

/**
* First column: emerald, jade, obsidian, pearl, ruby,
turquoise
* 2nd column: brass, bronze, chrome, copper, gold,
silver
* 3rd column: black, cyan, green, red, white, yellow
plastic
* 4th column: black, cyan, green, red, white, yellow
rubber
*/

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,0.07568,
0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435,
0.3);
renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
1, 0.829, 0.829, 0.296648, 0.296648, 0.296648,0.088);
renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
0.61424, 0.04136, 0.04136, 0.727811, 0.626959,
0.626959, 0.6);
renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,0.396,
0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
0.780392, 0.568627, 0.113725, 0.992157, 0.941176,
0.807843,0.21794872);
renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,0.714,
0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,0.7038,
0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,0.75164,
0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
0.50754, 0.50754, 0.50754, 0.508273, 0.508273,
0.508273, 0.4);
renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01,
0.01,0.50, 0.50, 0.50, .25);
renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0,
0.50980392, 0.50980392,
0.50196078, 0.50196078, 0.50196078, .25);
renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
0.7, 0.6, 0.6, .25);
renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
0.70, 0.70, 0.70, .25);
renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
0.60, 0.60, 0.50, .25);
renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01,
0.01,
0.4, 0.4, 0.4, .078125);
renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
0.04, 0.7, 0.7, .078125);
renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
0.04, 0.7, 0.04, .078125);

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,


0.7, 0.04, 0.04, .078125);
renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
0.7, 0.7, 0.7, .078125);
renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
0.7, 0.7, 0.04, .078125);
glFlush();
}

void reshape(int w, int h)


{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(0.0, 16.0, 0.0, 16.0*(GLfloat)h/(GLfloat)w,
-10.0, 10.0);
else
glOrtho(0.0, 16.0*(GLfloat)w/(GLfloat)h, 0.0, 16.0,
-10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)


{
switch (key) {
case 27:
exit(0);
break;
}
}

/*
* Main Loop
*/
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB |
GLUT_DEPTH);
glutInitWindowSize(500, 600);
glutInitWindowPosition(50,50);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

Output:

5.Program to spin a rectangle using openGL functions

/*
* This program demonstrates double buffering for
* flicker-free animation. The left and middle mouse
* buttons start and stop the spinning motion of the
square.
*/
#include <stdlib.h>
#include <GL/glut.h>
static GLfloat spin = 0.0;
int singleb, doubleb;

void displayd(void)
{
glClear (GL_COLOR_BUFFER_BIT);

glRectf (-25.0, -25.0, 25.0, 25.0);

glutSwapBuffers ();
}

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

void displays(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glRectf (-25.0, -25.0, 25.0, 25.0);
glFlush();
}

void spinDisplay (void)


{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutSetWindow(singleb);
glLoadIdentity();
glRotatef (spin, 0.0, 0.0, 1.0);
glutPostRedisplay();
glutSetWindow(doubleb);
glLoadIdentity();
glRotatef (spin, 0.0, 0.0, 1.0);
glutPostRedisplay();
}

void myinit (void)


{
glClearColor (0.0, 0.0, 0.0, 1.0);
glColor3f (1.0, 1.0, 1.0);
glShadeModel (GL_FLAT);
}

void mouse(int btn, int state, int x, int y)


{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(spinDisplay);
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
glutIdleFunc(NULL);
}

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
else
glOrtho( -50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/
(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{

glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
singleb=glutCreateWindow("single buffered");
myinit ();
glutDisplayFunc(displays);
glutReshapeFunc (myReshape);
glutIdleFunc (spinDisplay);
glutMouseFunc (mouse);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
doubleb=glutCreateWindow("double buffered");
myinit ();
glutDisplayFunc(displayd);
glutReshapeFunc (myReshape);
glutIdleFunc (spinDisplay);
glutMouseFunc (mouse);

glutMainLoop();

}
Output:

CSE Department, AIT, Bangalore -90 3


CG & Visualization Lab Manual

6.Program to build arm of a Robot and make the movements


using openGL functions
#include <stdlib.h>
#include <GL/glut.h>

static int shoulder = 0, elbow = 0;

void init(void)

{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)


{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0,
20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}

CSE Department, AIT, Bangalore -90 4


CG & Visualization Lab Manual

void keyboard (unsigned char key, int x, int y)


{
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay();
break;
case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

CSE Department, AIT, Bangalore -90 4


CG & Visualization Lab Manual

Output:

7. Program to draw a Solid Torus and display it in


different light positions

#include "glut.h"
static int spin = 0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
/* Here is where the light position is reset after the
modeling
* transformation (glRotated) is called. This places the
* light at a new position in world coordinates. The cube
* represents the position of the light.
*/
void display(void)
{
GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();

CSE Department, AIT, Bangalore -90 4


CG & Visualization Lab Manual

glTranslatef (0.0, 0.0, -5.0);


glPushMatrix ();
glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);
glLightfv (GL_LIGHT0, GL_POSITION, position);
glTranslated (0.0, 0.0, 1.5);
glDisable (GL_LIGHTING);
glColor3f (0.0, 1.0, 1.0);
glutWireCube (0.1);
glEnable (GL_LIGHTING);
glPopMatrix ();
glutSolidTorus (0.275, 0.85, 8, 15);
glPopMatrix ();
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
spin = (spin + 30) % 360;
glutPostRedisplay();
}
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

CSE Department, AIT, Bangalore -90 4


CG & Visualization Lab Manual

Out Put:

CSE Department, AIT, Bangalore -90 4

You might also like