aboutsummaryrefslogtreecommitdiff
path: root/demos/smoke/Simulation.cpp
blob: 36e3f5b6f7811addef6c8d6c2596b3a5d48bbd48 (plain)
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
/*
 * Copyright (C) 2016 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cassert>
#include <cmath>
#include <array>
#include <glm/gtc/matrix_transform.hpp>
#include "Simulation.h"

namespace {

class MeshPicker {
public:
    MeshPicker() :
        pattern_({{
                Meshes::MESH_PYRAMID,
                Meshes::MESH_ICOSPHERE,
                Meshes::MESH_TEAPOT,
                Meshes::MESH_PYRAMID,
                Meshes::MESH_ICOSPHERE,
                Meshes::MESH_PYRAMID,
                Meshes::MESH_PYRAMID,
                Meshes::MESH_PYRAMID,
                Meshes::MESH_PYRAMID,
                Meshes::MESH_PYRAMID,
                }}), cur_(-1)
    {
    }

    Meshes::Type pick()
    {
        cur_ = (cur_ + 1) % pattern_.size();
        return pattern_[cur_];
    }

    float scale(Meshes::Type type) const
    {
        float base = 0.005f;

        switch (type) {
        case Meshes::MESH_PYRAMID:
        default:
            return base * 1.0f;
        case Meshes::MESH_ICOSPHERE:
            return base * 3.0f;
        case Meshes::MESH_TEAPOT:
            return base * 10.0f;
        }
    }

private:
    const std::array<Meshes::Type, 10> pattern_;
    int cur_;
};

class ColorPicker {
public:
    ColorPicker(unsigned int rng_seed) :
        rng_(rng_seed),
        red_(0.0f, 1.0f),
        green_(0.0f, 1.0f),
        blue_(0.0f, 1.0f)
    {
    }

    glm::vec3 pick()
    {
        return glm::vec3{ red_(rng_),
                          green_(rng_),
                          blue_(rng_) };
    }

private:
    std::mt19937 rng_;
    std::uniform_real_distribution<float> red_;
    std::uniform_real_distribution<float> green_;
    std::uniform_real_distribution<float> blue_;
};

} // namespace

Animation::Animation(unsigned int rng_seed, float scale)
    : rng_(rng_seed), dir_(-1.0f, 1.0f), speed_(0.1f, 1.0f)
{
    float x = dir_(rng_);
    float y = dir_(rng_);
    float z = dir_(rng_);
    if (std::abs(x) + std::abs(y) + std::abs(z) == 0.0f)
        x = 1.0f;

    current_.axis = glm::normalize(glm::vec3(x, y, z));

    current_.speed = speed_(rng_);
    current_.scale = scale;

    current_.matrix = glm::scale(glm::mat4(1.0f), glm::vec3(current_.scale));
}

glm::mat4 Animation::transformation(float t)
{
    current_.matrix = glm::rotate(current_.matrix, current_.speed * t, current_.axis);

    return current_.matrix;
}

class Curve {
public:
    virtual ~Curve() {}
    virtual glm::vec3 evaluate(float t) = 0;
};

namespace {

enum CurveType {
    CURVE_RANDOM,
    CURVE_CIRCLE,
    CURVE_COUNT,
};

class RandomCurve : public Curve {
public:
    RandomCurve(unsigned int rng_seed)
        : rng_(rng_seed), direction_(-0.3f, 0.3f), duration_(1.0f, 5.0f),
          segment_start_(0.0f), segment_direction_(0.0f),
          time_start_(0.0f), time_duration_(0.0f)
    {
    }

    glm::vec3 evaluate(float t)
    {
        if (t >= time_start_ + time_duration_)
            new_segment(t);

        pos_ += unit_dir_ * (t - last_);
        last_ = t;

        return pos_;
    }

private:
    void new_segment(float time_start)
    {
        segment_start_ += segment_direction_;
        segment_direction_ = glm::vec3(direction_(rng_),
                                       direction_(rng_),
                                       direction_(rng_));

        time_start_ = time_start;
        time_duration_ = duration_(rng_);

        unit_dir_ = segment_direction_ / time_duration_;
        pos_ = segment_start_;
        last_ = time_start_;
    }

    std::mt19937 rng_;
    std::uniform_real_distribution<float> direction_;
    std::uniform_real_distribution<float> duration_;

    glm::vec3 segment_start_;
    glm::vec3 segment_direction_;
    float time_start_;
    float time_duration_;

    glm::vec3 unit_dir_;
    glm::vec3 pos_;
    float last_;
};

class CircleCurve : public Curve {
public:
    CircleCurve(float radius, glm::vec3 axis)
        : r_(radius)
    {
        glm::vec3 a;

        if (axis.x != 0.0f) {
            a.x = -axis.z / axis.x;
            a.y = 0.0f;
            a.z = 1.0f;
        } else if (axis.y != 0.0f) {
            a.x = 1.0f;
            a.y = -axis.x / axis.y;
            a.z = 0.0f;
        } else {
            a.x = 1.0f;
            a.y = 0.0f;
            a.z = -axis.x / axis.z;
        }

        a_ = glm::normalize(a);
        b_ = glm::normalize(glm::cross(a_, axis));
    }

    glm::vec3 evaluate(float t)
    {
        return (a_ * (glm::vec3(std::cos(t)) - glm::vec3(1.0f)) + b_ * glm::vec3(std::sin(t))) *
            glm::vec3(r_);
    }

private:
    float r_;
    glm::vec3 a_;
    glm::vec3 b_;
};

} // namespace

Path::Path(unsigned int rng_seed)
    : rng_(rng_seed), type_(0, CURVE_COUNT - 1), duration_(5.0f, 20.0f)
{
    // trigger a subpath generation
    current_.end = -1.0f;
    current_.now = 0.0f;
}

glm::vec3 Path::position(float t)
{
    current_.now += t;

    while (current_.now >= current_.end)
        generate_subpath();

    return current_.origin + current_.curve->evaluate(current_.now - current_.start);
}

void Path::generate_subpath()
{
    float duration = duration_(rng_);
    CurveType type = static_cast<CurveType>(type_(rng_));

    if (current_.curve) {
        current_.origin += current_.curve->evaluate(current_.end - current_.start);
        current_.start = current_.end;
    } else {
        std::uniform_real_distribution<float> origin(0.0f, 2.0f);
        current_.origin = glm::vec3(origin(rng_), origin(rng_), origin(rng_));
        current_.start = current_.now;
    }

    current_.end = current_.start + duration;

    Curve *curve;

    switch (type) {
    case CURVE_RANDOM:
        curve = new RandomCurve(rng_());
        break;
    case CURVE_CIRCLE:
        {
            std::uniform_real_distribution<float> dir(-1.0f, 1.0f);
            glm::vec3 axis(dir(rng_), dir(rng_), dir(rng_));
            if (axis.x == 0.0f && axis.y == 0.0f && axis.z == 0.0f)
                axis.x = 1.0f;

            std::uniform_real_distribution<float> radius_(0.02f, 0.2f);
            curve = new CircleCurve(radius_(rng_), axis);
        }
        break;
    default:
        assert(!"unreachable");
        curve = nullptr;
        break;
    }

    current_.curve.reset(curve);
}

Simulation::Simulation(int object_count)
    : random_dev_()
{
    MeshPicker mesh;
    ColorPicker color(random_dev_());

    objects_.reserve(object_count);
    for (int i = 0; i < object_count; i++) {
        Meshes::Type type = mesh.pick();
        float scale = mesh.scale(type);

        objects_.emplace_back(Object{
            type, glm::vec3(0.5f + 0.5f * (float)i / object_count),
            color.pick(), Animation(random_dev_(), scale), Path(random_dev_()),
        });
    }
}

void Simulation::set_frame_data_size(uint32_t size)
{
    uint32_t offset = 0;
    for (auto &obj : objects_) {
        obj.frame_data_offset = offset;
        offset += size;
    }
}

void Simulation::update(float time, int begin, int end)
{
    for (int i = begin; i < end; i++) {
        auto &obj = objects_[i];

        glm::vec3 pos = obj.path.position(time);
        glm::mat4 trans = obj.animation.transformation(time);
        obj.model = glm::translate(glm::mat4(1.0f), pos) * trans;
    }
}