summaryrefslogtreecommitdiff
path: root/src/2
diff options
context:
space:
mode:
Diffstat (limited to 'src/2')
-rw-r--r--src/2/main.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/2/main.c b/src/2/main.c
new file mode 100644
index 0000000..2d78d35
--- /dev/null
+++ b/src/2/main.c
@@ -0,0 +1,92 @@
1/* 3-d serpinsky gasket
2 *
3 * Akshay Oppiliappan
4 */
5
6#include <GL/glut.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <time.h>
10
11typedef float point[3];
12
13point tetra[4] = {
14 {0, 250, -250},
15 {0, 0, 250},
16 {250, -250, 250},
17 {-250, -250, -250},
18};
19
20void myInit() {
21 glClearColor(1.0, 1.0, 1.0, 1.0);
22 glOrtho(-300.0, 300.0, -300.0, 300.0, -300.0, 300.0);
23};
24
25void drawTriangle(point p1, point p2, point p3) {
26 glBegin(GL_TRIANGLES);
27 glVertex3fv(p1);
28 glVertex3fv(p2);
29 glVertex3fv(p3);
30 glEnd();
31}
32
33void drawTetra(point p1, point p2, point p3, point p4) {
34 glColor3f(1.0, 1.0, 0.0);
35 drawTriangle(p1, p2, p3);
36
37 glColor3f(0.0, 1.0, 0.0);
38 drawTriangle(p1, p3, p4);
39
40 glColor3f(1.0, 0.0, 0.0);
41 drawTriangle(p1, p4, p2);
42
43 glColor3f(0.0, 0.0, 1.0);
44 drawTriangle(p2, p3, p4);
45}
46
47void divideTetra(point p1, point p2, point p3, point p4, int iter) {
48 point mid[6];
49 int j;
50
51 if (iter > 0) {
52 for (j = 0; j < 3; j++)
53 mid[0][j] = (p1[j] + p2[j]) / 2;
54 for (j = 0; j < 3; j++)
55 mid[1][j] = (p1[j] + p3[j]) / 2;
56 for (j = 0; j < 3; j++)
57 mid[2][j] = (p1[j] + p4[j]) / 2;
58 for (j = 0; j < 3; j++)
59 mid[3][j] = (p2[j] + p3[j]) / 2;
60 for (j = 0; j < 3; j++)
61 mid[4][j] = (p3[j] + p4[j]) / 2;
62 for (j = 0; j < 3; j++)
63 mid[5][j] = (p2[j] + p4[j]) / 2;
64
65 divideTetra(p1, mid[0], mid[1], mid[2], iter - 1);
66 divideTetra(mid[0], p2, mid[3], mid[5], iter - 1);
67 divideTetra(mid[1], mid[3], p3, mid[4], iter - 1);
68 divideTetra(mid[2], mid[4], p4, mid[5], iter - 1);
69 } else {
70 drawTetra(p1, p2, p3, p4);
71 }
72}
73
74void display() {
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76 glBegin(GL_TRIANGLES);
77 divideTetra(tetra[0], tetra[1], tetra[2], tetra[3], 5);
78 glEnd();
79 glFlush();
80}
81
82int main(int argc, char *argv[]) {
83 glutInit(&argc, argv);
84 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
85 glutInitWindowSize(600, 600);
86 glutInitWindowPosition(0, 0);
87 glutCreateWindow("Sierpinski Gasket");
88 glutDisplayFunc(display);
89 glEnable(GL_DEPTH_TEST);
90 myInit();
91 glutMainLoop();
92}