Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* recursive subdivision of triangle to form Sierpinski gasket */
/* number of recursive steps given on command line */
#include<time.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
/* initial triangle */
typedef GLfloat point3[3];
point3 v[]={{-1.0, -0.58,0.0}, {1.0, -0.58,0.0}, {0.0, 1.15,0.0},{0.0,0.0,-1.73}};
int xyz[] = {0,0,0};
int n = 2; /* number of recursive steps */
int dg = 0 ;
void triangle( point3 a, point3 b, point3 c, point3 d)
{
/* draw one triangle */
glColor3f(1.0, 0.0, 0.0);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glColor3f(0.0, 1.0, 0.0);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(d);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(a);
glVertex3fv(c);
glVertex3fv(d);
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(c);
glVertex3fv(b);
glVertex3fv(d);
}
void divide_triangle(point3 a, point3 b, point3 c, point3 d , int m)
{
/* triangle subdivision using vertex numbers */
point3 v0, v1, v2, v3, v4, v5;
int j;
if(m > 0)
{
for(j=0; j<3; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) v2[j]=(d[j]+a[j])/2;
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2;
for(j=0; j<3; j++) v4[j]=(d[j]+b[j])/2;
for(j=0; j<3; j++) v5[j]=(d[j]+c[j])/2;
divide_triangle(a, v0, v1, v2, m-1);
divide_triangle(v0, b, v3, v4, m-1);
divide_triangle(v1, v3, c, v5, m-1);
divide_triangle(v2, v4, v5, d, m-1);
}
else triangle(a,b,c,d); /* draw triangle at end of recursion */
}
void keyboard ( unsigned char key , int x , int y )
{
switch (key) {
case 'q':
case 'Q':
if( n >= 1)
--n;
break;
case 'e':
case 'E':
++n;
break;
case 'a':
case 'A':
xyz[0] += 2;
case 's':
case 'S':
xyz[1] += 2;
case 'd':
case 'D':
xyz[2] += 2;
default:
break;
}
glutPostRedisplay () ;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //#do Rotate, Translate, Scale.
glLoadIdentity();
glRotatef(xyz[0],1,0,0);
glRotatef(xyz[1],0,1,0);
glRotatef(xyz[2],0,0,1);
glBegin(GL_TRIANGLES);
divide_triangle(v[0], v[1], v[2], v[3], n);
glEnd();
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);
}
void timer ( )
{
clock_t now = clock ( ) ;
static clock_t last = 0 ;
if ( ( now - last ) / ( ( float ) CLOCKS_PER_SEC ) >= 1 / 60.0 )
{
dg++;
dg %= 360 ;
glutPostRedisplay ( ) ;
last = now ;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(timer);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment