-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog7.cpp
More file actions
103 lines (92 loc) · 2.09 KB
/
prog7.cpp
File metadata and controls
103 lines (92 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
struct point
{
GLfloat x, y, z;
};
point v[4] = {{0, 0, 1}, {0, 1, 0}, {-1, -0.5, 0}, {1, -0.5, 0}};
int n;
void drawTriangle(point a, point b, point c)
{
glBegin(GL_POLYGON);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
glVertex3f(c.x, c.y, c.z);
glEnd();
}
point midPoint(point a, point b)
{
point mid;
mid.x = (a.x + b.x) / 2;
mid.y = (a.y + b.y) / 2;
mid.z = (a.z + b.z) / 2;
return mid;
}
void divideTriangle(point a, point b, point c, int n)
{
if (n > 0)
{
point v1, v2, v3;
int j;
v1 = midPoint(a, b);
v3 = midPoint(b, c);
v2 = midPoint(a, c);
divideTriangle(a, v1, v2, n - 1);
glFlush();
divideTriangle(c, v2, v3, n - 1);
glFlush();
divideTriangle(b, v3, v1, n - 1);
glFlush();
}
else
drawTriangle(a, b, c);
}
void drawPartTetrahedron(int r, int g, int b, int i, int j, int k)
{
glColor3f(r, g, b);
divideTriangle(v[i], v[j], v[k], n);
}
void tetrahedron()
{
drawPartTetrahedron(1, 0, 0, 0, 1, 2);
drawPartTetrahedron(0, 1, 0, 3, 2, 1);
drawPartTetrahedron(0, 0, 1, 0, 3, 1);
drawPartTetrahedron(0, 0, 0, 0, 2, 3);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2, 2, -2 * h / w, 2 * h / w, -10, 10);
else
glOrtho(-2 * w / h, 2 * w / h, -2, 2, -10, 10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void init()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(" 3D Sierpinski gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 0);
glutMainLoop();
}
int main(int argc, char **argv)
{
printf("No of Recursive steps/Division: ");
scanf("%d", &n);
glutInit(&argc, argv);
init();
}