]> git.localhorst.tv Git - blobs.git/blob - src/world/world.cpp
fun with resources
[blobs.git] / src / world / world.cpp
1 #include "Body.hpp"
2 #include "Orbit.hpp"
3 #include "Planet.hpp"
4 #include "Resource.hpp"
5 #include "Set.hpp"
6 #include "Simulation.hpp"
7 #include "Sun.hpp"
8 #include "Tile.hpp"
9 #include "TileType.hpp"
10
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"
17
18 #include <algorithm>
19 #include <cmath>
20 #include <iostream>
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>
25
26 using blobs::G;
27 using blobs::PI_2p0;
28
29 using std::sin;
30 using std::cos;
31 using std::pow;
32 using std::sqrt;
33
34
35 namespace blobs {
36 namespace world {
37
38 Body::Body()
39 : sim(nullptr)
40 , parent(nullptr)
41 , children()
42 , mass(1.0)
43 , radius(1.0)
44 , orbit()
45 , surface_tilt(0.0, 0.0)
46 , axis_tilt(0.0, 0.0)
47 , rotation(0.0)
48 , angular(0.0)
49 , orbital(1.0)
50 , inverse_orbital(1.0)
51 , local(1.0)
52 , inverse_local(1.0)
53 , creatures() {
54 }
55
56 Body::~Body() {
57         for (Creature *c : creatures) {
58                 delete c;
59         }
60 }
61
62 void Body::SetSimulation(Simulation &s) noexcept {
63         sim = &s;
64         for (auto child : children) {
65                 child->SetSimulation(s);
66         }
67 }
68
69 void Body::SetParent(Body &p) {
70         if (HasParent()) {
71                 UnsetParent();
72         }
73         parent = &p;
74         parent->AddChild(*this);
75 }
76
77 void Body::UnsetParent() {
78         if (!HasParent()) return;
79         parent->RemoveChild(*this);
80         parent = nullptr;
81 }
82
83 void Body::AddChild(Body &c) {
84         children.push_back(&c);
85         c.SetSimulation(*sim);
86 }
87
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);
92         }
93 }
94
95 double Body::Inertia() const noexcept {
96         // assume solid sphere for now
97         return (2.0/5.0) * Mass() * pow(Radius(), 2);
98 }
99
100 double Body::GravitationalParameter() const noexcept {
101         return G * Mass();
102 }
103
104 double Body::OrbitalPeriod() const noexcept {
105         if (parent) {
106                 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
107         } else {
108                 return 0.0;
109         }
110 }
111
112 double Body::RotationalPeriod() const noexcept {
113         if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
114                 return std::numeric_limits<double>::infinity();
115         } else {
116                 return PI_2p0 * Inertia() / angular;
117         }
118 }
119
120 glm::dmat4 Body::ToUniverse() const noexcept {
121         glm::dmat4 m(1.0);
122         const Body *b = this;
123         while (b->HasParent()) {
124                 m = b->ToParent() * m;
125                 b = &b->Parent();
126         }
127         return m;
128 }
129
130 glm::dmat4 Body::FromUniverse() const noexcept {
131         glm::dmat4 m(1.0);
132         const Body *b = this;
133         while (b->HasParent()) {
134                 m *= b->FromParent();
135                 b = &b->Parent();
136         }
137         return m;
138 }
139
140 void Body::Cache() noexcept {
141         if (parent) {
142                 orbital =
143                         orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
144                         * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
145                 inverse_orbital =
146                         glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
147                         * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
148         } else {
149                 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
150                 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
151         }
152         local =
153                 glm::eulerAngleY(rotation)
154                 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
155         inverse_local =
156                 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
157                 * glm::eulerAngleY(-rotation);
158 }
159
160 void Body::AddCreature(Creature *c) {
161         c->SetBody(*this);
162         creatures.push_back(c);
163 }
164
165
166 Orbit::Orbit()
167 : sma(1.0)
168 , ecc(0.0)
169 , inc(0.0)
170 , asc(0.0)
171 , arg(0.0)
172 , mna(0.0) {
173 }
174
175 Orbit::~Orbit() {
176 }
177
178 double Orbit::SemiMajorAxis() const noexcept {
179         return sma;
180 }
181
182 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
183         sma = s;
184         return *this;
185 }
186
187 double Orbit::Eccentricity() const noexcept {
188         return ecc;
189 }
190
191 Orbit &Orbit::Eccentricity(double e) noexcept {
192         ecc = e;
193         return *this;
194 }
195
196 double Orbit::Inclination() const noexcept {
197         return inc;
198 }
199
200 Orbit &Orbit::Inclination(double i) noexcept {
201         inc = i;
202         return *this;
203 }
204
205 double Orbit::LongitudeAscending() const noexcept {
206         return asc;
207 }
208
209 Orbit &Orbit::LongitudeAscending(double l) noexcept {
210         asc = l;
211         return *this;
212 }
213
214 double Orbit::ArgumentPeriapsis() const noexcept {
215         return arg;
216 }
217
218 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
219         arg = a;
220         return *this;
221 }
222
223 double Orbit::MeanAnomaly() const noexcept {
224         return mna;
225 }
226
227 Orbit &Orbit::MeanAnomaly(double m) noexcept {
228         mna = m;
229         return *this;
230 }
231
232 namespace {
233
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));
239                 E -= dE;
240                 if (abs(dE) < 1.0e-6) break;
241         }
242         return E;
243 }
244
245 }
246
247 glm::dmat4 Orbit::Matrix(double t) const noexcept {
248         double M = mna + t;
249         double E = mean2eccentric(M, ecc);
250
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));
254
255         return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
256 }
257
258 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
259         double M = mna + t;
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));
264 }
265
266
267 Planet::Planet(int sidelength)
268 : Body()
269 , sidelength(sidelength)
270 , tiles(TilesTotal())
271 , vao() {
272         Radius(double(sidelength) / 2.0);
273 }
274
275 Planet::~Planet() {
276 }
277
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();
283         return center;
284 }
285
286 void Planet::BuildVAO(const Set<TileType> &ts) {
287         vao.Bind();
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);
294         {
295                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
296                 float offset = Radius();
297
298                 // srf  0  1  2  3  4  5
299                 //  up +Z +X +Y -Z -X -Y
300
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;
313
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;
320
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;
327
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;
334                                 }
335                         }
336                 }
337         }
338         vao.BindElements();
339         vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
340         {
341                 auto element = vao.MapElements(GL_WRITE_ONLY);
342                 int index = 0;
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;
352                                 }
353                         }
354                 }
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;
364                                 }
365                         }
366                 }
367         }
368         vao.Unbind();
369 }
370
371 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
372         vao.Bind();
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);
386 }
387
388
389 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
390         rand::SimplexNoise elevation_gen(0);
391         rand::SimplexNoise variation_gen(45623752346);
392
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;
407
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;
413
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;
419
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;
427                                         continue;
428                                 }
429                                 float elevation = rand::OctaveNoise(
430                                         elevation_gen,
431                                         to_tile / p.Radius(),
432                                         3,   // octaves
433                                         0.5, // persistence
434                                         5 / p.Radius(), // frequency
435                                         2,   // amplitude
436                                         2    // growth
437                                 );
438                                 float variation = rand::OctaveNoise(
439                                         variation_gen,
440                                         to_tile / p.Radius(),
441                                         3,   // octaves
442                                         0.5, // persistence
443                                         16 / p.Radius(), // frequency
444                                         2,   // amplitude
445                                         2    // growth
446                                 );
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;
452                                         } else {
453                                                 p.TileAt(surface, x, y).type = water;
454                                         }
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;
463                                                 } else {
464                                                         p.TileAt(surface, x, y).type = desert;
465                                                 }
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;
473                                                 } else {
474                                                         p.TileAt(surface, x, y).type = grass;
475                                                 }
476                                         } else if (near_axis < fzone_end) {
477                                                 p.TileAt(surface, x, y).type = tundra;
478                                         } else {
479                                                 p.TileAt(surface, x, y).type = taiga;
480                                         }
481                                 } else if (elevation < mountain_thresh) {
482                                         if (variation > 0.3) {
483                                                 p.TileAt(surface, x, y).type = mntn;
484                                         } else {
485                                                 p.TileAt(surface, x, y).type = rock;
486                                         }
487                                 } else {
488                                         p.TileAt(surface, x, y).type = mntn;
489                                 }
490                         }
491                 }
492         }
493         p.BuildVAO(tiles);
494 }
495
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;
502                                 } else {
503                                         p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
504                                 }
505                         }
506                 }
507         }
508         p.BuildVAO(tiles);
509 }
510
511
512 Sun::Sun()
513 : Body() {
514 }
515
516 Sun::~Sun() {
517 }
518
519 }
520 }