4 #include "Resource.hpp"
6 #include "Simulation.hpp"
9 #include "TileType.hpp"
11 #include "../const.hpp"
12 #include "../app/Assets.hpp"
13 #include "../creature/Creature.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)
58 for (creature::Creature *c : creatures) {
63 void Body::SetSimulation(Simulation &s) noexcept {
65 for (auto child : children) {
66 child->SetSimulation(s);
70 void Body::SetParent(Body &p) {
75 parent->AddChild(*this);
78 void Body::UnsetParent() {
79 if (!HasParent()) return;
80 parent->RemoveChild(*this);
84 void Body::AddChild(Body &c) {
85 children.push_back(&c);
86 c.SetSimulation(*sim);
89 void Body::RemoveChild(Body &c) {
90 auto entry = std::find(children.begin(), children.end(), &c);
91 if (entry != children.end()) {
92 children.erase(entry);
96 double Body::Inertia() const noexcept {
97 // assume solid sphere for now
98 return (2.0/5.0) * Mass() * pow(Radius(), 2);
101 double Body::GravitationalParameter() const noexcept {
105 double Body::OrbitalPeriod() const noexcept {
107 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
113 double Body::RotationalPeriod() const noexcept {
114 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
115 return std::numeric_limits<double>::infinity();
117 return PI_2p0 * Inertia() / angular;
121 glm::dmat4 Body::ToUniverse() const noexcept {
123 const Body *b = this;
124 while (b->HasParent()) {
125 m = b->ToParent() * m;
131 glm::dmat4 Body::FromUniverse() const noexcept {
133 const Body *b = this;
134 while (b->HasParent()) {
135 m *= b->FromParent();
141 void Body::Tick(double dt) {
142 rotation += dt * AngularMomentum() / Inertia();
144 for (creature::Creature *c : Creatures()) {
149 void Body::Cache() noexcept {
152 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
153 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
155 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
156 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
158 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
159 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
162 glm::eulerAngleY(rotation)
163 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
165 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
166 * glm::eulerAngleY(-rotation);
169 void Body::AddCreature(creature::Creature *c) {
170 creatures.push_back(c);
173 void Body::RemoveCreature(creature::Creature *c) {
174 auto entry = std::find(creatures.begin(), creatures.end(), c);
175 if (entry != creatures.end()) {
176 creatures.erase(entry);
193 double Orbit::SemiMajorAxis() const noexcept {
197 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
202 double Orbit::Eccentricity() const noexcept {
206 Orbit &Orbit::Eccentricity(double e) noexcept {
211 double Orbit::Inclination() const noexcept {
215 Orbit &Orbit::Inclination(double i) noexcept {
220 double Orbit::LongitudeAscending() const noexcept {
224 Orbit &Orbit::LongitudeAscending(double l) noexcept {
229 double Orbit::ArgumentPeriapsis() const noexcept {
233 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
238 double Orbit::MeanAnomaly() const noexcept {
242 Orbit &Orbit::MeanAnomaly(double m) noexcept {
249 double mean2eccentric(double M, double e) {
250 double E = M; // eccentric anomaly, solve M = E - e sin E
251 // limit to 100 steps to prevent deadlocks in impossible situations
252 for (int i = 0; i < 100; ++i) {
253 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
255 if (abs(dE) < 1.0e-6) break;
262 glm::dmat4 Orbit::Matrix(double t) const noexcept {
264 double E = mean2eccentric(M, ecc);
266 // coordinates in orbital plane, P=x, Q=-z
267 double P = sma * (cos(E) - ecc);
268 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
270 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
273 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
275 double E = mean2eccentric(M, ecc);
276 double P = sma * (cos(E) - ecc);
277 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
278 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
282 Planet::Planet(int sidelength)
284 , sidelength(sidelength)
285 , tiles(TilesTotal())
287 Radius(double(sidelength) / 2.0);
293 const TileType &Planet::TypeAt(int surface, int x, int y) const {
294 return GetSimulation().TileTypes()[TileAt(surface, x, y).type];
297 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
298 glm::dvec3 center(0.0f);
299 center[(surface + 0) % 3] = x + 0.5 - Radius();
300 center[(surface + 1) % 3] = y + 0.5 - Radius();
301 center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
305 void Planet::BuildVAO(const Set<TileType> &ts) {
307 vao.BindAttributes();
308 vao.EnableAttribute(0);
309 vao.EnableAttribute(1);
310 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
311 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
312 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
314 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
315 float offset = Radius();
318 // up +Z +X +Y -Z -X -Y
320 for (int index = 0, surface = 0; surface < 6; ++surface) {
321 for (int y = 0; y < sidelength; ++y) {
322 for (int x = 0; x < sidelength; ++x, ++index) {
323 float tex = ts[TileAt(surface, x, y).type].texture;
324 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
325 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
326 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
327 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
328 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
329 attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
330 attrib[4 * index + 0].tex_coord[1] = 1.0f;
331 attrib[4 * index + 0].tex_coord[2] = tex;
333 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
334 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
335 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
336 attrib[4 * index + 1].tex_coord[0] = tex_u_end;
337 attrib[4 * index + 1].tex_coord[1] = 1.0f;
338 attrib[4 * index + 1].tex_coord[2] = tex;
340 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
341 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
342 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
343 attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
344 attrib[4 * index + 2].tex_coord[1] = 0.0f;
345 attrib[4 * index + 2].tex_coord[2] = tex;
347 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
348 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
349 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
350 attrib[4 * index + 3].tex_coord[0] = tex_u_end;
351 attrib[4 * index + 3].tex_coord[1] = 0.0f;
352 attrib[4 * index + 3].tex_coord[2] = tex;
358 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
360 auto element = vao.MapElements(GL_WRITE_ONLY);
362 for (int surface = 0; surface < 3; ++surface) {
363 for (int y = 0; y < sidelength; ++y) {
364 for (int x = 0; x < sidelength; ++x, ++index) {
365 element[6 * index + 0] = 4 * index + 0;
366 element[6 * index + 1] = 4 * index + 2;
367 element[6 * index + 2] = 4 * index + 1;
368 element[6 * index + 3] = 4 * index + 1;
369 element[6 * index + 4] = 4 * index + 2;
370 element[6 * index + 5] = 4 * index + 3;
374 for (int surface = 3; surface < 6; ++surface) {
375 for (int y = 0; y < sidelength; ++y) {
376 for (int x = 0; x < sidelength; ++x, ++index) {
377 element[6 * index + 0] = 4 * index + 0;
378 element[6 * index + 1] = 4 * index + 1;
379 element[6 * index + 2] = 4 * index + 2;
380 element[6 * index + 3] = 4 * index + 2;
381 element[6 * index + 4] = 4 * index + 1;
382 element[6 * index + 5] = 4 * index + 3;
390 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
392 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
393 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
394 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
395 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
396 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
397 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
398 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
399 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
400 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
401 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
402 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
403 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
404 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
408 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
409 rand::SimplexNoise elevation_gen(0);
410 rand::SimplexNoise variation_gen(45623752346);
412 const int ice = tiles["ice"].id;
413 const int ocean = tiles["ocean"].id;
414 const int water = tiles["water"].id;
415 const int sand = tiles["sand"].id;
416 const int grass = tiles["grass"].id;
417 const int tundra = tiles["tundra"].id;
418 const int taiga = tiles["taiga"].id;
419 const int desert = tiles["desert"].id;
420 const int mntn = tiles["mountain"].id;
421 const int algae = tiles["algae"].id;
422 const int forest = tiles["forest"].id;
423 const int jungle = tiles["jungle"].id;
424 const int rock = tiles["rock"].id;
425 const int wheat = tiles["wheat"].id;
427 constexpr double ocean_thresh = -0.2;
428 constexpr double water_thresh = 0.0;
429 constexpr double beach_thresh = 0.05;
430 constexpr double highland_thresh = 0.4;
431 constexpr double mountain_thresh = 0.5;
433 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
434 const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
435 const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
436 const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
437 const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
439 for (int surface = 0; surface <= 5; ++surface) {
440 for (int y = 0; y < p.SideLength(); ++y) {
441 for (int x = 0; x < p.SideLength(); ++x) {
442 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
443 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
444 if (near_axis > cap_thresh) {
445 p.TileAt(surface, x, y).type = ice;
448 float elevation = rand::OctaveNoise(
450 to_tile / p.Radius(),
453 5 / p.Radius(), // frequency
457 float variation = rand::OctaveNoise(
459 to_tile / p.Radius(),
462 16 / p.Radius(), // frequency
466 if (elevation < ocean_thresh) {
467 p.TileAt(surface, x, y).type = ocean;
468 } else if (elevation < water_thresh) {
469 if (variation > 0.3) {
470 p.TileAt(surface, x, y).type = algae;
472 p.TileAt(surface, x, y).type = water;
474 } else if (elevation < beach_thresh) {
475 p.TileAt(surface, x, y).type = sand;
476 } else if (elevation < highland_thresh) {
477 if (near_axis < equ_thresh) {
478 if (variation > 0.6) {
479 p.TileAt(surface, x, y).type = grass;
480 } else if (variation > 0.2) {
481 p.TileAt(surface, x, y).type = sand;
483 p.TileAt(surface, x, y).type = desert;
485 } else if (near_axis < fzone_start) {
486 if (variation > 0.4) {
487 p.TileAt(surface, x, y).type = forest;
488 } else if (variation < -0.5) {
489 p.TileAt(surface, x, y).type = jungle;
490 } else if (variation > -0.02 && variation < 0.02) {
491 p.TileAt(surface, x, y).type = wheat;
493 p.TileAt(surface, x, y).type = grass;
495 } else if (near_axis < fzone_end) {
496 p.TileAt(surface, x, y).type = tundra;
498 p.TileAt(surface, x, y).type = taiga;
500 } else if (elevation < mountain_thresh) {
501 if (variation > 0.3) {
502 p.TileAt(surface, x, y).type = mntn;
504 p.TileAt(surface, x, y).type = rock;
507 p.TileAt(surface, x, y).type = mntn;
515 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
516 for (int surface = 0; surface <= 5; ++surface) {
517 for (int y = 0; y < p.SideLength(); ++y) {
518 for (int x = 0; x < p.SideLength(); ++x) {
519 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
520 p.TileAt(surface, x, y).type = surface;
522 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
539 std::vector<TileType::Yield>::const_iterator TileType::FindResource(int r) const {
540 auto yield = resources.cbegin();
541 for (; yield != resources.cend(); ++yield) {
542 if (yield->resource == r) {