4 #include "Simulation.hpp"
8 #include "TileType.hpp"
10 #include "Creature.hpp"
11 #include "../const.hpp"
12 #include "../app/Assets.hpp"
13 #include "../graphics/Viewport.hpp"
14 #include "../rand/OctaveNoise.hpp"
15 #include "../rand/SimplexNoise.hpp"
20 #include <glm/gtc/matrix_transform.hpp>
21 #include <glm/gtx/euler_angles.hpp>
22 #include <glm/gtx/io.hpp>
23 #include <glm/gtx/transform.hpp>
44 , surface_tilt(0.0, 0.0)
49 , inverse_orbital(1.0)
56 for (Creature *c : creatures) {
61 void Body::SetSimulation(Simulation &s) noexcept {
63 for (auto child : children) {
64 child->SetSimulation(s);
68 void Body::SetParent(Body &p) {
73 parent->AddChild(*this);
76 void Body::UnsetParent() {
77 if (!HasParent()) return;
78 parent->RemoveChild(*this);
82 void Body::AddChild(Body &c) {
83 children.push_back(&c);
84 c.SetSimulation(*sim);
87 void Body::RemoveChild(Body &c) {
88 auto entry = std::find(children.begin(), children.end(), &c);
89 if (entry != children.end()) {
90 children.erase(entry);
94 double Body::Inertia() const noexcept {
95 // assume solid sphere for now
96 return (2.0/5.0) * Mass() * pow(Radius(), 2);
99 double Body::GravitationalParameter() const noexcept {
103 double Body::OrbitalPeriod() const noexcept {
105 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
111 double Body::RotationalPeriod() const noexcept {
112 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
113 return std::numeric_limits<double>::infinity();
115 return PI_2p0 * Inertia() / angular;
119 glm::dmat4 Body::ToUniverse() const noexcept {
121 const Body *b = this;
122 while (b->HasParent()) {
123 m = b->ToParent() * m;
129 glm::dmat4 Body::FromUniverse() const noexcept {
131 const Body *b = this;
132 while (b->HasParent()) {
133 m *= b->FromParent();
139 void Body::Cache() noexcept {
142 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
143 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
145 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
146 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
148 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
149 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
152 glm::eulerAngleY(rotation)
153 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
155 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
156 * glm::eulerAngleY(-rotation);
159 void Body::AddCreature(Creature *c) {
161 creatures.push_back(c);
177 double Orbit::SemiMajorAxis() const noexcept {
181 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
186 double Orbit::Eccentricity() const noexcept {
190 Orbit &Orbit::Eccentricity(double e) noexcept {
195 double Orbit::Inclination() const noexcept {
199 Orbit &Orbit::Inclination(double i) noexcept {
204 double Orbit::LongitudeAscending() const noexcept {
208 Orbit &Orbit::LongitudeAscending(double l) noexcept {
213 double Orbit::ArgumentPeriapsis() const noexcept {
217 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
222 double Orbit::MeanAnomaly() const noexcept {
226 Orbit &Orbit::MeanAnomaly(double m) noexcept {
233 double mean2eccentric(double M, double e) {
234 double E = M; // eccentric anomaly, solve M = E - e sin E
235 // limit to 100 steps to prevent deadlocks in impossible situations
236 for (int i = 0; i < 100; ++i) {
237 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
239 if (abs(dE) < 1.0e-6) break;
246 glm::dmat4 Orbit::Matrix(double t) const noexcept {
248 double E = mean2eccentric(M, ecc);
250 // coordinates in orbital plane, P=x, Q=-z
251 double P = sma * (cos(E) - ecc);
252 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
254 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
257 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
259 double E = mean2eccentric(M, ecc);
260 double P = sma * (cos(E) - ecc);
261 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
262 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
266 Planet::Planet(int sidelength)
268 , sidelength(sidelength)
269 , tiles(TilesTotal())
271 Radius(double(sidelength) / 2.0);
277 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
278 glm::dvec3 center(0.0f);
279 center[(surface + 0) % 3] = x + 0.5 - Radius();
280 center[(surface + 1) % 3] = y + 0.5 - Radius();
281 center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
285 void Planet::BuildVAO(const TileSet &ts) {
287 vao.BindAttributes();
288 vao.EnableAttribute(0);
289 vao.EnableAttribute(1);
290 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
291 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
292 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
294 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
295 float offset = Radius();
298 // up +Z +X +Y -Z -X -Y
300 for (int index = 0, surface = 0; surface < 6; ++surface) {
301 for (int y = 0; y < sidelength; ++y) {
302 for (int x = 0; x < sidelength; ++x, ++index) {
303 float tex = ts[TileAt(surface, x, y).type].texture;
304 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
305 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
306 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
307 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
308 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
309 attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
310 attrib[4 * index + 0].tex_coord[1] = 1.0f;
311 attrib[4 * index + 0].tex_coord[2] = tex;
313 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
314 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
315 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
316 attrib[4 * index + 1].tex_coord[0] = tex_u_end;
317 attrib[4 * index + 1].tex_coord[1] = 1.0f;
318 attrib[4 * index + 1].tex_coord[2] = tex;
320 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
321 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
322 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
323 attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
324 attrib[4 * index + 2].tex_coord[1] = 0.0f;
325 attrib[4 * index + 2].tex_coord[2] = tex;
327 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
328 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
329 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
330 attrib[4 * index + 3].tex_coord[0] = tex_u_end;
331 attrib[4 * index + 3].tex_coord[1] = 0.0f;
332 attrib[4 * index + 3].tex_coord[2] = tex;
338 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
340 auto element = vao.MapElements(GL_WRITE_ONLY);
342 for (int surface = 0; surface < 3; ++surface) {
343 for (int y = 0; y < sidelength; ++y) {
344 for (int x = 0; x < sidelength; ++x, ++index) {
345 element[6 * index + 0] = 4 * index + 0;
346 element[6 * index + 1] = 4 * index + 2;
347 element[6 * index + 2] = 4 * index + 1;
348 element[6 * index + 3] = 4 * index + 1;
349 element[6 * index + 4] = 4 * index + 2;
350 element[6 * index + 5] = 4 * index + 3;
354 for (int surface = 3; surface < 6; ++surface) {
355 for (int y = 0; y < sidelength; ++y) {
356 for (int x = 0; x < sidelength; ++x, ++index) {
357 element[6 * index + 0] = 4 * index + 0;
358 element[6 * index + 1] = 4 * index + 1;
359 element[6 * index + 2] = 4 * index + 2;
360 element[6 * index + 3] = 4 * index + 2;
361 element[6 * index + 4] = 4 * index + 1;
362 element[6 * index + 5] = 4 * index + 3;
370 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
372 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
373 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
374 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
375 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
376 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
377 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
378 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
379 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
380 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
381 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
382 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
383 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
384 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
388 void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
389 rand::SimplexNoise elevation_gen(0);
391 const int ice = tiles["ice"].id;
392 const int ocean = tiles["ocean"].id;
393 const int water = tiles["water"].id;
394 const int sand = tiles["sand"].id;
395 const int grass = tiles["grass"].id;
396 const int tundra = tiles["tundra"].id;
397 const int desert = tiles["desert"].id;
398 const int mntn = tiles["mountain"].id;
400 constexpr double ocean_thresh = -0.2;
401 constexpr double water_thresh = 0.0;
402 constexpr double beach_thresh = 0.1;
403 constexpr double mountain_thresh = 0.5;
405 const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
406 const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
407 const double equ_thresh = 2.0 * (1.0 - cap_thresh);
408 const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
409 const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
411 for (int surface = 0; surface <= 5; ++surface) {
412 for (int y = 0; y < p.SideLength(); ++y) {
413 for (int x = 0; x < p.SideLength(); ++x) {
414 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
415 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
416 if (near_axis > cap_thresh) {
417 p.TileAt(surface, x, y).type = ice;
420 float elevation = rand::OctaveNoise(
422 to_tile / p.Radius(),
425 2 / p.Radius(), // frequency
429 if (elevation < ocean_thresh) {
430 p.TileAt(surface, x, y).type = ocean;
431 } else if (elevation < water_thresh) {
432 p.TileAt(surface, x, y).type = water;
433 } else if (elevation < beach_thresh) {
434 p.TileAt(surface, x, y).type = sand;
435 } else if (elevation < mountain_thresh) {
436 // TODO: perturb climate rings a little
437 if (near_axis < equ_thresh) {
438 p.TileAt(surface, x, y).type = desert;
439 } else if (near_axis < fzone_start) {
440 p.TileAt(surface, x, y).type = grass;
441 } else if (near_axis < fzone_end) {
442 p.TileAt(surface, x, y).type = tundra;
444 p.TileAt(surface, x, y).type = grass;
447 p.TileAt(surface, x, y).type = mntn;
455 void GenerateTest(const TileSet &tiles, Planet &p) noexcept {
456 for (int surface = 0; surface <= 5; ++surface) {
457 for (int y = 0; y < p.SideLength(); ++y) {
458 for (int x = 0; x < p.SideLength(); ++x) {
459 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
460 p.TileAt(surface, x, y).type = surface;
462 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
483 TileSet::~TileSet() {
486 int TileSet::Add(const TileType &t) {
487 int id = types.size();
488 if (!names.emplace(t.name, id).second) {
489 throw std::runtime_error("duplicate tile type name " + t.name);
491 types.emplace_back(t);
492 types.back().id = id;
496 TileType &TileSet::operator [](const std::string &name) {
497 auto entry = names.find(name);
498 if (entry != names.end()) {
499 return types[entry->second];
501 throw std::runtime_error("unknown tile type " + name);
505 const TileType &TileSet::operator [](const std::string &name) const {
506 auto entry = names.find(name);
507 if (entry != names.end()) {
508 return types[entry->second];
510 throw std::runtime_error("unknown tile type " + name);