From e0180e01f4c659c97973f585fea5eb3344254ce0 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 27 Nov 2017 22:47:33 +0100 Subject: [PATCH] track creature with camera --- src/app/states.cpp | 10 ++++++++++ src/blobs.cpp | 2 +- src/graphics/Camera.hpp | 5 +++++ src/graphics/CreatureSkin.hpp | 1 + src/graphics/PlanetSurface.hpp | 1 + src/graphics/SunSurface.hpp | 1 + src/graphics/shader.cpp | 24 ++++++++++++++++++++++++ src/graphics/viewport.cpp | 30 +++++++++++++++++++++--------- src/ui/CreaturePanel.hpp | 3 +++ 9 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/app/states.cpp b/src/app/states.cpp index d98a099..d1e092e 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -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 diff --git a/src/blobs.cpp b/src/blobs.cpp index c1dfbbc..836c51d 100644 --- a/src/blobs.cpp +++ b/src/blobs.cpp @@ -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)) ; diff --git a/src/graphics/Camera.hpp b/src/graphics/Camera.hpp index 51dd8f1..736020d 100644 --- a/src/graphics/Camera.hpp +++ b/src/graphics/Camera.hpp @@ -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; } diff --git a/src/graphics/CreatureSkin.hpp b/src/graphics/CreatureSkin.hpp index 1b5b6b8..c24a746 100644 --- a/src/graphics/CreatureSkin.hpp +++ b/src/graphics/CreatureSkin.hpp @@ -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; diff --git a/src/graphics/PlanetSurface.hpp b/src/graphics/PlanetSurface.hpp index a5f177d..b915283 100644 --- a/src/graphics/PlanetSurface.hpp +++ b/src/graphics/PlanetSurface.hpp @@ -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; diff --git a/src/graphics/SunSurface.hpp b/src/graphics/SunSurface.hpp index 6572761..f10e0e5 100644 --- a/src/graphics/SunSurface.hpp +++ b/src/graphics/SunSurface.hpp @@ -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; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index dbdeeb1..6a08bb5 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -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; diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index ac0b9e5..398eb42 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -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 #include @@ -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)); diff --git a/src/ui/CreaturePanel.hpp b/src/ui/CreaturePanel.hpp index e3df041..b8a7bf6 100644 --- a/src/ui/CreaturePanel.hpp +++ b/src/ui/CreaturePanel.hpp @@ -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: -- 2.39.2