4 #include "Resource.hpp"
6 #include "Simulation.hpp"
9 #include "TileType.hpp"
11 #include "Creature.hpp"
12 #include "../const.hpp"
13 #include "../app/Assets.hpp"
14 #include "../graphics/Viewport.hpp"
15 #include "../rand/OctaveNoise.hpp"
16 #include "../rand/SimplexNoise.hpp"
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtx/euler_angles.hpp>
23 #include <glm/gtx/io.hpp>
24 #include <glm/gtx/transform.hpp>
45 , surface_tilt(0.0, 0.0)
50 , inverse_orbital(1.0)
57 for (Creature *c : creatures) {
62 void Body::SetSimulation(Simulation &s) noexcept {
64 for (auto child : children) {
65 child->SetSimulation(s);
69 void Body::SetParent(Body &p) {
74 parent->AddChild(*this);
77 void Body::UnsetParent() {
78 if (!HasParent()) return;
79 parent->RemoveChild(*this);
83 void Body::AddChild(Body &c) {
84 children.push_back(&c);
85 c.SetSimulation(*sim);
88 void Body::RemoveChild(Body &c) {
89 auto entry = std::find(children.begin(), children.end(), &c);
90 if (entry != children.end()) {
91 children.erase(entry);
95 double Body::Inertia() const noexcept {
96 // assume solid sphere for now
97 return (2.0/5.0) * Mass() * pow(Radius(), 2);
100 double Body::GravitationalParameter() const noexcept {
104 double Body::OrbitalPeriod() const noexcept {
106 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
112 double Body::RotationalPeriod() const noexcept {
113 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
114 return std::numeric_limits<double>::infinity();
116 return PI_2p0 * Inertia() / angular;
120 glm::dmat4 Body::ToUniverse() const noexcept {
122 const Body *b = this;
123 while (b->HasParent()) {
124 m = b->ToParent() * m;
130 glm::dmat4 Body::FromUniverse() const noexcept {
132 const Body *b = this;
133 while (b->HasParent()) {
134 m *= b->FromParent();
140 void Body::Cache() noexcept {
143 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
144 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
146 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
147 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
149 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
150 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
153 glm::eulerAngleY(rotation)
154 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
156 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
157 * glm::eulerAngleY(-rotation);
160 void Body::AddCreature(Creature *c) {
162 creatures.push_back(c);
178 double Orbit::SemiMajorAxis() const noexcept {
182 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
187 double Orbit::Eccentricity() const noexcept {
191 Orbit &Orbit::Eccentricity(double e) noexcept {
196 double Orbit::Inclination() const noexcept {
200 Orbit &Orbit::Inclination(double i) noexcept {
205 double Orbit::LongitudeAscending() const noexcept {
209 Orbit &Orbit::LongitudeAscending(double l) noexcept {
214 double Orbit::ArgumentPeriapsis() const noexcept {
218 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
223 double Orbit::MeanAnomaly() const noexcept {
227 Orbit &Orbit::MeanAnomaly(double m) noexcept {
234 double mean2eccentric(double M, double e) {
235 double E = M; // eccentric anomaly, solve M = E - e sin E
236 // limit to 100 steps to prevent deadlocks in impossible situations
237 for (int i = 0; i < 100; ++i) {
238 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
240 if (abs(dE) < 1.0e-6) break;
247 glm::dmat4 Orbit::Matrix(double t) const noexcept {
249 double E = mean2eccentric(M, ecc);
251 // coordinates in orbital plane, P=x, Q=-z
252 double P = sma * (cos(E) - ecc);
253 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
255 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
258 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
260 double E = mean2eccentric(M, ecc);
261 double P = sma * (cos(E) - ecc);
262 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
263 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
267 Planet::Planet(int sidelength)
269 , sidelength(sidelength)
270 , tiles(TilesTotal())
273 Radius(double(sidelength) / 2.0);
279 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
280 glm::dvec3 center(0.0f);
281 center[(surface + 0) % 3] = x + 0.5 - Radius();
282 center[(surface + 1) % 3] = y + 0.5 - Radius();
283 center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
287 void Planet::BuildVAO(const Set<TileType> &ts) {
289 vao.BindAttributes();
290 vao.EnableAttribute(0);
291 vao.EnableAttribute(1);
292 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
293 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
294 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
296 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
297 float offset = Radius();
300 // up +Z +X +Y -Z -X -Y
302 for (int index = 0, surface = 0; surface < 6; ++surface) {
303 for (int y = 0; y < sidelength; ++y) {
304 for (int x = 0; x < sidelength; ++x, ++index) {
305 float tex = ts[TileAt(surface, x, y).type].texture;
306 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
307 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
308 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
309 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
310 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
311 attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
312 attrib[4 * index + 0].tex_coord[1] = 1.0f;
313 attrib[4 * index + 0].tex_coord[2] = tex;
315 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
316 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
317 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
318 attrib[4 * index + 1].tex_coord[0] = tex_u_end;
319 attrib[4 * index + 1].tex_coord[1] = 1.0f;
320 attrib[4 * index + 1].tex_coord[2] = tex;
322 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
323 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
324 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
325 attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
326 attrib[4 * index + 2].tex_coord[1] = 0.0f;
327 attrib[4 * index + 2].tex_coord[2] = tex;
329 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
330 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
331 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
332 attrib[4 * index + 3].tex_coord[0] = tex_u_end;
333 attrib[4 * index + 3].tex_coord[1] = 0.0f;
334 attrib[4 * index + 3].tex_coord[2] = tex;
340 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
342 auto element = vao.MapElements(GL_WRITE_ONLY);
344 for (int surface = 0; surface < 3; ++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 + 2;
349 element[6 * index + 2] = 4 * index + 1;
350 element[6 * index + 3] = 4 * index + 1;
351 element[6 * index + 4] = 4 * index + 2;
352 element[6 * index + 5] = 4 * index + 3;
356 for (int surface = 3; surface < 6; ++surface) {
357 for (int y = 0; y < sidelength; ++y) {
358 for (int x = 0; x < sidelength; ++x, ++index) {
359 element[6 * index + 0] = 4 * index + 0;
360 element[6 * index + 1] = 4 * index + 1;
361 element[6 * index + 2] = 4 * index + 2;
362 element[6 * index + 3] = 4 * index + 2;
363 element[6 * index + 4] = 4 * index + 1;
364 element[6 * index + 5] = 4 * index + 3;
372 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
374 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
375 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
376 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
377 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
378 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
379 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
380 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
381 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
382 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
383 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
384 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
385 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
386 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
390 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
391 rand::SimplexNoise elevation_gen(0);
392 rand::SimplexNoise variation_gen(45623752346);
394 const int ice = tiles["ice"].id;
395 const int ocean = tiles["ocean"].id;
396 const int water = tiles["water"].id;
397 const int sand = tiles["sand"].id;
398 const int grass = tiles["grass"].id;
399 const int tundra = tiles["tundra"].id;
400 const int taiga = tiles["taiga"].id;
401 const int desert = tiles["desert"].id;
402 const int mntn = tiles["mountain"].id;
403 const int algae = tiles["algae"].id;
404 const int forest = tiles["forest"].id;
405 const int jungle = tiles["jungle"].id;
406 const int rock = tiles["rock"].id;
407 const int wheat = tiles["wheat"].id;
409 constexpr double ocean_thresh = -0.2;
410 constexpr double water_thresh = 0.0;
411 constexpr double beach_thresh = 0.05;
412 constexpr double highland_thresh = 0.4;
413 constexpr double mountain_thresh = 0.5;
415 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
416 const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
417 const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
418 const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
419 const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
421 for (int surface = 0; surface <= 5; ++surface) {
422 for (int y = 0; y < p.SideLength(); ++y) {
423 for (int x = 0; x < p.SideLength(); ++x) {
424 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
425 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
426 if (near_axis > cap_thresh) {
427 p.TileAt(surface, x, y).type = ice;
430 float elevation = rand::OctaveNoise(
432 to_tile / p.Radius(),
435 5 / p.Radius(), // frequency
439 float variation = rand::OctaveNoise(
441 to_tile / p.Radius(),
444 16 / p.Radius(), // frequency
448 if (elevation < ocean_thresh) {
449 p.TileAt(surface, x, y).type = ocean;
450 } else if (elevation < water_thresh) {
451 if (variation > 0.3) {
452 p.TileAt(surface, x, y).type = algae;
454 p.TileAt(surface, x, y).type = water;
456 } else if (elevation < beach_thresh) {
457 p.TileAt(surface, x, y).type = sand;
458 } else if (elevation < highland_thresh) {
459 if (near_axis < equ_thresh) {
460 if (variation > 0.6) {
461 p.TileAt(surface, x, y).type = grass;
462 } else if (variation > 0.2) {
463 p.TileAt(surface, x, y).type = sand;
465 p.TileAt(surface, x, y).type = desert;
467 } else if (near_axis < fzone_start) {
468 if (variation > 0.4) {
469 p.TileAt(surface, x, y).type = forest;
470 } else if (variation < -0.5) {
471 p.TileAt(surface, x, y).type = jungle;
472 } else if (variation > -0.02 && variation < 0.02) {
473 p.TileAt(surface, x, y).type = wheat;
475 p.TileAt(surface, x, y).type = grass;
477 } else if (near_axis < fzone_end) {
478 p.TileAt(surface, x, y).type = tundra;
480 p.TileAt(surface, x, y).type = taiga;
482 } else if (elevation < mountain_thresh) {
483 if (variation > 0.3) {
484 p.TileAt(surface, x, y).type = mntn;
486 p.TileAt(surface, x, y).type = rock;
489 p.TileAt(surface, x, y).type = mntn;
497 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
498 for (int surface = 0; surface <= 5; ++surface) {
499 for (int y = 0; y < p.SideLength(); ++y) {
500 for (int x = 0; x < p.SideLength(); ++x) {
501 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
502 p.TileAt(surface, x, y).type = surface;
504 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;