Program To Recursively Subdivide A Tetrahedron To Form 3D Serpinsky Gasket - The Number of Recursive Steps Is To Be Specified by The User
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];
int n;
void triangle(point a,point b,point c)
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
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();
}
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();
}
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");
}
4
Computer Graphics and Visualization Lab
3. Program to draw a color cube and spin it using opengl transformation matrices.
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
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 };
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 };
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();
}
6
Computer Graphics and Visualization Lab
void spincube()
{
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}
7
Computer Graphics and Visualization Lab
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
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);
}
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
};
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;
}
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
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();
}
14
Computer Graphics and Visualization Lab
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
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_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();
}
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>
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();
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>
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 };
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 };
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();
}
22
Computer Graphics and Visualization Lab
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;
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();
}
#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