CG LAB PROGRAM 3 and 4
CG LAB PROGRAM 3 and 4
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
{0.0,0.942809,-0.33333},
{-0.816497,-0.471405,-0.333333},
{0.816497,-0.471405,-0.333333}
}; //vertices of tetrahedron
int n;
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
{ point v1,v2,v3;
int j;
for(j=0;j<3;j++)
for (j=0;j<3;j++)
for(j=0;j<3;j++)
v3[j]=(b[j]+c[j])/2; //find midpoint of side3
divide_triangle(a,v1,v2,m-1); // triangle
redivision ,
recursive
divide_triangle(c,v2,v3,m-1);
divide_triangle(b,v3,v1,m-1);
glColor3f(0.9,0.1,0.2);
triangular face-1
division
glColor3f(0.8,0.1,0.8);
triangular face-2
division
glColor3f(0.2,0.1,0.9);
triangular face-3
division
glColor3f(0.3,0.3,0.3);
triangular face-4
division
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
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();
fflush(stdout);
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
return 0;
Output:
LAB PROGRAM - 4
4. Develop a OpenGL program to spin 3D sierpinski gasket using OpenGL transformation matrices.
#include<stdlib.h>
#include<stdio.h>
#include<GL/glut.h>
{0.0,0.942809,-0.33333},
{-0.816497,-0.471405,-0.333333},
{0.816497,-0.471405,-0.333333}
}; //vertices of tetrahedron
int n;
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
{ point v1,v2,v3;
int j;
for(j=0;j<3;j++)
for (j=0;j<3;j++)
for(j=0;j<3;j++)
divide_triangle(a,v1,v2,m-1); // triangle
redivision ,
recursive
divide_triangle(c,v2,v3,m-1);
divide_triangle(b,v3,v1,m-1);
{
glColor3f(0.9,0.1,0.2);
triangular face-1
division
glColor3f(0.8,0.1,0.8);
triangular face-2
division
glColor3f(0.2,0.1,0.9);
triangular face-3
division
glColor3f(0.3,0.3,0.3);
triangular face-4
division
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
tetrahedron(n);
glRotatef(0.5,1.0,0.0,0.0);
glFlush();
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();
fflush(stdout);
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutIdleFunc(display);
glutMainLoop();
return 0;
Output: