Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google, Inc. |
| 3 | * |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 7 | * |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 9 | * |
Jon Ashburn | 43b53e8 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <cassert> |
| 18 | #include <cmath> |
| 19 | #include <array> |
| 20 | #include <glm/gtc/matrix_transform.hpp> |
| 21 | #include "Simulation.h" |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class MeshPicker { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 26 | public: |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 27 | MeshPicker() |
| 28 | : pattern_({{ |
| 29 | Meshes::MESH_PYRAMID, Meshes::MESH_ICOSPHERE, Meshes::MESH_TEAPOT, Meshes::MESH_PYRAMID, Meshes::MESH_ICOSPHERE, |
| 30 | Meshes::MESH_PYRAMID, Meshes::MESH_PYRAMID, Meshes::MESH_PYRAMID, Meshes::MESH_PYRAMID, Meshes::MESH_PYRAMID, |
| 31 | }}), |
| 32 | cur_(-1) {} |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 33 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 34 | Meshes::Type pick() { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 35 | cur_ = (cur_ + 1) % pattern_.size(); |
| 36 | return pattern_[cur_]; |
| 37 | } |
| 38 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 39 | float scale(Meshes::Type type) const { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 40 | float base = 0.005f; |
| 41 | |
| 42 | switch (type) { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 43 | case Meshes::MESH_PYRAMID: |
| 44 | default: |
| 45 | return base * 1.0f; |
| 46 | case Meshes::MESH_ICOSPHERE: |
| 47 | return base * 3.0f; |
| 48 | case Meshes::MESH_TEAPOT: |
| 49 | return base * 10.0f; |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 53 | private: |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 54 | const std::array<Meshes::Type, 10> pattern_; |
| 55 | int cur_; |
| 56 | }; |
| 57 | |
| 58 | class ColorPicker { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 59 | public: |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 60 | ColorPicker(unsigned int rng_seed) : rng_(rng_seed), red_(0.0f, 1.0f), green_(0.0f, 1.0f), blue_(0.0f, 1.0f) {} |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 61 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 62 | glm::vec3 pick() { return glm::vec3{red_(rng_), green_(rng_), blue_(rng_)}; } |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 63 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 64 | private: |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 65 | std::mt19937 rng_; |
| 66 | std::uniform_real_distribution<float> red_; |
| 67 | std::uniform_real_distribution<float> green_; |
| 68 | std::uniform_real_distribution<float> blue_; |
| 69 | }; |
| 70 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 71 | } // namespace |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 72 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 73 | Animation::Animation(unsigned int rng_seed, float scale) : rng_(rng_seed), dir_(-1.0f, 1.0f), speed_(0.1f, 1.0f) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 74 | float x = dir_(rng_); |
| 75 | float y = dir_(rng_); |
| 76 | float z = dir_(rng_); |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 77 | if (std::abs(x) + std::abs(y) + std::abs(z) == 0.0f) x = 1.0f; |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 78 | |
| 79 | current_.axis = glm::normalize(glm::vec3(x, y, z)); |
| 80 | |
| 81 | current_.speed = speed_(rng_); |
| 82 | current_.scale = scale; |
| 83 | |
| 84 | current_.matrix = glm::scale(glm::mat4(1.0f), glm::vec3(current_.scale)); |
| 85 | } |
| 86 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 87 | glm::mat4 Animation::transformation(float t) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 88 | current_.matrix = glm::rotate(current_.matrix, current_.speed * t, current_.axis); |
| 89 | |
| 90 | return current_.matrix; |
| 91 | } |
| 92 | |
| 93 | class Curve { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 94 | public: |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 95 | virtual ~Curve() {} |
| 96 | virtual glm::vec3 evaluate(float t) = 0; |
| 97 | }; |
| 98 | |
| 99 | namespace { |
| 100 | |
| 101 | enum CurveType { |
| 102 | CURVE_RANDOM, |
| 103 | CURVE_CIRCLE, |
| 104 | CURVE_COUNT, |
| 105 | }; |
| 106 | |
| 107 | class RandomCurve : public Curve { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 108 | public: |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 109 | RandomCurve(unsigned int rng_seed) |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 110 | : rng_(rng_seed), |
| 111 | direction_(-0.3f, 0.3f), |
| 112 | duration_(1.0f, 5.0f), |
| 113 | segment_start_(0.0f), |
| 114 | segment_direction_(0.0f), |
| 115 | time_start_(0.0f), |
| 116 | time_duration_(0.0f) {} |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 117 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 118 | glm::vec3 evaluate(float t) { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 119 | if (t >= time_start_ + time_duration_) new_segment(t); |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 120 | |
| 121 | pos_ += unit_dir_ * (t - last_); |
| 122 | last_ = t; |
| 123 | |
| 124 | return pos_; |
| 125 | } |
| 126 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 127 | private: |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 128 | void new_segment(float time_start) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 129 | segment_start_ += segment_direction_; |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 130 | segment_direction_ = glm::vec3(direction_(rng_), direction_(rng_), direction_(rng_)); |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 131 | |
| 132 | time_start_ = time_start; |
| 133 | time_duration_ = duration_(rng_); |
| 134 | |
| 135 | unit_dir_ = segment_direction_ / time_duration_; |
| 136 | pos_ = segment_start_; |
| 137 | last_ = time_start_; |
| 138 | } |
| 139 | |
| 140 | std::mt19937 rng_; |
| 141 | std::uniform_real_distribution<float> direction_; |
| 142 | std::uniform_real_distribution<float> duration_; |
| 143 | |
| 144 | glm::vec3 segment_start_; |
| 145 | glm::vec3 segment_direction_; |
| 146 | float time_start_; |
| 147 | float time_duration_; |
| 148 | |
| 149 | glm::vec3 unit_dir_; |
| 150 | glm::vec3 pos_; |
| 151 | float last_; |
| 152 | }; |
| 153 | |
| 154 | class CircleCurve : public Curve { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 155 | public: |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 156 | CircleCurve(float radius, glm::vec3 axis) : r_(radius) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 157 | glm::vec3 a; |
| 158 | |
| 159 | if (axis.x != 0.0f) { |
| 160 | a.x = -axis.z / axis.x; |
| 161 | a.y = 0.0f; |
| 162 | a.z = 1.0f; |
| 163 | } else if (axis.y != 0.0f) { |
| 164 | a.x = 1.0f; |
| 165 | a.y = -axis.x / axis.y; |
| 166 | a.z = 0.0f; |
| 167 | } else { |
| 168 | a.x = 1.0f; |
| 169 | a.y = 0.0f; |
| 170 | a.z = -axis.x / axis.z; |
| 171 | } |
| 172 | |
| 173 | a_ = glm::normalize(a); |
| 174 | b_ = glm::normalize(glm::cross(a_, axis)); |
| 175 | } |
| 176 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 177 | glm::vec3 evaluate(float t) { |
| 178 | return (a_ * (glm::vec3(std::cos(t)) - glm::vec3(1.0f)) + b_ * glm::vec3(std::sin(t))) * glm::vec3(r_); |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 181 | private: |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 182 | float r_; |
| 183 | glm::vec3 a_; |
| 184 | glm::vec3 b_; |
| 185 | }; |
| 186 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 187 | } // namespace |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 188 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 189 | Path::Path(unsigned int rng_seed) : rng_(rng_seed), type_(0, CURVE_COUNT - 1), duration_(5.0f, 20.0f) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 190 | // trigger a subpath generation |
| 191 | current_.end = -1.0f; |
| 192 | current_.now = 0.0f; |
| 193 | } |
| 194 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 195 | glm::vec3 Path::position(float t) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 196 | current_.now += t; |
| 197 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 198 | while (current_.now >= current_.end) generate_subpath(); |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 199 | |
| 200 | return current_.origin + current_.curve->evaluate(current_.now - current_.start); |
| 201 | } |
| 202 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 203 | void Path::generate_subpath() { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 204 | float duration = duration_(rng_); |
| 205 | CurveType type = static_cast<CurveType>(type_(rng_)); |
| 206 | |
| 207 | if (current_.curve) { |
| 208 | current_.origin += current_.curve->evaluate(current_.end - current_.start); |
| 209 | current_.start = current_.end; |
| 210 | } else { |
| 211 | std::uniform_real_distribution<float> origin(0.0f, 2.0f); |
| 212 | current_.origin = glm::vec3(origin(rng_), origin(rng_), origin(rng_)); |
| 213 | current_.start = current_.now; |
| 214 | } |
| 215 | |
| 216 | current_.end = current_.start + duration; |
| 217 | |
| 218 | Curve *curve; |
| 219 | |
| 220 | switch (type) { |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 221 | case CURVE_RANDOM: |
| 222 | curve = new RandomCurve(rng_()); |
| 223 | break; |
| 224 | case CURVE_CIRCLE: { |
| 225 | std::uniform_real_distribution<float> dir(-1.0f, 1.0f); |
| 226 | glm::vec3 axis(dir(rng_), dir(rng_), dir(rng_)); |
| 227 | if (axis.x == 0.0f && axis.y == 0.0f && axis.z == 0.0f) axis.x = 1.0f; |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 228 | |
Mark Lobodzinski | cc7c305 | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 229 | std::uniform_real_distribution<float> radius_(0.02f, 0.2f); |
| 230 | curve = new CircleCurve(radius_(rng_), axis); |
| 231 | } break; |
| 232 | default: |
| 233 | assert(!"unreachable"); |
| 234 | curve = nullptr; |
| 235 | break; |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | current_.curve.reset(curve); |
| 239 | } |
| 240 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 241 | Simulation::Simulation(int object_count) : random_dev_() { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 242 | MeshPicker mesh; |
| 243 | ColorPicker color(random_dev_()); |
| 244 | |
| 245 | objects_.reserve(object_count); |
| 246 | for (int i = 0; i < object_count; i++) { |
| 247 | Meshes::Type type = mesh.pick(); |
| 248 | float scale = mesh.scale(type); |
| 249 | |
| 250 | objects_.emplace_back(Object{ |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 251 | type, glm::vec3(0.5f + 0.5f * (float)i / object_count), color.pick(), Animation(random_dev_(), scale), |
| 252 | Path(random_dev_()), |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 253 | }); |
| 254 | } |
| 255 | } |
| 256 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 257 | void Simulation::set_frame_data_size(uint32_t size) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 258 | uint32_t offset = 0; |
| 259 | for (auto &obj : objects_) { |
| 260 | obj.frame_data_offset = offset; |
| 261 | offset += size; |
| 262 | } |
| 263 | } |
| 264 | |
Mark Lobodzinski | bc9caa5 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 265 | void Simulation::update(float time, int begin, int end) { |
Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 266 | for (int i = begin; i < end; i++) { |
| 267 | auto &obj = objects_[i]; |
| 268 | |
| 269 | glm::vec3 pos = obj.path.position(time); |
| 270 | glm::mat4 trans = obj.animation.transformation(time); |
| 271 | obj.model = glm::translate(glm::mat4(1.0f), pos) * trans; |
| 272 | } |
| 273 | } |