Repository navigation
Expand file tree
/
Copy pathicosphere.h
More file actions
231 lines (164 loc) · 4.53 KB
/
Copy pathicosphere.h
File metadata and controls
231 lines (164 loc) · 4.53 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
struct Icosphere: Model {
std::vector<glm::vec3> positions;
std::vector<glm::uvec3> indices;
Buffer pos, ind;
Icosphere():Model({"in_Position", "in_Tex"}){
build();
split();
pos.fill(positions);
ind.fill(indices);
bind<glm::vec3>("in_Position", &pos);
index(&ind);
SIZE = indices.size()*3;
}
void build();
void split();
void sort(){
std::sort(indices.begin(), indices.end(), [&](const glm::uvec3& a, const glm::uvec3& b){
glm::vec3 ap = cam::vp*glm::vec4((positions[a.x] + positions[a.y] + positions[a.z])/3.0f, 1.0);
glm::vec3 bp = cam::vp*glm::vec4((positions[b.x] + positions[b.y] + positions[b.z])/3.0f, 1.0);
return ap.z > bp.z;
});
ind.fill(indices);
}
};
void Icosphere::build(){
const float r = 1.0f;
const float a = 72.0f/360.0f*2.0f*3.14159265f;
//Helper Methods
auto addpos = [&](float a, float b, float c){
positions.emplace_back(a, b, c);
};
//For a Triangles
auto triangle = [&](int a, int b, int c){
indices.emplace_back(a, b, c);
};
//Top Cap
addpos(0, r, 0);
for(int i = 0; i < 5; i++){ //Top
float x1 = r*cos(atan(0.5))*cos(a*i);
float y1 = r*cos(atan(0.5))*sin(a*i);
float z1 = r*sin(atan(0.5));
addpos(x1,z1,y1);
}
for(int i = 0; i < 5; i++){ //Bottom
float x2 = r*cos(atan(0.5))*cos(a*i+a/2.0f);
float y2 = r*cos(atan(0.5))*sin(a*i+a/2.0f);
float z2 = -r*sin(atan(0.5));
addpos(x2,z2,y2);
}
//Bottom Cap
addpos(0,-r,0);
//Top Triangles
triangle(1,0,2);
triangle(2,0,3);
triangle(3,0,4);
triangle(4,0,5);
triangle(5,0,1);
//Bottom Triangles
triangle(6,7,11);
triangle(7,8,11);
triangle(8,9,11);
triangle(9,10,11);
triangle(10,6,11);
//Connecting Triangles (Bottom)
triangle(6,2,7);
triangle(7,3,8);
triangle(8,4,9);
triangle(9,5,10);
triangle(10,1,6);
//Connecting Triangles (Top)
triangle(2,3,7);
triangle(1,2,6);
triangle(3,4,8);
triangle(4,5,9);
triangle(5,1,10);
}
void Icosphere::split(){
std::vector<glm::uvec3> newind;
auto addpos = [&](glm::vec3 p){
positions.push_back(p);
};
auto triangle = [&](int a, int b, int c){
newind.emplace_back(a, b, c);
};
auto splittriangle = [&](int i){
//Indicies of the old triangle
GLuint k1 = indices[i][0];
GLuint k2 = indices[i][1];
GLuint k3 = indices[i][2];
//Positions of the old triangle
glm::vec3 a = positions[k1];
glm::vec3 b = positions[k2];
glm::vec3 c = positions[k3];
//Starting Indices
int NI = positions.size();
//Compute Split Points
glm::vec3 d = normalize(0.5f*(a+b));
glm::vec3 e = normalize(0.5f*(b+c));
glm::vec3 f = normalize(0.5f*(c+a));
//Add New Positions to Model
addpos(d); addpos(e); addpos(f);
triangle(k1, NI+0, NI+2);
triangle(k2, NI+1, NI+0);
triangle(k3, NI+2, NI+1);
triangle(NI+0, NI+1, NI+2);
};
for(size_t j = 0; j < 5; j++){
newind.clear();
for(int i = 0; i < indices.size(); i++) //Loop over Old Triangles
splittriangle(i);
indices = newind;
}
newind.clear();
for(int i = 0; i < indices.size(); i++){
GLuint k1 = indices[i][0];
GLuint k2 = indices[i][1];
GLuint k3 = indices[i][2];
//Positions of the old triangle
glm::vec3 a = positions[k1];
glm::vec3 b = positions[k2];
glm::vec3 c = positions[k3];
//Average Position
glm::vec3 TC = normalize((a + b + c)/3.0f);
if(abs(TC.y) > 0.6)
splittriangle(i);
else newind.push_back(indices[i]);
//Loop over Old Triangles
}
indices = newind;
newind.clear();
for(int i = 0; i < indices.size(); i++){
GLuint k1 = indices[i][0];
GLuint k2 = indices[i][1];
GLuint k3 = indices[i][2];
//Positions of the old triangle
glm::vec3 a = positions[k1];
glm::vec3 b = positions[k2];
glm::vec3 c = positions[k3];
//Average Position
glm::vec3 TC = normalize((a + b + c)/3.0f);
if(abs(TC.y) > 0.7)
splittriangle(i);
else newind.push_back(indices[i]);
//Loop over Old Triangles
}
indices = newind;
newind.clear();
for(int i = 0; i < indices.size(); i++){
GLuint k1 = indices[i][0];
GLuint k2 = indices[i][1];
GLuint k3 = indices[i][2];
//Positions of the old triangle
glm::vec3 a = positions[k1];
glm::vec3 b = positions[k2];
glm::vec3 c = positions[k3];
//Average Position
glm::vec3 TC = normalize((a + b + c)/3.0f);
if(abs(TC.y) > 0.8)
splittriangle(i);
else newind.push_back(indices[i]);
//Loop over Old Triangles
}
indices = newind;
}