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

Program To Recursively Subdivide A Tetrahedron To Form 3D Serpinsky Gasket - The Number of Recursive Steps Is To Be Specified by The User

The document contains code for 4 computer graphics programs: 1. A program to recursively subdivide a tetrahedron to form a 3D Sierpinsky gasket. 2. A program to implement the Liang-Barsky line clipping algorithm. 3. A program to draw a color cube in OpenGL and spin it using transformation matrices. 4. A program to create a rotating house figure about a fixed point using OpenGL functions.

Uploaded by

ani2011
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Program To Recursively Subdivide A Tetrahedron To Form 3D Serpinsky Gasket - The Number of Recursive Steps Is To Be Specified by The User

The document contains code for 4 computer graphics programs: 1. A program to recursively subdivide a tetrahedron to form a 3D Sierpinsky gasket. 2. A program to implement the Liang-Barsky line clipping algorithm. 3. A program to draw a color cube in OpenGL and spin it using transformation matrices. 4. A program to create a rotating house figure about a fixed point using OpenGL functions.

Uploaded by

ani2011
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 29

Computer Graphics and Visualization Lab

1. Program to recursively subdivide a tetrahedron to form 3D Serpinsky gasket . The number


of recursive steps is to be specified by the user.

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

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


{0.0,0.943,-0.33},
{-0.816,-0.471,-0.33},
{0.816,-0.471,0.33}};

int n;
void triangle(point a,point b,point c)
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

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


{
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_tri(a,v1,v2,m-1);

divide_tri(c,v2,v3,m-1);

divide_tri(b,v3,v1,m-1);

1
Computer Graphics and Visualization Lab

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

void tetrahedron(int m)
{
glColor3f(1.0,0.0,0.0);
divide_tri(v[0],v[1],v[2],m);
glColor3f(0.0,1.0,0.0);
divide_tri(v[3],v[2],v[1],m);
glColor3f(0.0,0.0,1.0);
divide_tri(v[0],v[3],v[1],m);
glColor3f(0.0,0.0,0.0);
divide_tri(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)


{

2
Computer Graphics and Visualization Lab

if(argc>1)
n=atoi(argv[1]);
else
n=4;
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();
}
2. Program to implement liang barsky line clipping algorithm .

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>

void main()
{
int gm,gd=DETECT,k;
float x1,x2,y1,y2,xmin,ymin,xmax,ymax,dx,dy;
float p[4],q[4],r[4],u1=0.0,u2=1.0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
// clrscr();
printf("enter the clipping window");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
printf("enter the line ");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;

3
Computer Graphics and Visualization Lab

q[3]=ymax-y1;

for(k=0;k<=3;k++)
{
if(p[k]==0 && q[k]<0)
{
printf("the line lies completely outside");
getch();
}
}
for(k=0;k<=3;k++)
{
if(p[k]<0)
{
r[k]=q[k]/p[k];
u1=max(u1,r[k]);
}

if(p[k]>0)
{
r[k]=q[k]/p[k];
u2=min(u2,r[k]);
}
}

if(u1>u2)
{
printf("the line lies outside the clipping window\n");
}

if(u1==0.0 && u2==1.0)


{
printf("the line lies inside the clipping window");
}

if(u1<u2 && u1!=0.0 && u2!=1.0)


{
x2=x1+(dx*u2);
y2=y1+(dx*u2);
x1=x1+(dx*u1);
y1=y1+(dx*u1);

4
Computer Graphics and Visualization Lab

printf("the clipping line is");


clrscr();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}

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

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

GLfloat vertices[ ]={ -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 normals[ ]={ -1.0,-1.0,-1.0,

5
Computer Graphics and Visualization Lab

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[ ]={ 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};

GLubyte cubeIndices[]={0,3,2,1,
2,3,7,6,
0,4,7,3,
1,2,6,5,
4,5,6,7,
0,1,5,4 };

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


static GLint axis=2;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);
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_RIGHT_BUTTON && state==GLUT_DOWN) axis=1;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=2;

6
Computer Graphics and Visualization Lab

void spincube()
{
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}

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

void main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("color cuce");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutIdleFunc(spincube);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
glColorPointer(3,GL_FLOAT,0,colors);
glNormalPointer(GL_FLOAT,0,normals);
glColor3f(1.0,1.0,1.0);
glutMainLoop();
}
4. Program to create a house like figure and roatate about a given fixed point using opengl
functions.

7
Computer Graphics and Visualization Lab

#include <stdlib.h>
#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 rot_mat[3][3]={{0},{0},{0}};
GLfloat result[3][9]={{0},{0},{0}};
GLfloat h=100.0;
GLfloat k=100.0,theta;

void multiply()
{
int i,j,l;
for(i=0;i<3;i++)
for(j=0;j<9;j++)
{
result[i][j]=0;
for(l=0;l<3;l++)
result[i][j]=result[i][j]+rot_mat[i][l]*house[l][j];
}
}

void rotate()
{
GLfloat m,n;
m=-h*(cos(theta)-1)+k*(sin(theta));
n=-k*(cos(theta)-1)-h*(sin(theta));
rot_mat[0][0]=cos(theta);
rot_mat[0][1]=-sin(theta);
rot_mat[0][2]=m;
rot_mat[1][0]=sin(theta);
rot_mat[1][1]=cos(theta);
rot_mat[1][2]=n;
rot_mat[2][0]=0;
rot_mat[2][1]=0;
rot_mat[2][2]=1;
multiply();
}

8
Computer Graphics and Visualization Lab

void draw_house()
{
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]);
glVertex2f(result[0][7],result[1][7]);
glVertex2f(result[0][8],result[1][8]);

9
Computer Graphics and Visualization Lab

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);
draw_house();
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,500.0,0.0,500.0);
}

void main(int argc,char *argv[])


{
printf("enter the rotation angle\n");
scanf("%f",&theta);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("house rotation");
glutDisplayFunc(display);
myinit();

10
Computer Graphics and Visualization Lab

glutMainLoop();
}

5. Program to implement the cohen sutherland line clipping algorithm .Make provisions to
specify the input line ,window for clipping and viewport for displaying the clipped image.

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
#define MAX 20
enum
{
TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8
};

enum
{
TRUE,FALSE
};

typedef unsigned int outcode;

outcode compute_outcode(int x,int y,int xmin,int ymin,int xmax,int ymax)


{
outcode oc=0;

11
Computer Graphics and Visualization Lab

if (y>ymax)
oc|=TOP;
else if (y<ymin)
oc|=BOTTOM;
else if (x>xmax)
oc|=RIGHT;
else if (x<xmin)
oc|=LEFT;
return oc;
}

void cohen_suth(double x1,double y1,double x2,double y2,double xmin,


double ymin,double xmax,double ymax)
{

int accept,done;
outcode outcode1,outcode2;
accept=FALSE;
done=FALSE;

outcode1=compute_outcode(x1,y1,xmin,ymin,xmax,ymax);
outcode2=compute_outcode(x2,y2,xmin,ymin,xmax,ymax);

do
{
if (outcode1==0 && outcode2==0)
{
accept=TRUE;
done=TRUE;
}
else if (outcode1 & outcode2)
{
done=TRUE;
}
else
{
double x,y;
int outcode_ex=outcode1?outcode1:outcode2;
if(outcode_ex & TOP)
{
x=x1+(x2-x1)*(ymax-y1)/(y2-y1);

12
Computer Graphics and Visualization Lab

y=ymax;
}
else if (outcode_ex & BOTTOM)
{
x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
y=ymin;
}
else if (outcode_ex & RIGHT)
{
y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
x=xmax;
}
else
{
y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
x=xmin;
}
if (outcode_ex==outcode1)
{
x1=x;
y1=y;
outcode1=compute_outcode(x,y,xmin,ymin,xmax,ymax);
}
else
{
x2=x;
y2=y;
outcode2=compute_outcode(x2,y2,xmin,ymin,xmax,ymax);
}
}
}while(done==FALSE);

if(accept==TRUE)
line(x1,y1,x2,y2);
}

void main()
{
int n,i,j,gd=DETECT,gm;
int ln[MAX][4];
int clip[4];

13
Computer Graphics and Visualization Lab

printf("enter the no.of lines to be clipped");


scanf("%d",&n);

printf("enter the x,y coordinates of lines:\n");


for(i=0;i<n;i++)
for(j=0;j<4;j++)
scanf("%d",&ln[i][j]);

printf("enter the clipping window");


for(i=0;i<4;i++)
scanf("%d",&clip[i]);

initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(clip[0],clip[1],clip[2],clip[3]);
for(i=0;i<n;i++)
{
line(ln[i][0],ln[i][1],ln[i][2],ln[i][3]);
}
getch();
cleardevice();
rectangle(clip[0],clip[1],clip[2],clip[3]);
for(i=0;i<n;i++)
{
cohen_suth(ln[i][0],ln[i][1],ln[i][2],ln[i][3],
clip[0],clip[1],clip[2],clip[3]);
getch();
}
closegraph();
}

6. Program to create a cylinder and a parallelepiped by extruding a circle and a quadrilateral


respectively.Allow the user to specify the circle and the quadrilateral.

14
Computer Graphics and Visualization Lab

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

void draw_pixel(GLint cx,GLint cy)


{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(cx,cy);
glEnd();
}

void plot_pixel(GLint h,GLint k,GLint x,GLint y)


{
draw_pixel(x+h,y+k);
draw_pixel(-x+h,y+k);
draw_pixel(x+h,-y+k);
draw_pixel(-x+h,-y+k);
draw_pixel(y+h,x+k);
draw_pixel(-y+h,x+k);
draw_pixel(y+h,-x+k);
draw_pixel(-y+h,-x+k);
}

void circle_draw(GLint h,GLint k,GLint r)


{
GLint d=1-r,x=0,y=r;
while(y>x)
{
plot_pixel(h,k,x,y);
if(d<0) d+=2*x+3;
else
{
d+=2*(x-y)+5;
--y;
} ++x;
}
plot_pixel(h,k,x,y);
}

void cylinder_draw()
{
GLint xc=100,yc=100,r=50;
GLint i,n=50;

15
Computer Graphics and Visualization Lab

for(i=0;i<n;i+=3)
circle_draw(xc,yc+i,r);
}

void parallelopiped(int x1,int x2,int y1,int y2,int y3,int y4)


{
glColor3f(0.0,0.0,1.0);
glPointSize(2.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y3);
glVertex2f(x2,y4);
glVertex2f(x1,y2);
glEnd();
}

void parallelopiped_draw()
{
int x1=200,x2=300,y1=100,y2=175,y3=100,y4=175;
GLint i,n=40;
for(i=0;i<n;i+=2)
parallelopiped(x1+i,x2+i,y1+i,y2+i,y3+i,y4+i);
}

void init(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,400.0,0.0,300.0);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(2.0);
cylinder_draw();
parallelopiped_draw();
glFlush();
}

void main(int argc,char *argv[])


{
glutInit(&argc,argv);

16
Computer Graphics and Visualization Lab

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cylinder and parallelopiped");
glutDisplayFunc(display);
init();
glutMainLoop();
}

7. Program ,using opengl functions to draw a simple shaded scene consisiting of a teapot on a
table.Define suitably the positions and properties of the light source along with the properties of the
surfaces of the solid object used in the scene .

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

void wall(double thickness)


{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.0,thickness,1.0);
glutSolidCube(1.0);
glPopMatrix();
}

void tableleg(double thick,double len)


{
glPushMatrix();
glTranslated(0,len/2,0);
glScaled(thick,len,thick);
glutSolidCube(1.0);
glPopMatrix();
}

void table(double topw,double topt,double legt,double legl)


{
glPushMatrix();
glTranslated(0,legl,0);
glScaled(topw,topt,topw);

17
Computer Graphics and Visualization Lab

glutSolidCube(1.0);
glPopMatrix();

double dist=0.95*topw/2.0-legt/2.0;

glPushMatrix();

glTranslated(dist,0,dist);
tableleg(legt,legl);

glTranslated(0,0,-2*dist);
tableleg(legt,legl);

glTranslated(-2*dist,0,2*dist);
tableleg(legt,legl);

glTranslated(0,0,-2*dist);
tableleg(legt,legl);
glPopMatrix();
}

void displaysolid(void)
{
GLfloat mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};

glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);

GLfloat lightint[]={0.7f,0.7f,0.7f,1.0f};
GLfloat lightpos[]={2.0f,6.0f,3.0f,0.0f};

glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightint);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

18
Computer Graphics and Visualization Lab

double winht=1.0;
glOrtho(-winht*64/48.0,winht*64/48.0,-winht,winht,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.3,2.0,0.0,0.25,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotated(90.0,0.0,0.0,1.0);
wall(0.02);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();

glPushMatrix();
glTranslated(0.4,0,0.4);
table(0.6,0.02,0.02,0.3);
glPopMatrix();

glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();

glFlush();

void main(int argc,char**argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("teapot");
glutDisplayFunc(displaysolid);
glEnable(GL_LIGHTING);

19
Computer Graphics and Visualization Lab

glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(0.1,0.1,0.1,0.0);
glViewport(0,0,640,480);
glutMainLoop();
}

8. Program to draw a color cube and allow the user to move the camera to experiment with
perspective viewing .Use opengl functions.

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

GLfloat vertices[ ]={ -1.0,-1.0,-1.0,


1.0,-1.0,-1.0,
1.0, 1.0,-1.0,
- 1.0, 1.0,-1.0,

20
Computer Graphics and Visualization Lab

- 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 normals[ ]={ -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[ ]={ 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};

GLubyte cubeIndices[]={0,3,2,1,
2,3,7,6,
0,4,7,3,
1,2,6,5,
4,5,6,7,
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};

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(theta[0],1.0,0.0,0.0);

21
Computer Graphics and Visualization Lab

glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices);

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_RIGHT_BUTTON && state==GLUT_DOWN) axis=1;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) axis=2;
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}

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


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

22
Computer Graphics and Visualization Lab

void myReshape(int w,int h)


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

void main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("color cuce");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc(keys);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
glColorPointer(3,GL_FLOAT,0,colors);
glNormalPointer(GL_FLOAT,0,normals);
glColor3f(1.0,1.0,1.0);
glutMainLoop();
}

23
Computer Graphics and Visualization Lab

9. Program to fill any polygon using scanline area filling algorithm.(use appropriate
datastructures)

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>

struct point
{
int x,y;
};

int n=0,k,temp,dx,dy,a[20][2],xi[20];
float slope[20];

void scanfill()
{
int i,y;
for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];

if(dy==0)
slope[i]=1.0;
if(dx==0)
slope[i]=0.0;
if((dy!=0)&&(dx!=0))
{
slope[i]=(float)dx/dy;
}

24
Computer Graphics and Visualization Lab

for(y=0;y<480;y++)
{
k=0;
for(i=0;i<n;i++)
{
if((a[i][1]<=y && a[i+1][1]>y) ||
(a[i][1]>y && a[i+1][1]<=y))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}

for(i=0;i<k-1;i++)
{
if(xi[1]>xi[i+1])
{
temp=xi[1];
xi[1]=xi[i+1];
xi[i+1]=temp;
}
}

setcolor(RED);
for(i=0;i<k;i++)
{
line(xi[i],y,xi[i+1],y);
delay(20);
}
}
}

void main()
{
struct point p[30];
int i,gm,gd=DETECT;

printf("enter the no. of points");


scanf("%d",&n);

printf("enter the points");


for(i=0;i<n;i++)

25
Computer Graphics and Visualization Lab

scanf("%d%d",&p[i].x,&p[i].y);

p[i].x=p[0].x;
p[i].y=p[0].y;

initgraph(&gd,&gm,"c:\\tc\\bgi");

for(i=0;i<n;i++)
line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);

for(i=0;i<n;i++)
{
a[i][0]=p[i].x;
a[i][1]=p[i].y;
}

scanfill();
getch();
}

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

#include <stdlib.h>
#include <GL/glut.h>
#define maxx 20
#define maxy 25
#define dx 15
#define dy 10

GLfloat x[maxx]={0.0},y[maxy]={0.0};
GLfloat x0=50.0,y0=50.0;
GLint i,j;

void Myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);

26
Computer Graphics and Visualization Lab

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
glutPostRedisplay();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(i=0;i<maxx;i++)
x[i]=x0+i*dx;
for(j=0;j<maxy;j++)
y[j]=y0+j*dy;
for(i=0;i<maxx-1;i++)
for(j=0;j<maxy-1;j++)
{
glBegin(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();
}
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("mesh");
glutDisplayFunc(display);
Myinit();
glutMainLoop();
}

27
Computer Graphics and Visualization Lab

28
Computer Graphics and Visualization Lab

29

You might also like