4 #include "Simulation.hpp"
8 #include "../const.hpp"
9 #include "../app/Assets.hpp"
10 #include "../graphics/Viewport.hpp"
14 #include <glm/gtc/matrix_transform.hpp>
15 #include <glm/gtx/euler_angles.hpp>
16 #include <glm/gtx/transform.hpp>
37 , surface_tilt(0.0, 0.0)
42 , inverse_orbital(1.0)
44 , inverse_local(1.0) {
50 void Body::SetSimulation(Simulation &s) noexcept {
52 for (auto child : children) {
53 child->SetSimulation(s);
57 void Body::SetParent(Body &p) {
62 parent->AddChild(*this);
65 void Body::UnsetParent() {
66 if (!HasParent()) return;
67 parent->RemoveChild(*this);
71 void Body::AddChild(Body &c) {
72 children.push_back(&c);
73 c.SetSimulation(*sim);
76 void Body::RemoveChild(Body &c) {
77 auto entry = std::find(children.begin(), children.end(), &c);
78 if (entry != children.end()) {
79 children.erase(entry);
83 double Body::Inertia() const noexcept {
84 // assume solid sphere for now
85 return (2.0/5.0) * Mass() * pow(Radius(), 2);
88 double Body::GravitationalParameter() const noexcept {
92 double Body::OrbitalPeriod() const noexcept {
94 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
100 double Body::RotationalPeriod() const noexcept {
101 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
102 return std::numeric_limits<double>::infinity();
104 return PI_2p0 * Inertia() / angular;
108 glm::dmat4 Body::ToUniverse() const noexcept {
110 const Body *b = this;
111 while (b->HasParent()) {
112 m = b->ToParent() * m;
118 glm::dmat4 Body::FromUniverse() const noexcept {
120 const Body *b = this;
121 while (b->HasParent()) {
122 m *= b->FromParent();
128 void Body::Cache() noexcept {
131 orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
132 * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
134 glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
135 * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
137 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
138 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
141 glm::eulerAngleY(rotation)
142 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
144 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
145 * glm::eulerAngleY(-rotation);
161 double Orbit::SemiMajorAxis() const noexcept {
165 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
170 double Orbit::Eccentricity() const noexcept {
174 Orbit &Orbit::Eccentricity(double e) noexcept {
179 double Orbit::Inclination() const noexcept {
183 Orbit &Orbit::Inclination(double i) noexcept {
188 double Orbit::LongitudeAscending() const noexcept {
192 Orbit &Orbit::LongitudeAscending(double l) noexcept {
197 double Orbit::ArgumentPeriapsis() const noexcept {
201 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
206 double Orbit::MeanAnomaly() const noexcept {
210 Orbit &Orbit::MeanAnomaly(double m) noexcept {
217 double mean2eccentric(double M, double e) {
218 double E = M; // eccentric anomaly, solve M = E - e sin E
219 // limit to 100 steps to prevent deadlocks in impossible situations
220 for (int i = 0; i < 100; ++i) {
221 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
223 if (abs(dE) < 1.0e-6) break;
230 glm::dmat4 Orbit::Matrix(double t) const noexcept {
232 double E = mean2eccentric(M, ecc);
234 // coordinates in orbital plane, P=x, Q=-z
235 double P = sma * (cos(E) - ecc);
236 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
238 return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
241 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
243 double E = mean2eccentric(M, ecc);
244 double P = sma * (cos(E) - ecc);
245 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
246 return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
250 Planet::Planet(int sidelength)
252 , sidelength(sidelength)
253 , tiles(new Tile[TilesTotal()])
255 Radius(double(sidelength) / 2.0);
261 void Planet::BuildVAOs() {
263 vao.BindAttributes();
264 vao.EnableAttribute(0);
265 vao.EnableAttribute(1);
266 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
267 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
268 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
270 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
271 float offset = sidelength * 0.5f;
274 // up +Z +X +Y -Z -X -Y
276 for (int index = 0, surface = 0; surface < 6; ++surface) {
277 for (int y = 0; y < sidelength; ++y) {
278 for (int x = 0; x < sidelength; ++x, ++index) {
279 float tex = TileAt(surface, x, y).type;
280 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
281 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
282 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
283 attrib[4 * index + 0].tex_coord[0] = 0.0f;
284 attrib[4 * index + 0].tex_coord[1] = 0.0f;
285 attrib[4 * index + 0].tex_coord[2] = tex;
287 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
288 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
289 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
290 attrib[4 * index + 1].tex_coord[0] = 0.0f;
291 attrib[4 * index + 1].tex_coord[1] = 1.0f;
292 attrib[4 * index + 1].tex_coord[2] = tex;
294 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
295 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
296 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
297 attrib[4 * index + 2].tex_coord[0] = 1.0f;
298 attrib[4 * index + 2].tex_coord[1] = 0.0f;
299 attrib[4 * index + 2].tex_coord[2] = tex;
301 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
302 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
303 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
304 attrib[4 * index + 3].tex_coord[0] = 1.0f;
305 attrib[4 * index + 3].tex_coord[1] = 1.0f;
306 attrib[4 * index + 3].tex_coord[2] = tex;
312 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
314 auto element = vao.MapElements(GL_WRITE_ONLY);
316 for (int surface = 0; surface < 3; ++surface) {
317 for (int y = 0; y < sidelength; ++y) {
318 for (int x = 0; x < sidelength; ++x, ++index) {
319 element[6 * index + 0] = 4 * index + 0;
320 element[6 * index + 1] = 4 * index + 2;
321 element[6 * index + 2] = 4 * index + 1;
322 element[6 * index + 3] = 4 * index + 1;
323 element[6 * index + 4] = 4 * index + 2;
324 element[6 * index + 5] = 4 * index + 3;
328 for (int surface = 3; surface < 6; ++surface) {
329 for (int y = 0; y < sidelength; ++y) {
330 for (int x = 0; x < sidelength; ++x, ++index) {
331 element[6 * index + 0] = 4 * index + 0;
332 element[6 * index + 1] = 4 * index + 1;
333 element[6 * index + 2] = 4 * index + 2;
334 element[6 * index + 3] = 4 * index + 2;
335 element[6 * index + 4] = 4 * index + 1;
336 element[6 * index + 5] = 4 * index + 3;
344 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
346 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
347 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
348 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
349 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
350 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
351 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
352 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
353 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
354 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
355 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
356 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
357 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
358 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
362 void GenerateTest(Planet &p) {
363 for (int surface = 0; surface <= 5; ++surface) {
364 for (int y = 0; y < p.SideLength(); ++y) {
365 for (int x = 0; x < p.SideLength(); ++x) {
366 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2);