May 26, 2012

Computer Graphics And Visualization Laboratory


1.Program to create a 3D Sierpinski gasket using Open GL functions

#include
#include
typedef float point[3];
point v[]={{-1,0,0},{1,0,0},{0,2,0},{0,1,1}};
int n;

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

void tetra(point a,point b,point c,int m)
{
       point v1,v2,v3;
       int i;
       if(m>0)
       {
             for(i=0;i<3;i++)
             {
                    v1[i]=(a[i]+b[i])/2;
                    v2[i]=(b[i]+c[i])/2;
                    v3[i]=(c[i]+a[i])/2;
             }
             tetra(a,v1,v3,m-1);
             tetra(b,v1,v2,m-1);
             tetra(c,v2,v3,m-1);
       }
       else
              triangle(a,b,c);
}

void display()
{
       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
       glColor3f(1,0,0);
       tetra(v[0],v[1],v[2],n);
       glColor3f(0,1,0);
       tetra(v[0],v[1],v[3],n);
       glColor3f(0,0,1);
       tetra(v[0],v[2],v[3],n);
       glColor3f(0,0,0);
       tetra(v[1],v[2],v[3],n);
}

void myInit()
{
       glClearColor(1,1,1,0);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(-3,3,-3,3,-10,10);
}

void main(int argc,char **argv)
{
       printf("\nEnter the no.of division:\n");
       scanf("%d",&n);
       glutInit(&argc,argv);
       glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH);
       glutInitWindowSize(600,600);
       glutInitWindowPosition(0,0);
       glutCreateWindow("gasket");

       glutDisplayFunc(display);
       glEnable(GL_DEPTH_TEST);
       myInit();
       glutMainLoop();
}




No comments:

Post a Comment