]> git.localhorst.tv Git - blobs.git/commitdiff
half-assed implementation of "other" body rendering
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Nov 2017 20:39:15 +0000 (21:39 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Nov 2017 20:52:47 +0000 (21:52 +0100)
src/blobs.cpp
src/graphics/viewport.cpp
src/world/Body.hpp
src/world/sim.cpp
src/world/world.cpp

index 464273bac4f894c387b68dd11e24bf4febf9a578..f96dd0f223512380070083104e622f6cec2677b1 100644 (file)
@@ -21,7 +21,7 @@ int main(int argc, char *argv[]) {
        sun.Mass(1.0e12);
        sun.Radius(10.0);
        sun.SurfaceTilt(glm::dvec2(PI * 0.25, PI * 0.25));
-       sun.AngularMomentum(1.0e9);
+       sun.AngularMomentum(1.0e13);
 
        world::Planet planet(11);
        world::GenerateTest(planet);
@@ -40,9 +40,19 @@ int main(int argc, char *argv[]) {
        moon.Rotation(PI * 0.25);
        moon.AngularMomentum(1.0e4);
 
+       world::Planet second_planet(9);
+       world::GenerateTest(second_planet);
+       second_planet.SetParent(sun);
+       second_planet.Mass(1.0e9);
+       second_planet.GetOrbit().SemiMajorAxis(350.0);
+       second_planet.SurfaceTilt(glm::dvec2(PI * 0.125, PI * 0.25));
+       second_planet.AxialTilt(glm::dvec2(PI * 0.95, 0.0));
+       second_planet.AngularMomentum(1.0e8);
+
        world::Simulation sim(sun);
        sim.AddSun(sun);
        sim.AddPlanet(planet);
+       sim.AddPlanet(second_planet);
        sim.AddPlanet(moon);
 
        std::cout << "length of year: " << planet.OrbitalPeriod() << "s" << std::endl;
@@ -61,9 +71,12 @@ int main(int argc, char *argv[]) {
                //.FirstPerson(3, glm::vec3(0.0f, 0.0f, 0.1f), glm::vec3(1.0f, -0.75f, 0.1f))
                // from afar
                //.MapView(0, glm::vec3(0.0f, 0.0f, 25.0f), 0.0f)
-               // system view
-               //.Orbital(glm::vec3(50.0f, 2500.0f, 50.0f));
        ;
+       // system view
+       //state.GetCamera()
+       //      .Reference(sun)
+       //      .Orbital(glm::vec3(-500.0f, 500.0f, 500.0f))
+       //;
        planet.BuildVAOs();
 
        app::Application app(init.window, init.viewport);
index 2d9b469236a07d8f2ce84da43d014530836c09e7..9e22a260eeb12072eb75ea5931be9b666009d0c1 100644 (file)
@@ -120,8 +120,9 @@ glm::mat4 Camera::Model(const world::Body &b) const noexcept {
                        ? ref->InverseTransform() * ref->ToParent() * b.LocalTransform()
                        : ref->ToParent() * b.LocalTransform();
        } else {
-               // TODO: model matrices for path distances > 1
-               return track_orient ? glm::mat4(1.0f) : glm::mat4(ref->LocalTransform());
+               return track_orient
+                       ? ref->InverseTransform() * ref->ToUniverse() * b.FromUniverse() * b.LocalTransform()
+                       : ref->ToUniverse() * b.FromUniverse() * b.LocalTransform();
        }
 }
 
index 1b9e2e3a2a65036b75f370aa47a58b3730374545..5f82be232b4dc3e78029fd27338b122ab4a41421 100644 (file)
@@ -70,14 +70,19 @@ public:
        double OrbitalPeriod() const noexcept;
        double RotationalPeriod() const noexcept;
 
-       glm::dmat4 LocalTransform() const noexcept;
-       glm::dmat4 InverseTransform() const noexcept;
+       const glm::dmat4 &LocalTransform() const noexcept { return local; }
+       const glm::dmat4 &InverseTransform() const noexcept { return inverse_local; }
 
-       glm::dmat4 ToParent() const noexcept;
-       glm::dmat4 FromParent() const noexcept;
+       const glm::dmat4 &ToParent() const noexcept { return inverse_orbital; }
+       const glm::dmat4 &FromParent() const noexcept { return orbital; }
+
+       glm::dmat4 ToUniverse() const noexcept;
+       glm::dmat4 FromUniverse() const noexcept;
 
        virtual void Draw(app::Assets &, graphics::Viewport &) { }
 
+       void Cache() noexcept;
+
 private:
        void AddChild(Body &);
        void RemoveChild(Body &);
@@ -94,6 +99,11 @@ private:
        double rotation;
        double angular;
 
+       glm::dmat4 orbital;
+       glm::dmat4 inverse_orbital;
+       glm::dmat4 local;
+       glm::dmat4 inverse_local;
+
 };
 
 }
index 185046d4cfc4eed8e73270513c7568e3a55a9561..8b4c2140c2ba96b672c9dd08ac7987223b52c9f9 100644 (file)
@@ -41,6 +41,7 @@ void Simulation::Tick() {
        time += dt;
        for (auto body : bodies) {
                body->Rotation(body->Rotation() + dt * body->AngularMomentum() / body->Inertia());
+               body->Cache();
        }
 }
 
index eb03490204c722cf3f6f58425a2d88bc3ce2b022..c9a7a326f3d94a2f4bc496e4100da541df82e794 100644 (file)
@@ -37,7 +37,11 @@ Body::Body()
 , surface_tilt(0.0, 0.0)
 , axis_tilt(0.0, 0.0)
 , rotation(0.0)
-, angular(0.0) {
+, angular(0.0)
+, orbital(1.0)
+, inverse_orbital(1.0)
+, local(1.0)
+, inverse_local(1.0) {
 }
 
 Body::~Body() {
@@ -101,32 +105,44 @@ double Body::RotationalPeriod() const noexcept {
        }
 }
 
-glm::dmat4 Body::LocalTransform() const noexcept {
-       glm::dmat4 srf = glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
-       glm::dmat4 rot = glm::eulerAngleY(rotation);
-       glm::dmat4 tilt = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
-       return tilt * rot * srf;
-}
-
-glm::dmat4 Body::InverseTransform() const noexcept {
-       glm::dmat4 srf = glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x);
-       glm::dmat4 rot = glm::eulerAngleY(-rotation);
-       glm::dmat4 tilt = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
-       return srf * rot * tilt;
+glm::dmat4 Body::ToUniverse() const noexcept {
+       glm::dmat4 m(1.0);
+       const Body *b = this;
+       while (b->HasParent()) {
+               m = b->ToParent() * m;
+               b = &b->Parent();
+       }
+       return m;
 }
 
-glm::dmat4 Body::ToParent() const noexcept {
-       if (!parent) {
-               return glm::dmat4(1.0);
+glm::dmat4 Body::FromUniverse() const noexcept {
+       glm::dmat4 m(1.0);
+       const Body *b = this;
+       while (b->HasParent()) {
+               m *= b->FromParent();
+               b = &b->Parent();
        }
-       return orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
+       return m;
 }
 
-glm::dmat4 Body::FromParent() const noexcept {
-       if (!parent) {
-               return glm::dmat4(1.0);
+void Body::Cache() noexcept {
+       if (parent) {
+               orbital =
+                       orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
+                       * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
+               inverse_orbital =
+                       glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
+                       * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
+       } else {
+               orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
+               inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
        }
-       return orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
+       local =
+               glm::eulerAngleY(rotation)
+               * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
+       inverse_local =
+               glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
+               * glm::eulerAngleY(-rotation);
 }