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())
272 Radius(double(sidelength) / 2.0);
278 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
279 glm::dvec3 center(0.0f);
280 center[(surface + 0) % 3] = x + 0.5 - Radius();
281 center[(surface + 1) % 3] = y + 0.5 - Radius();
282 center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
286 void Planet::BuildVAO(const Set<TileType> &ts) {
288 vao.BindAttributes();
289 vao.EnableAttribute(0);
290 vao.EnableAttribute(1);
291 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
292 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
293 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
295 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
296 float offset = Radius();
299 // up +Z +X +Y -Z -X -Y
301 for (int index = 0, surface = 0; surface < 6; ++surface) {
302 for (int y = 0; y < sidelength; ++y) {
303 for (int x = 0; x < sidelength; ++x, ++index) {
304 float tex = ts[TileAt(surface, x, y).type].texture;
305 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
306 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
307 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
308 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
309 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
310 attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
311 attrib[4 * index + 0].tex_coord[1] = 1.0f;
312 attrib[4 * index + 0].tex_coord[2] = tex;
314 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
315 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
316 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
317 attrib[4 * index + 1].tex_coord[0] = tex_u_end;
318 attrib[4 * index + 1].tex_coord[1] = 1.0f;
319 attrib[4 * index + 1].tex_coord[2] = tex;
321 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
322 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
323 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
324 attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
325 attrib[4 * index + 2].tex_coord[1] = 0.0f;
326 attrib[4 * index + 2].tex_coord[2] = tex;
328 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
329 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
330 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
331 attrib[4 * index + 3].tex_coord[0] = tex_u_end;
332 attrib[4 * index + 3].tex_coord[1] = 0.0f;
333 attrib[4 * index + 3].tex_coord[2] = tex;
339 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
341 auto element = vao.MapElements(GL_WRITE_ONLY);
343 for (int surface = 0; surface < 3; ++surface) {
344 for (int y = 0; y < sidelength; ++y) {
345 for (int x = 0; x < sidelength; ++x, ++index) {
346 element[6 * index + 0] = 4 * index + 0;
347 element[6 * index + 1] = 4 * index + 2;
348 element[6 * index + 2] = 4 * index + 1;
349 element[6 * index + 3] = 4 * index + 1;
350 element[6 * index + 4] = 4 * index + 2;
351 element[6 * index + 5] = 4 * index + 3;
355 for (int surface = 3; surface < 6; ++surface) {
356 for (int y = 0; y < sidelength; ++y) {
357 for (int x = 0; x < sidelength; ++x, ++index) {
358 element[6 * index + 0] = 4 * index + 0;
359 element[6 * index + 1] = 4 * index + 1;
360 element[6 * index + 2] = 4 * index + 2;
361 element[6 * index + 3] = 4 * index + 2;
362 element[6 * index + 4] = 4 * index + 1;
363 element[6 * index + 5] = 4 * index + 3;
371 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
373 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
374 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
375 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
376 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
377 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
378 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
379 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
380 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
381 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
382 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
383 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
384 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
385 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
389 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
390 rand::SimplexNoise elevation_gen(0);
391 rand::SimplexNoise variation_gen(45623752346);
393 const int ice = tiles["ice"].id;
394 const int ocean = tiles["ocean"].id;
395 const int water = tiles["water"].id;
396 const int sand = tiles["sand"].id;
397 const int grass = tiles["grass"].id;
398 const int tundra = tiles["tundra"].id;
399 const int taiga = tiles["taiga"].id;
400 const int desert = tiles["desert"].id;
401 const int mntn = tiles["mountain"].id;
402 const int algae = tiles["algae"].id;
403 const int forest = tiles["forest"].id;
404 const int jungle = tiles["jungle"].id;
405 const int rock = tiles["rock"].id;
406 const int wheat = tiles["wheat"].id;
408 constexpr double ocean_thresh = -0.2;
409 constexpr double water_thresh = 0.0;
410 constexpr double beach_thresh = 0.05;
411 constexpr double highland_thresh = 0.4;
412 constexpr double mountain_thresh = 0.5;
414 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
415 const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
416 const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
417 const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
418 const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
420 for (int surface = 0; surface <= 5; ++surface) {
421 for (int y = 0; y < p.SideLength(); ++y) {
422 for (int x = 0; x < p.SideLength(); ++x) {
423 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
424 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
425 if (near_axis > cap_thresh) {
426 p.TileAt(surface, x, y).type = ice;
429 float elevation = rand::OctaveNoise(
431 to_tile / p.Radius(),
434 5 / p.Radius(), // frequency
438 float variation = rand::OctaveNoise(
440 to_tile / p.Radius(),
443 16 / p.Radius(), // frequency
447 if (elevation < ocean_thresh) {
448 p.TileAt(surface, x, y).type = ocean;
449 } else if (elevation < water_thresh) {
450 if (variation > 0.3) {
451 p.TileAt(surface, x, y).type = algae;
453 p.TileAt(surface, x, y).type = water;
455 } else if (elevation < beach_thresh) {
456 p.TileAt(surface, x, y).type = sand;
457 } else if (elevation < highland_thresh) {
458 if (near_axis < equ_thresh) {
459 if (variation > 0.6) {
460 p.TileAt(surface, x, y).type = grass;
461 } else if (variation > 0.2) {
462 p.TileAt(surface, x, y).type = sand;
464 p.TileAt(surface, x, y).type = desert;
466 } else if (near_axis < fzone_start) {
467 if (variation > 0.4) {
468 p.TileAt(surface, x, y).type = forest;
469 } else if (variation < -0.5) {
470 p.TileAt(surface, x, y).type = jungle;
471 } else if (variation > -0.02 && variation < 0.02) {
472 p.TileAt(surface, x, y).type = wheat;
474 p.TileAt(surface, x, y).type = grass;
476 } else if (near_axis < fzone_end) {
477 p.TileAt(surface, x, y).type = tundra;
479 p.TileAt(surface, x, y).type = taiga;
481 } else if (elevation < mountain_thresh) {
482 if (variation > 0.3) {
483 p.TileAt(surface, x, y).type = mntn;
485 p.TileAt(surface, x, y).type = rock;
488 p.TileAt(surface, x, y).type = mntn;
496 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
497 for (int surface = 0; surface <= 5; ++surface) {
498 for (int y = 0; y < p.SideLength(); ++y) {
499 for (int x = 0; x < p.SideLength(); ++x) {
500 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
501 p.TileAt(surface, x, y).type = surface;
503 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;