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)
46 void Body::SetSimulation(Simulation &s) noexcept {
48 for (auto child : children) {
49 child->SetSimulation(s);
53 void Body::SetParent(Body &p) {
58 parent->AddChild(*this);
61 void Body::UnsetParent() {
62 if (!HasParent()) return;
63 parent->RemoveChild(*this);
67 void Body::AddChild(Body &c) {
68 children.push_back(&c);
69 c.SetSimulation(*sim);
72 void Body::RemoveChild(Body &c) {
73 auto entry = std::find(children.begin(), children.end(), &c);
74 if (entry != children.end()) {
75 children.erase(entry);
79 double Body::Inertia() const noexcept {
80 // assume solid sphere for now
81 return (2.0/5.0) * Mass() * pow(Radius(), 2);
84 double Body::GravitationalParameter() const noexcept {
88 double Body::OrbitalPeriod() const noexcept {
90 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
96 double Body::RotationalPeriod() const noexcept {
97 if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
98 return std::numeric_limits<double>::infinity();
100 return PI_2p0 * Inertia() / angular;
104 glm::dmat4 Body::LocalTransform() const noexcept {
105 glm::dmat4 srf = glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
106 glm::dmat4 rot = glm::eulerAngleY(rotation);
107 glm::dmat4 tilt = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
108 return tilt * rot * srf;
111 glm::dmat4 Body::InverseTransform() const noexcept {
112 glm::dmat4 srf = glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x);
113 glm::dmat4 rot = glm::eulerAngleY(-rotation);
114 glm::dmat4 tilt = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
115 return srf * rot * tilt;
118 glm::dmat4 Body::ToParent() const noexcept {
120 return glm::dmat4(1.0);
122 return orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
125 glm::dmat4 Body::FromParent() const noexcept {
127 return glm::dmat4(1.0);
129 return orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
145 double Orbit::SemiMajorAxis() const noexcept {
149 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
154 double Orbit::Eccentricity() const noexcept {
158 Orbit &Orbit::Eccentricity(double e) noexcept {
163 double Orbit::Inclination() const noexcept {
167 Orbit &Orbit::Inclination(double i) noexcept {
172 double Orbit::LongitudeAscending() const noexcept {
176 Orbit &Orbit::LongitudeAscending(double l) noexcept {
181 double Orbit::ArgumentPeriapsis() const noexcept {
185 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
190 double Orbit::MeanAnomaly() const noexcept {
194 Orbit &Orbit::MeanAnomaly(double m) noexcept {
201 double mean2eccentric(double M, double e) {
202 double E = M; // eccentric anomaly, solve M = E - e sin E
203 // limit to 100 steps to prevent deadlocks in impossible situations
204 for (int i = 0; i < 100; ++i) {
205 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
207 if (abs(dE) < 1.0e-6) break;
214 glm::dmat4 Orbit::Matrix(double t) const noexcept {
216 double E = mean2eccentric(M, ecc);
218 // coordinates in orbital plane, P=x, Q=-z
219 double P = sma * (cos(E) - ecc);
220 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
222 return glm::translate(glm::yawPitchRoll(asc, inc, arg), glm::dvec3(P, 0.0, -Q));
225 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
227 double E = mean2eccentric(M, ecc);
228 double P = sma * (cos(E) - ecc);
229 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
230 return glm::transpose(glm::yawPitchRoll(asc, inc, arg)) * glm::translate(glm::dvec3(-P, 0.0, Q));
234 Planet::Planet(int sidelength)
236 , sidelength(sidelength)
237 , tiles(new Tile[TilesTotal()])
239 Radius(double(sidelength) / 2.0);
245 void Planet::BuildVAOs() {
247 vao.BindAttributes();
248 vao.EnableAttribute(0);
249 vao.EnableAttribute(1);
250 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
251 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
252 vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
254 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
255 float offset = sidelength * 0.5f;
257 for (int index = 0, surface = 0; surface < 6; ++surface) {
258 for (int y = 0; y < sidelength; ++y) {
259 for (int x = 0; x < sidelength; ++x, ++index) {
260 float tex = TileAt(surface, x, y).type;
261 attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
262 attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
263 attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
264 attrib[4 * index + 0].tex_coord[0] = 0.0f;
265 attrib[4 * index + 0].tex_coord[1] = 0.0f;
266 attrib[4 * index + 0].tex_coord[2] = tex;
268 attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
269 attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
270 attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
271 attrib[4 * index + 1].tex_coord[0] = 0.0f;
272 attrib[4 * index + 1].tex_coord[1] = 1.0f;
273 attrib[4 * index + 1].tex_coord[2] = tex;
275 attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
276 attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
277 attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
278 attrib[4 * index + 2].tex_coord[0] = 1.0f;
279 attrib[4 * index + 2].tex_coord[1] = 0.0f;
280 attrib[4 * index + 2].tex_coord[2] = tex;
282 attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
283 attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
284 attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
285 attrib[4 * index + 3].tex_coord[0] = 1.0f;
286 attrib[4 * index + 3].tex_coord[1] = 1.0f;
287 attrib[4 * index + 3].tex_coord[2] = tex;
293 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
295 auto element = vao.MapElements(GL_WRITE_ONLY);
297 for (int surface = 0; surface < 3; ++surface) {
298 for (int y = 0; y < sidelength; ++y) {
299 for (int x = 0; x < sidelength; ++x, ++index) {
300 element[6 * index + 0] = 4 * index + 0;
301 element[6 * index + 1] = 4 * index + 2;
302 element[6 * index + 2] = 4 * index + 1;
303 element[6 * index + 3] = 4 * index + 1;
304 element[6 * index + 4] = 4 * index + 2;
305 element[6 * index + 5] = 4 * index + 3;
309 for (int surface = 3; surface < 6; ++surface) {
310 for (int y = 0; y < sidelength; ++y) {
311 for (int x = 0; x < sidelength; ++x, ++index) {
312 element[6 * index + 0] = 4 * index + 0;
313 element[6 * index + 1] = 4 * index + 1;
314 element[6 * index + 2] = 4 * index + 2;
315 element[6 * index + 3] = 4 * index + 2;
316 element[6 * index + 4] = 4 * index + 1;
317 element[6 * index + 5] = 4 * index + 3;
325 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
327 const glm::mat4 &MV = assets.shaders.planet_surface.MV();
328 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
329 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
330 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
331 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
332 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
333 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
334 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
335 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
336 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
337 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
338 assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
339 vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
343 void GenerateTest(Planet &p) {
344 for (int surface = 0; surface <= 5; ++surface) {
345 for (int y = 0; y < p.SideLength(); ++y) {
346 for (int x = 0; x < p.SideLength(); ++x) {
347 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2);