]> git.localhorst.tv Git - blobs.git/commitdiff
track creature with camera
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 21:47:33 +0000 (22:47 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 21:47:33 +0000 (22:47 +0100)
src/app/states.cpp
src/blobs.cpp
src/graphics/Camera.hpp
src/graphics/CreatureSkin.hpp
src/graphics/PlanetSurface.hpp
src/graphics/SunSurface.hpp
src/graphics/shader.cpp
src/graphics/viewport.cpp
src/ui/CreaturePanel.hpp

index d98a0991cbc67c4ec2dbb9c0c68030d2cfc1bb87..d1e092e70a8589dc70387d9d72793b7868e461ef 100644 (file)
@@ -70,6 +70,16 @@ void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
 }
 
 void MasterState::OnRender(graphics::Viewport &viewport) {
+       if (cp.Shown()) {
+               cam.TopDown(cp.GetCreature(), 10.0f);
+               assets.shaders.planet_surface.Activate();
+               assets.shaders.planet_surface.SetV(cam.View());
+               assets.shaders.sun_surface.Activate();
+               assets.shaders.sun_surface.SetV(cam.View());
+               assets.shaders.creature_skin.Activate();
+               assets.shaders.creature_skin.SetV(cam.View());
+       }
+
        int num_lights = 0;
        for (auto sun : sim.Suns()) {
                // TODO: source sun's light color and strength
index c1dfbbc250491500c7e2b785c4be2b4d100d3d3f..836c51d335845b0f79e74caf891c6bc3f9ae4732 100644 (file)
@@ -109,7 +109,7 @@ int main(int argc, char *argv[]) {
                // sunset
                //.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, 15.0f), 0.0f)
+               .MapView(0, glm::vec3(0.0f, 0.0f, 30.0f), 0.0f)
                // from afar, rotating
                //.Orbital(glm::vec3(-60.0f, 0.0f, 0.0f))
        ;
index 51dd8f15c447e1a67731b816b78f7211f251179d..736020da8b491efa9ea39f9d4c3ecdef9276f8f2 100644 (file)
@@ -5,6 +5,9 @@
 
 
 namespace blobs {
+namespace creature {
+       class Creature;
+}
 namespace world {
        class Body;
 }
@@ -38,6 +41,8 @@ public:
        Camera &MapView(int surface, const glm::vec3 &pos, float roll = 0.0f) noexcept;
        /// look at center, position relative to orbital reference plane for children
        Camera &Orbital(const glm::vec3 &pos) noexcept;
+       /// look at creature from above
+       Camera &TopDown(const creature::Creature &, float distance, float roll = 0.0f);
 
        const glm::mat4 &Projection() const noexcept { return projection; }
        const glm::mat4 &View() const noexcept { return view; }
index 1b5b6b8bc47ae4dd625f0818dff28e2d5539b8e6..c24a746062173b9ec5351da349e3086acede7a2f 100644 (file)
@@ -28,6 +28,7 @@ public:
        void Activate() noexcept;
 
        void SetM(const glm::mat4 &m) noexcept;
+       void SetV(const glm::mat4 &v) noexcept;
        void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetTexture(ArrayTexture &) noexcept;
index a5f177da14e3a81a8b91deda99f2762ca1978040..b915283d2bdc6453fb8db5dc810d6ca8385f0578 100644 (file)
@@ -28,6 +28,7 @@ public:
        void Activate() noexcept;
 
        void SetM(const glm::mat4 &m) noexcept;
+       void SetV(const glm::mat4 &v) noexcept;
        void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetNormal(const glm::vec3 &) noexcept;
index 65727617996fc12212b7aa4a433fd75e0f314070..f10e0e5db6a2da7933660dcca2956a45dd58453a 100644 (file)
@@ -28,6 +28,7 @@ public:
        void Activate() noexcept;
 
        void SetM(const glm::mat4 &m) noexcept;
+       void SetV(const glm::mat4 &v) noexcept;
        void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetLight(const glm::vec3 &color, float strength) noexcept;
index dbdeeb15552efa28699b4b7b50893e73ae9e10d3..6a08bb5f99be5402edda61b239be9e145b58cb40 100644 (file)
@@ -282,6 +282,14 @@ void PlanetSurface::SetM(const glm::mat4 &mm) noexcept {
        prog.Uniform(mvp_handle, mvp);
 }
 
+void PlanetSurface::SetV(const glm::mat4 &vv) noexcept {
+       v = vv;
+       mv = v * m;
+       mvp = p * mv;
+       prog.Uniform(mv_handle, mv);
+       prog.Uniform(mvp_handle, mvp);
+}
+
 void PlanetSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
        v = vv;
        p = pp;
@@ -458,6 +466,14 @@ void SunSurface::SetM(const glm::mat4 &mm) noexcept {
        prog.Uniform(mvp_handle, mvp);
 }
 
+void SunSurface::SetV(const glm::mat4 &vv) noexcept {
+       v = vv;
+       mv = v * m;
+       mvp = p * mv;
+       prog.Uniform(mv_handle, mv);
+       prog.Uniform(mvp_handle, mvp);
+}
+
 void SunSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
        v = vv;
        p = pp;
@@ -592,6 +608,14 @@ void CreatureSkin::SetM(const glm::mat4 &mm) noexcept {
        prog.Uniform(mvp_handle, mvp);
 }
 
+void CreatureSkin::SetV(const glm::mat4 &vv) noexcept {
+       v = vv;
+       mv = v * m;
+       mvp = p * mv;
+       prog.Uniform(mv_handle, mv);
+       prog.Uniform(mvp_handle, mvp);
+}
+
 void CreatureSkin::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
        v = vv;
        p = pp;
index ac0b9e5c5b7c1516ea2795f615f581bc50861c4a..398eb42ef1b786d3d57581a9b9f4e6a8244cf5ee 100644 (file)
@@ -1,8 +1,10 @@
 #include "Camera.hpp"
 #include "Viewport.hpp"
 
+#include "../creature/Creature.hpp"
 #include "../math/const.hpp"
 #include "../world/Body.hpp"
+#include "../world/Planet.hpp"
 
 #include <cmath>
 #include <GL/glew.h>
@@ -84,24 +86,34 @@ Camera &Camera::MapView(int srf, const glm::vec3 &pos, float roll) noexcept {
 
        float dir = srf < 3 ? 1.0f : -1.0f;
 
-       glm::vec3 position;
-       position[(srf + 0) % 3] = pos.x;
-       position[(srf + 1) % 3] = pos.y;
-       position[(srf + 2) % 3] = dir * (pos.z + Reference().Radius());
-
        glm::vec3 up(0.0f);
-       up[(srf + 0) % 3] = std::cos(roll);
-       up[(srf + 1) % 3] = std::sin(roll);
+       up[(srf + 0) % 3] = std::sin(roll);
+       up[(srf + 1) % 3] = std::cos(roll);
        up[(srf + 2) % 3] = 0.0f;
 
-       glm::vec3 target = position;
+       glm::vec3 target = pos;
        target[(srf + 2) % 3] -= dir;
 
-       view = glm::lookAt(position, target, up);
+       view = glm::lookAt(pos, target, up);
 
        return *this;
 }
 
+Camera &Camera::TopDown(const creature::Creature &c, float distance, float roll) {
+       const creature::Situation &s = c.GetSituation();
+       if (s.OnPlanet()) {
+               int srf = s.Surface();
+               glm::vec3 pos(s.Position());
+               pos[(srf + 2) % 3] += srf < 3 ? distance : -distance;
+               Reference(s.GetPlanet());
+               return MapView(srf, pos, roll);
+       } else {
+               glm::vec3 pos(s.Position());
+               pos += glm::normalize(pos) * distance;
+               return Orbital(pos);
+       }
+}
+
 Camera &Camera::Orbital(const glm::vec3 &pos) noexcept {
        track_orient = false;
        view = glm::lookAt(pos, glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
index e3df0417ce68f81e980bb192f31d332bf2652f98..b8a7bf602890af49c3a752939f41e79d34e3374a 100644 (file)
@@ -35,6 +35,9 @@ public:
        void Show(creature::Creature &);
        void Hide() noexcept;
 
+       bool Shown() const noexcept { return c; }
+       const creature::Creature &GetCreature() const noexcept { return *c; }
+
        void Draw(app::Assets &, graphics::Viewport &) noexcept;
 
 private: