-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.cpp
More file actions
431 lines (349 loc) · 11.5 KB
/
scene.cpp
File metadata and controls
431 lines (349 loc) · 11.5 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "scene.h"
#include <cassert>
#include <cmath>
#include <random>
#include "fonts.h"
#include "utils.h"
using namespace ospcommon;
Scene::Scene()
{
// Create everything!
createWorld();
}
Scene::~Scene()
{
ospRelease(_spheresGeometry);
ospRelease(_world);
}
void Scene::generateSpheres(std::string text)
{
assert(_spheres.empty());
const float letterSize = 0.2f;
const float lineHeight = 0.3f;
const float pixelSize = letterSize / fonts::lettersWidth;
const float sphereRadius =
0.48f * pixelSize; // Just a bit of space between spheres
const float destX = -2.f;
const float destY = 0.5f;
const float destZ = 0;
// Create random number distributions
std::random_device rd{};
std::mt19937 gen{rd()};
std::uniform_real_distribution<float> xVelocityDistribution{-0.2f, 0.2f};
std::uniform_real_distribution<float> zVelocityDistribution{0.8f, 1.2f};
std::uniform_real_distribution<float> fadeOffSpeedDistribution{0.2f, 1.f};
// Render text and create a sphere for each pixel
fonts::renderText(text, [&](float x, float y) {
auto &s = _spheres.emplace_back(); // C++ 17 required
s.radius = s.refRadius = sphereRadius;
// Position
float yFract, yInt;
yFract = std::modff(y, &yInt);
s.center.x = destX + letterSize * x;
s.center.y = destY - letterSize * yFract - lineHeight * yInt;
s.center.z = destZ;
// Animation data
s.maxHeight = s.center.y;
s.endPos = s.center;
s.fadeOffDuration = fadeOffSpeedDistribution(gen);
s.velocity.x = xVelocityDistribution(gen);
s.velocity.y = -zVelocityDistribution(gen);
});
// Once we have all the spheres, set their colors to do a rainbow
float i = 0;
for (auto &s : _spheres)
{
auto rgb = utils::hsl2RGB(180 * i / _spheres.size(), 1, 0.5f);
s.color.x = rgb.x;
s.color.y = rgb.y;
s.color.z = rgb.z;
s.color.w = 1;
++i;
}
}
void Scene::computeAnimations()
{
const int numFrames = 150;
const float g = 9.81f;
for (auto &s : _spheres)
{
// Allocate memory for the all the frames
s.positions.resize(numFrames);
// Move the sphere away and store each position, the animation will be
// played backward
float t = 0;
vec3f pos = s.center;
for (auto rit = s.positions.rbegin(); rit != s.positions.rend(); ++rit)
{
const float maxHeight = 1 + s.maxHeight;
const float T = sqrtf(8.f * maxHeight / g);
const float Vmax = sqrtf(2.f * maxHeight * g);
const float tRemainder = std::fmodf(0.5f * T + t, T);
pos.y = -1.f + s.radius - 0.5f * g * tRemainder * tRemainder +
Vmax * tRemainder;
// Add some side movements
pos.x += _deltaTime * s.velocity.x;
pos.z += _deltaTime * s.velocity.y;
// Store result
*rit = pos;
t += _deltaTime;
}
}
}
bool Scene::AnimState::operator()(std::vector<Sphere> &spheres,
const float deltaTime)
{
bool done = false;
// Play animation for the current phase, and move to next phase when current
// phase is done
switch (phase)
{
case AnimPhase::playback:
{
if (!doPlayback(spheres))
{
phase = AnimPhase::wave;
_t0 = _t + deltaTime;
}
}
break;
case AnimPhase::wave:
if (!doWave(spheres))
{
phase = AnimPhase::delay;
_t0 = _t + deltaTime;
}
break;
case AnimPhase::delay:
{
if ((_t - _t0) >= 1)
{
phase = AnimPhase::fadeOut;
_t0 = _t + deltaTime;
}
}
break;
case AnimPhase::fadeOut:
{
if (!doFadeOut(spheres))
{
phase = AnimPhase::done;
}
}
break;
// Done
default:
assert(false);
case AnimPhase::done:
done = true;
break;
}
// Increment time
_t += deltaTime;
return !done;
}
bool Scene::AnimState::doPlayback(std::vector<Sphere> &spheres)
{
bool updated = false;
for (auto &s : spheres)
{
if (_playbackIndex < s.positions.size())
{
s.center = s.positions[_playbackIndex];
updated = true;
}
else
{
s.center = s.endPos;
}
}
++_playbackIndex;
return updated;
}
bool Scene::AnimState::doWave(std::vector<Sphere> &spheres)
{
bool updated = false;
if ((_waveX0 == _waveX1) && (!spheres.empty()))
{
_waveX0 = spheres.front().center.x;
_waveX1 = spheres.back().center.x;
}
float tRel = _t - _t0;
// Make a wave go across the spheres field
for (auto &s : spheres)
{
const float waveWidth = 0.5f;
const float waveStrength = 0.05f;
const float waveSpeed = 1.f;
const float scaleFactor = 2.f;
const float dx = (s.center.x - _waveX0) / (_waveX1 - _waveX0);
float tWave = tRel * waveSpeed - dx;
if ((tWave >= 0.f) && (tWave <= waveWidth))
{
tWave /= waveWidth;
const float wave =
1.f + std::sinf(float(std::_Pi) * (2 * tWave - 0.5f));
s.center.z = s.endPos.z + waveStrength * wave;
const float a = std::max(0.f, (tWave < 0.5f ? tWave : 1.f - tWave));
s.radius = s.refRadius * (1.f + scaleFactor * a);
updated = true;
}
}
return updated;
}
bool Scene::AnimState::doFadeOut(std::vector<Sphere> &spheres)
{
bool updated = false;
float tRel = _t - _t0;
// Fade out all the spheres
for (auto &s : spheres)
{
float scale = std::max(0.f, 1.f - tRel / s.fadeOffDuration);
s.radius = s.refRadius * scale;
if (scale > 0.f)
{
updated = true;
}
}
return updated;
}
OSPGeometry Scene::createSpheresGeometry()
{
generateSpheres("The Blue Brain\nProject is\nmindblowing!");
computeAnimations();
// create a data object with all the sphere information
OSPData spheresData = ospNewData(_spheres.size() * sizeof(Sphere),
OSP_UCHAR, _spheres.data());
// create the sphere geometry, and assign attributes
_spheresGeometry = ospNewGeometry("spheres");
ospSetData(_spheresGeometry, "spheres", spheresData);
ospSet1i(_spheresGeometry, "bytes_per_sphere", int(sizeof(Sphere)));
ospSet1i(_spheresGeometry, "offset_center", int(offsetof(Sphere, center)));
ospSet1i(_spheresGeometry, "offset_radius", int(offsetof(Sphere, radius)));
ospSetData(_spheresGeometry, "color", spheresData);
ospSet1i(_spheresGeometry, "color_offset", int(offsetof(Sphere, color)));
ospSet1i(_spheresGeometry, "color_format", int(OSP_FLOAT4));
ospSet1i(_spheresGeometry, "color_stride", int(sizeof(Sphere)));
// create alloy material and assign to geometry
OSPMaterial alloyMaterial = ospNewMaterial2("pathtracer", "Alloy");
ospCommit(alloyMaterial);
ospSetMaterial(_spheresGeometry, alloyMaterial);
// commit the spheres geometry
ospCommit(_spheresGeometry);
// release handles we no longer need
ospRelease(spheresData);
ospRelease(alloyMaterial);
return _spheresGeometry;
}
OSPGeometry Scene::createBackgroundGeometry()
{
OSPGeometry planeGeometry = ospNewGeometry("quads");
struct Vertex
{
vec3f position;
vec3f normal;
vec4f color;
};
struct QuadIndex
{
int x;
int y;
int z;
int w;
};
std::vector<Vertex> vertices;
std::vector<QuadIndex> quadIndices;
// ground plane
int startingIndex = vertices.size();
// extent of plane in the (x, y) directions
const float planeExtent = 20.f;
const float planeZ = -10;
const vec3f back = vec3f{0.f, 0.f, -1.f};
const vec4f gray = vec4f{0.05f, 0.05f, 0.05f, 1.f};
vertices.push_back(
Vertex{vec3f{-planeExtent, -planeExtent, planeZ}, back, gray});
vertices.push_back(
Vertex{vec3f{planeExtent, -planeExtent, planeZ}, back, gray});
vertices.push_back(
Vertex{vec3f{planeExtent, planeExtent, planeZ}, back, gray});
vertices.push_back(
Vertex{vec3f{-planeExtent, planeExtent, planeZ}, back, gray});
quadIndices.push_back(QuadIndex{startingIndex, startingIndex + 1,
startingIndex + 2, startingIndex + 3});
// create OSPRay data objects
std::vector<vec3f> positionVector;
std::vector<vec3f> normalVector;
std::vector<vec4f> colorVector;
std::transform(vertices.begin(), vertices.end(),
std::back_inserter(positionVector),
[](Vertex const &v) { return v.position; });
std::transform(vertices.begin(), vertices.end(),
std::back_inserter(normalVector),
[](Vertex const &v) { return v.normal; });
std::transform(vertices.begin(), vertices.end(),
std::back_inserter(colorVector),
[](Vertex const &v) { return v.color; });
OSPData positionData =
ospNewData(vertices.size(), OSP_FLOAT3, positionVector.data());
OSPData normalData =
ospNewData(vertices.size(), OSP_FLOAT3, normalVector.data());
OSPData colorData =
ospNewData(vertices.size(), OSP_FLOAT4, colorVector.data());
OSPData indexData =
ospNewData(quadIndices.size(), OSP_INT4, quadIndices.data());
// set vertex / index data on the geometry
ospSetData(planeGeometry, "vertex", positionData);
ospSetData(planeGeometry, "vertex.normal", normalData);
ospSetData(planeGeometry, "vertex.color", colorData);
ospSetData(planeGeometry, "index", indexData);
// create and assign a material to the geometry
OSPMaterial material = ospNewMaterial2("pathtracer", "OBJMaterial");
ospCommit(material);
ospSetMaterial(planeGeometry, material);
// finally, commit the geometry
ospCommit(planeGeometry);
// release handles we no longer need
ospRelease(positionData);
ospRelease(normalData);
ospRelease(colorData);
ospRelease(indexData);
ospRelease(material);
return planeGeometry;
}
void Scene::createWorld()
{
assert(_world == nullptr);
// create the "world" model which will contain all of our geometries
_world = ospNewModel();
// add in spheres geometry (100 of them)
ospAddGeometry(_world, createSpheresGeometry());
// add in background plane geometry
ospAddGeometry(_world, createBackgroundGeometry());
// commit the world model
ospCommit(_world);
}
void Scene::updateSpheresGeometry()
{
// create new spheres data for the updated center coordinates, and assign to
// geometry
OSPData spheresData = ospNewData(_spheres.size() * sizeof(Sphere),
OSP_UCHAR, _spheres.data());
ospSetData(_spheresGeometry, "spheres", spheresData);
// commit the updated spheres geometry
ospCommit(_spheresGeometry);
// release handles we no longer need
ospRelease(spheresData);
}
// updates the bouncing spheres' coordinates, geometry, and model
bool Scene::tick()
{
// update the spheres coordinates and geometry
if (_animState(_spheres, _deltaTime))
{
updateSpheresGeometry();
// commit the model since the spheres geometry changed
ospCommit(_world);
return true;
}
return false;
}