Skip to content

Instantly share code, notes, and snippets.

@taida957789
Created October 30, 2014 17:55
Show Gist options
  • Save taida957789/db1cb9796c02ce1a4c9a to your computer and use it in GitHub Desktop.
Save taida957789/db1cb9796c02ce1a4c9a to your computer and use it in GitHub Desktop.
/* 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 n = 1; /* 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(c);
glVertex3fv(d);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(a);
glVertex3fv(d);
glVertex3fv(b);
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(b);
glVertex3fv(d);
glVertex3fv(c);
}
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++) v4[j]=(d[j]+a[j])/2;
for(j=0; j<3; j++) v2[j]=(b[j]+c[j])/2;
for(j=0; j<3; j++) v3[j]=(d[j]+c[j])/2;
for(j=0; j<3; j++) v5[j]=(d[j]+b[j])/2;
divide_triangle(a, v0, v1, v4, m-1);
divide_triangle(v0, v1, v2, v5, m-1);
divide_triangle(v1, v2, c, v3, m-1);
divide_triangle(v4, v5, v3, d, m-1);
}
else triangle(a,b,c,d); /* draw triangle at end of recursion */
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //#do Rotate, Translate, Scale.
glLoadIdentity();
glRotatef(dg,0,1,0);
glBegin(GL_TRIANGLES);
divide_triangle(v[0], v[1], v[2], v[3], n);
glEnd();
glFlush();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
glEnable(GL_DEPTH_TEST);
}
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 / 300.0 )
{
dg++;
dg %= 360 ;
last = now ;
}
glutPostRedisplay ( ) ;
}
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);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment