-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingFunctions.cpp
131 lines (106 loc) · 3.94 KB
/
DrawingFunctions.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "DrawingFunctions.h"
#include <math.h>
#include <time.h>
namespace DrawingFunctions
{
const float X1 = .5257311F;
const float Z1 = .8506508F;
const float vdata[12][3] = {
{-X1, 0.0, Z1}, {X1, 0.0, Z1}, {-X1, 0.0, -Z1}, {X1, 0.0, -Z1},
{0.0, Z1, X1}, {0.0, Z1, -X1}, {0.0, -Z1, X1}, {0.0, -Z1, -X1},
{Z1, X1, 0.0}, {-Z1, X1, 0.0}, {Z1, -X1, 0.0}, {-Z1, -X1, 0.0} };
const int tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
void Normalize(float* vector)
{
float length = std::sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
vector[0] /= length;
vector[1] /= length;
vector[2] /= length;
}
void DrawTriangle(const float* a, const float* b, const float* c, int div, float r)
{
if (div <= 0)
{
glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r);
glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r);
glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, c[2] * r);
}
else
{
GLfloat ab[3], ac[3], bc[3];
for (int i = 0; i < 3; i++)
{
ab[i] = (a[i] + b[i]) / 2;
ac[i] = (a[i] + c[i]) / 2;
bc[i] = (b[i] + c[i]) / 2;
}
Normalize(ab); Normalize(ac); Normalize(bc);
DrawTriangle(a, ab, ac, div - 1, r);
DrawTriangle(b, bc, ab, div - 1, r);
DrawTriangle(c, ac, bc, div - 1, r);
DrawTriangle(ab, bc, ac, div - 1, r);
}
}
void DrawSphere(int ndiv, float radius) {
glBegin(GL_TRIANGLES);
for (int i = 0; i < 20; i++)
DrawTriangle(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], ndiv, radius);
glEnd();
}
void DrawCube(float scale, bool active, bool selected)
{
const float sizex = 0.5f * scale;
const float sizey = 0.5f * scale;
const float sizez = 0.5f * scale;
float selectionColorBump = 0.05f;
if (active)
selectionColorBump = 0.5f;
if (selected)
glColor3f(0.8f, 0.8f, 0.8f);
glBegin(GL_QUADS);
// FRONT
if (!selected) glColor3f(0.0f, 0.0f, selectionColorBump);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(sizex, -sizey, sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(-sizex, sizey, sizez);
// BACK
glNormal3f(0.0f, 0.0f, selectionColorBump);
glVertex3f(-sizex, -sizey, -sizez);
glVertex3f(-sizex, sizey, -sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(sizex, -sizey, -sizez);
// LEFT
if (!selected) glColor3f(selectionColorBump, 0.0f, 0.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(-sizex, sizey, sizez);
glVertex3f(-sizex, sizey, -sizez);
glVertex3f(-sizex, -sizey, -sizez);
// RIGHT
glNormal3f(selectionColorBump, 0.0f, 0.0f);
glVertex3f(sizex, -sizey, -sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(sizex, -sizey, sizez);
// TOP
if (!selected) glColor3f(0.0f, selectionColorBump, 0.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-sizex, sizey, sizez);
glVertex3f(sizex, sizey, sizez);
glVertex3f(sizex, sizey, -sizez);
glVertex3f(-sizex, sizey, -sizez);
// BOTTOM
glNormal3f(0.0f, selectionColorBump, 0.0f);
glVertex3f(-sizex, -sizey, sizez);
glVertex3f(-sizex, -sizey, -sizez);
glVertex3f(sizex, -sizey, -sizez);
glVertex3f(sizex, -sizey, sizez);
glEnd();
}
}