-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimitives.cpp
254 lines (225 loc) · 13 KB
/
Primitives.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Created by jeff2310 on 4/25/18.
//
#include <iostream>
#include "Primitives.h"
#include "ShaderVariables.h"
namespace VkRenderer {
Mesh::Mesh() : vertices(), faces() {}
Mesh::Mesh(const Mesh &m) : vertices(m.vertices), faces(m.faces) {}
int Mesh::count() {
int _count = (int) vertices.size() - 2;
if (_count < 0) return 0;
return _count;
}
void Mesh::addVertex(const Vertex &v) {
vertices.push_back(v);
}
void Mesh::build() {
int faceCount = (int) vertices.size() / 3;
for (int i = 0; i < faceCount; i++) {
Triangle triangle{vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]};
faces.push_back(triangle);
}
}
Mesh createCube(float length, const Color *colors) {
/* [0,0]: -1, -1, -1; [0,1]: 1, -1, -1; [0,2]: 1, 1, -1; [0,3]: -1, 1, -1; (back)
* [1,0]: -1, -1, 1; [1,1]: 1, -1, 1; [1,2]: 1, 1, 1; [1,3]: -1, 1, 1; (front)
* [2,0]: -1, -1, 1; [2,1]: -1, -1, -1; [2,2]: -1, 1, -1; [2,3]: -1, 1, 1; (left)
* [3,0]: 1, -1, 1; [3,1]: 1, -1, -1; [3,2]: 1, 1, -1; [3,3]: 1, 1, 1; (right)
* [4,0]: -1, -1, 1; [4,1]: 1, -1, 1; [4,2]: 1, -1, -1; [4,3]: -1, -1, -1; (bottom)
* [5,0]: -1, 1, 1; [5,1]: 1, 1, 1; [5,2]: 1, 1, -1; [5,3]: -1, 1, -1; (up)
*/
Mesh cube;
length /= 2;
Vector normal;
normal = Vector(0.0f, 0.0f, -1.0f);
cube.addVertex(Vertex(Vector(length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[0], normal,
PhongVariables(Vector(length, -length, -length))));
cube.addVertex(Vertex(Vector(-length, -length, -length), TextureCoordinate(1.0f, 0.0f), colors[0], normal,
PhongVariables(Vector(-length, -length, -length))));
cube.addVertex(Vertex(Vector(-length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[0], normal,
PhongVariables(Vector(-length, length, -length))));
cube.addVertex(Vertex(Vector(length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[0], normal,
PhongVariables(Vector(length, -length, -length))));
cube.addVertex(Vertex(Vector(-length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[0], normal,
PhongVariables(Vector(-length, length, -length))));
cube.addVertex(Vertex(Vector(length, length, -length), TextureCoordinate(0.0f, 1.0f), colors[0], normal,
PhongVariables(Vector(length, length, -length))));
normal = Vector(0.0f, 0.0f, 1.0f);
cube.addVertex(Vertex(Vector(-length, -length, length), TextureCoordinate(0.0f, 0.0f), colors[1], normal,
PhongVariables(Vector(-length, -length, length))));
cube.addVertex(Vertex(Vector(length, -length, length), TextureCoordinate(1.0f, 0.0f), colors[1], normal,
PhongVariables(Vector(length, -length, length))));
cube.addVertex(Vertex(Vector(length, length, length), TextureCoordinate(1.0f, 1.0f), colors[1], normal,
PhongVariables(Vector(length, length, length))));
cube.addVertex(Vertex(Vector(-length, -length, length), TextureCoordinate(0.0f, 0.0f), colors[1], normal,
PhongVariables(Vector(-length, -length, length))));
cube.addVertex(Vertex(Vector(-length, length, length), TextureCoordinate(0.0f, 1.0f), colors[1], normal,
PhongVariables(Vector(-length, length, length))));
cube.addVertex(Vertex(Vector(length, length, length), TextureCoordinate(1.0f, 1.0f), colors[1], normal,
PhongVariables(Vector(length, length, length))));
normal = Vector(-1.0f, 0.0f, 0.0f);
cube.addVertex(Vertex(Vector(-length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[2], normal,
PhongVariables(Vector(-length, -length, -length))));
cube.addVertex(Vertex(Vector(-length, -length, length), TextureCoordinate(1.0f, 0.0f), colors[2], normal,
PhongVariables(Vector(-length, -length, length))));
cube.addVertex(Vertex(Vector(-length, length, length), TextureCoordinate(1.0f, 1.0f), colors[2], normal,
PhongVariables(Vector(-length, length, length))));
cube.addVertex(Vertex(Vector(-length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[2], normal,
PhongVariables(Vector(-length, -length, -length))));
cube.addVertex(Vertex(Vector(-length, length, length), TextureCoordinate(1.0f, 1.0f), colors[2], normal,
PhongVariables(Vector(-length, length, length))));
cube.addVertex(Vertex(Vector(-length, length, -length), TextureCoordinate(0.0f, 1.0f), colors[2], normal,
PhongVariables(Vector(-length, length, -length))));
normal = Vector(1.0f, 0.0f, 0.0f);
cube.addVertex(Vertex(Vector(length, -length, length), TextureCoordinate(0.0f, 0.0f), colors[3], normal,
PhongVariables(Vector(length, -length, length))));
cube.addVertex(Vertex(Vector(length, -length, -length), TextureCoordinate(1.0f, 0.0f), colors[3], normal,
PhongVariables(Vector(length, -length, -length))));
cube.addVertex(Vertex(Vector(length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[3], normal,
PhongVariables(Vector(length, length, -length))));
cube.addVertex(Vertex(Vector(length, -length, length), TextureCoordinate(0.0f, 0.0f), colors[3], normal,
PhongVariables(Vector(length, -length, length))));
cube.addVertex(Vertex(Vector(length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[3], normal,
PhongVariables(Vector(length, length, -length))));
cube.addVertex(Vertex(Vector(length, length, length), TextureCoordinate(0.0f, 1.0f), colors[3], normal,
PhongVariables(Vector(length, length, length))));
normal = Vector(0.0f, -1.0f, 0.0f);
cube.addVertex(Vertex(Vector(-length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[4], normal,
PhongVariables(Vector(-length, -length, -length))));
cube.addVertex(Vertex(Vector(length, -length, -length), TextureCoordinate(1.0f, 0.0f), colors[4], normal,
PhongVariables(Vector(length, -length, -length))));
cube.addVertex(Vertex(Vector(length, -length, length), TextureCoordinate(1.0f, 1.0f), colors[4], normal,
PhongVariables(Vector(length, -length, length))));
cube.addVertex(Vertex(Vector(-length, -length, -length), TextureCoordinate(0.0f, 0.0f), colors[4], normal,
PhongVariables(Vector(-length, -length, -length))));
cube.addVertex(Vertex(Vector(length, -length, length), TextureCoordinate(1.0f, 1.0f), colors[4], normal,
PhongVariables(Vector(length, -length, length))));
cube.addVertex(Vertex(Vector(-length, -length, length), TextureCoordinate(0.0f, 1.0f), colors[4], normal,
PhongVariables(Vector(-length, -length, length))));
normal = Vector(0.0f, 1.0f, 0.0f);
cube.addVertex(Vertex(Vector(-length, length, length), TextureCoordinate(0.0f, 0.0f), colors[5], normal,
PhongVariables(Vector(-length, length, length))));
cube.addVertex(Vertex(Vector(length, length, length), TextureCoordinate(1.0f, 0.0f), colors[5], normal,
PhongVariables(Vector(length, length, length))));
cube.addVertex(Vertex(Vector(length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[5], normal,
PhongVariables(Vector(length, length, -length))));
cube.addVertex(Vertex(Vector(-length, length, length), TextureCoordinate(0.0f, 0.0f), colors[5], normal,
PhongVariables(Vector(-length, length, length))));
cube.addVertex(Vertex(Vector(length, length, -length), TextureCoordinate(1.0f, 1.0f), colors[5], normal,
PhongVariables(Vector(length, length, -length))));
cube.addVertex(Vertex(Vector(-length, length, -length), TextureCoordinate(0.0f, 1.0f), colors[5], normal,
PhongVariables(Vector(-length, length, -length))));
cube.build();
return cube;
}
Mesh createSphere(float radius, float angle_step) {
Mesh sphere;
int count = (int) lround(360.0f / angle_step);
angle_step = 2.0f * 3.1415926f / count;
float theta = 0.0f, phi;
float theta_next, phi_next;
Vector pos11, pos12, pos21, pos22;
TextureCoordinate tex11, tex12, tex21, tex22;
Color color(0.6, 0.24, 0.3);
// phi and theta are exchanged here
while (theta < 2.0f * 3.1415926f) {
theta_next = theta + angle_step;
if (theta_next >= 2.0f * 3.1415926f) break;
phi = -0.5f * 3.1415926f;
while (phi < 0.5f * 3.1415926f) {
phi_next = phi + angle_step;
if (phi_next >= 0.5f * 3.1415926f) break;
pos11 = Vector(cosf(phi) * cosf(theta), sinf(phi), cosf(phi) * sinf(theta)) * radius;
pos12 = Vector(cosf(phi) * cosf(theta_next), sinf(phi), cosf(phi) * sinf(theta_next)) * radius;
pos21 = Vector(cosf(phi_next) * cosf(theta), sinf(phi_next), cosf(phi_next) * sinf(theta)) * radius;
pos22 = Vector(cosf(phi_next) * cosf(theta_next), sinf(phi_next), cosf(phi_next) * sinf(theta_next)) *
radius;
tex11 = TextureCoordinate(theta, phi);
tex12 = TextureCoordinate(theta_next, phi);
tex21 = TextureCoordinate(theta, phi_next);
tex22 = TextureCoordinate(theta_next, phi_next);
sphere.addVertex(
Vertex(
pos11,
tex11,
color,
pos11,
PhongVariables(pos11)
)
);
sphere.addVertex(
Vertex(
pos12,
tex12,
color,
pos12,
PhongVariables(pos12)
)
);
sphere.addVertex(
Vertex(
pos22,
tex22,
color,
pos22,
PhongVariables(pos22)
)
);
sphere.addVertex(
Vertex(
pos11,
tex11,
color,
pos11,
PhongVariables(pos11)
)
);
sphere.addVertex(
Vertex(
pos22,
tex22,
color,
pos22,
PhongVariables(pos22)
)
);
sphere.addVertex(
Vertex(
pos21,
tex21,
color,
pos21,
PhongVariables(pos21)
)
);
//phi +=45.0f;
phi = phi_next;
}
theta = theta_next;
}
sphere.build();
return sphere;
}
Vertex interp(const Vertex &v1, const Vertex &v2, float t) {
Vertex _v;
_v.pos = interp(v1.pos, v2.pos, t);
float z_inv = _v.pos.w;
float t2;
float z1 = 1 / v1.pos.w, z2 = 1 / v2.pos.w;
if (z1 < z2 + 0.0001f && z1 > z2 - 0.0001f)
t2 = t;
else
t2 = (1 / z_inv - 1 / v1.pos.w) / (1 / v2.pos.w - 1 / v1.pos.w);
//t2 = t;
// printf("%.3f <> %.3f \n", t, t2);
_v.texCoord.u = interp(v1.texCoord.u, v2.texCoord.u, t2);
_v.texCoord.v = interp(v1.texCoord.v, v2.texCoord.v, t2);
_v.color = interp(v1.color, v2.color, t2);
_v.normal = interp(v1.normal, v2.normal, t2); // TODO: normal interpolate needed
_v.shaderVariables = PhongVariables::variablesInterp(v1.shaderVariables, v2.shaderVariables,
t);
return _v;
}
}