4 #include "Simulation.hpp"
8 #include "TileType.hpp"
10 #include "../const.hpp"
11 #include "../app/Assets.hpp"
12 #include "../graphics/Viewport.hpp"
13 #include "../rand/OctaveNoise.hpp"
14 #include "../rand/SimplexNoise.hpp"
19 #include <glm/gtc/matrix_transform.hpp>
20 #include <glm/gtx/euler_angles.hpp>
21 #include <glm/gtx/io.hpp>
22 #include <glm/gtx/transform.hpp>
43 , surface_tilt(0.0, 0.0)
48 , inverse_orbital(1.0)
50 , inverse_local(1.0) {
56 void Body::SetSimulation(Simulation &s) noexcept {
58 for (auto child : children) {
59 child->SetSimulation(s);
63 void Body::SetParent(Body &p) {
68 parent->AddChild(*this);
71 void Body::UnsetParent() {
72 if (!HasParent()) return;
73 parent->RemoveChild(*this);
77 void Body::AddChild(Body &c) {
78 children.push_back(&c);
79 c.SetSimulation(*sim);
82 void Body::RemoveChild(Body &c) {
83 auto entry = std::find(children.begin(), children.end(), &c);
84 if (entry != children.end()) {
85 children.erase(entry);
89 double Body::Inertia() const noexcept {
90 // assume solid sphere for now
91 return (2.0/5.0) * Mass() * pow(Radius(), 2);
94 double Body::GravitationalParameter() const noexcept {
98 double Body::OrbitalPeriod() const noexcept {
100 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
106 double Body::RotationalPeriod() const noexcept {
107 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
108 return std::numeric_limits<double>::infinity();
110 return PI_2p0 * Inertia() / angular;
114 glm::dmat4 Body::ToUniverse() const noexcept {
116 const Body *b = this;
117 while (b->HasParent()) {
118 m = b->ToParent() * m;
124 glm::dmat4 Body::FromUniverse() const noexcept {
126 const Body *b = this;
127 while (b->HasParent()) {
128 m *= b->FromParent();
134 void Body::Cache() noexcept {
137 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
138 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
140 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
141 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
143 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
144 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
147 glm::eulerAngleY(rotation)
148 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
150 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
151 * glm::eulerAngleY(-rotation);
167 double Orbit::SemiMajorAxis() const noexcept {
171 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
176 double Orbit::Eccentricity() const noexcept {
180 Orbit &Orbit::Eccentricity(double e) noexcept {
185 double Orbit::Inclination() const noexcept {
189 Orbit &Orbit::Inclination(double i) noexcept {
194 double Orbit::LongitudeAscending() const noexcept {
198 Orbit &Orbit::LongitudeAscending(double l) noexcept {
203 double Orbit::ArgumentPeriapsis() const noexcept {
207 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
212 double Orbit::MeanAnomaly() const noexcept {
216 Orbit &Orbit::MeanAnomaly(double m) noexcept {
223 double mean2eccentric(double M, double e) {
224 double E = M; // eccentric anomaly, solve M = E - e sin E
225 // limit to 100 steps to prevent deadlocks in impossible situations
226 for (int i = 0; i < 100; ++i) {
227 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
229 if (abs(dE) < 1.0e-6) break;
236 glm::dmat4 Orbit::Matrix(double t) const noexcept {
238 double E = mean2eccentric(M, ecc);
240 // coordinates in orbital plane, P=x, Q=-z
241 double P = sma * (cos(E) - ecc);
242 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
244 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
247 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
249 double E = mean2eccentric(M, ecc);
250 double P = sma * (cos(E) - ecc);
251 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
252 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
256 Planet::Planet(int sidelength)
258 , sidelength(sidelength)
259 , tiles(new Tile[TilesTotal()])
261 Radius(double(sidelength) / 2.0);
267 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
268 glm::dvec3 center(0.0f);
269 center[(surface + 0) % 3] = x + 0.5 - Radius();
270 center[(surface + 1) % 3] = y + 0.5 - Radius();
271 center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
275 void Planet::BuildVAOs(const TileSet &ts) {
277 vao.BindAttributes();
278 vao.EnableAttribute(0);
279 vao.EnableAttribute(1);
280 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
281 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
282 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
284 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
285 float offset = Radius();
288 // up +Z +X +Y -Z -X -Y
290 for (int index = 0, surface = 0; surface < 6; ++surface) {
291 for (int y = 0; y < sidelength; ++y) {
292 for (int x = 0; x < sidelength; ++x, ++index) {
293 float tex = ts[TileAt(surface, x, y).type].texture;
294 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
295 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
296 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
297 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
298 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
299 attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
300 attrib[4 * index + 0].tex_coord[1] = 1.0f;
301 attrib[4 * index + 0].tex_coord[2] = tex;
303 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
304 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
305 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
306 attrib[4 * index + 1].tex_coord[0] = tex_u_end;
307 attrib[4 * index + 1].tex_coord[1] = 1.0f;
308 attrib[4 * index + 1].tex_coord[2] = tex;
310 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
311 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
312 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
313 attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
314 attrib[4 * index + 2].tex_coord[1] = 0.0f;
315 attrib[4 * index + 2].tex_coord[2] = tex;
317 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
318 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
319 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
320 attrib[4 * index + 3].tex_coord[0] = tex_u_end;
321 attrib[4 * index + 3].tex_coord[1] = 0.0f;
322 attrib[4 * index + 3].tex_coord[2] = tex;
328 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
330 auto element = vao.MapElements(GL_WRITE_ONLY);
332 for (int surface = 0; surface < 3; ++surface) {
333 for (int y = 0; y < sidelength; ++y) {
334 for (int x = 0; x < sidelength; ++x, ++index) {
335 element[6 * index + 0] = 4 * index + 0;
336 element[6 * index + 1] = 4 * index + 2;
337 element[6 * index + 2] = 4 * index + 1;
338 element[6 * index + 3] = 4 * index + 1;
339 element[6 * index + 4] = 4 * index + 2;
340 element[6 * index + 5] = 4 * index + 3;
344 for (int surface = 3; surface < 6; ++surface) {
345 for (int y = 0; y < sidelength; ++y) {
346 for (int x = 0; x < sidelength; ++x, ++index) {
347 element[6 * index + 0] = 4 * index + 0;
348 element[6 * index + 1] = 4 * index + 1;
349 element[6 * index + 2] = 4 * index + 2;
350 element[6 * index + 3] = 4 * index + 2;
351 element[6 * index + 4] = 4 * index + 1;
352 element[6 * index + 5] = 4 * index + 3;
360 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
362 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
363 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
364 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
365 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
366 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
367 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
368 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
369 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
370 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
371 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
372 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
373 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
374 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
378 void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
379 rand::SimplexNoise elevation_gen(0);
381 const int ice = tiles["ice"].id;
382 const int grass = tiles["grass"].id;
383 const int water = tiles["water"].id;
384 const int sand = tiles["sand"].id;
385 const int rock = tiles["rock"].id;
387 constexpr double water_thresh = 0.0;
388 constexpr double beach_thresh = 0.1;
389 constexpr double mountain_thresh = 0.5;
391 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
392 const double cap_thresh = std::cos(p.AxialTilt().x);
394 for (int surface = 0; surface <= 5; ++surface) {
395 for (int y = 0; y < p.SideLength(); ++y) {
396 for (int x = 0; x < p.SideLength(); ++x) {
397 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
398 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
399 if (near_axis > cap_thresh) {
400 p.TileAt(surface, x, y).type = ice;
403 float elevation = rand::OctaveNoise(
405 to_tile / p.Radius(),
408 2 / p.Radius(), // frequency
412 if (elevation < water_thresh) {
413 p.TileAt(surface, x, y).type = water;
414 } else if (elevation < beach_thresh) {
415 p.TileAt(surface, x, y).type = sand;
416 } else if (elevation < mountain_thresh) {
417 p.TileAt(surface, x, y).type = grass;
419 p.TileAt(surface, x, y).type = rock;
427 void GenerateTest(const TileSet &tiles, Planet &p) noexcept {
428 for (int surface = 0; surface <= 5; ++surface) {
429 for (int y = 0; y < p.SideLength(); ++y) {
430 for (int x = 0; x < p.SideLength(); ++x) {
431 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
432 p.TileAt(surface, x, y).type = surface;
434 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
455 TileSet::~TileSet() {
458 int TileSet::Add(const TileType &t) {
459 int id = types.size();
460 if (!names.emplace(t.name, id).second) {
461 throw std::runtime_error("duplicate tile type name " + t.name);
463 types.emplace_back(t);
464 types.back().id = id;
468 TileType &TileSet::operator [](const std::string &name) {
469 auto entry = names.find(name);
470 if (entry != names.end()) {
471 return types[entry->second];
473 throw std::runtime_error("unknown tile type " + name);
477 const TileType &TileSet::operator [](const std::string &name) const {
478 auto entry = names.find(name);
479 if (entry != names.end()) {
480 return types[entry->second];
482 throw std::runtime_error("unknown tile type " + name);