Tony Barbour | 2f18b29 | 2016-02-25 15:44:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | * DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef SIMULATION_H |
| 24 | #define SIMULATION_H |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <random> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include <glm/glm.hpp> |
| 31 | |
| 32 | #include "Meshes.h" |
| 33 | |
| 34 | class Animation { |
| 35 | public: |
| 36 | Animation(unsigned rng_seed, float scale); |
| 37 | |
| 38 | glm::mat4 transformation(float t); |
| 39 | |
| 40 | private: |
| 41 | struct Data { |
| 42 | glm::vec3 axis; |
| 43 | float speed; |
| 44 | float scale; |
| 45 | |
| 46 | glm::mat4 matrix; |
| 47 | }; |
| 48 | |
| 49 | std::mt19937 rng_; |
| 50 | std::uniform_real_distribution<float> dir_; |
| 51 | std::uniform_real_distribution<float> speed_; |
| 52 | |
| 53 | Data current_; |
| 54 | }; |
| 55 | |
| 56 | class Curve; |
| 57 | |
| 58 | class Path { |
| 59 | public: |
| 60 | Path(unsigned rng_seed); |
| 61 | |
| 62 | glm::vec3 position(float t); |
| 63 | |
| 64 | private: |
| 65 | struct Subpath { |
| 66 | glm::vec3 origin; |
| 67 | float start; |
| 68 | float end; |
| 69 | float now; |
| 70 | |
| 71 | std::shared_ptr<Curve> curve; |
| 72 | }; |
| 73 | |
| 74 | void generate_subpath(); |
| 75 | |
| 76 | std::mt19937 rng_; |
| 77 | std::uniform_int_distribution<> type_; |
| 78 | std::uniform_real_distribution<float> duration_; |
| 79 | |
| 80 | Subpath current_; |
| 81 | }; |
| 82 | |
| 83 | class Simulation { |
| 84 | public: |
| 85 | Simulation(int object_count); |
| 86 | |
| 87 | struct Object { |
| 88 | Meshes::Type mesh; |
| 89 | glm::vec3 light_pos; |
| 90 | glm::vec3 light_color; |
| 91 | |
| 92 | Animation animation; |
| 93 | Path path; |
| 94 | |
| 95 | uint32_t frame_data_offset; |
| 96 | |
| 97 | glm::mat4 model; |
| 98 | }; |
| 99 | |
| 100 | const std::vector<Object> &objects() const { return objects_; } |
| 101 | |
| 102 | unsigned int rng_seed() { return random_dev_(); } |
| 103 | |
| 104 | void set_frame_data_size(uint32_t size); |
| 105 | void update(float time, int begin, int end); |
| 106 | |
| 107 | private: |
| 108 | std::random_device random_dev_; |
| 109 | std::vector<Object> objects_; |
| 110 | }; |
| 111 | |
| 112 | #endif // SIMULATION_H |