From f430ad789fe620ad2e8b2b2b99af868372791295 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 19 Oct 2015 14:36:16 +0200 Subject: [PATCH] use entity's eyes to aim also squashed some assets --- assets | 2 +- src/app/app.cpp | 2 ++ src/client/client.cpp | 4 ++++ src/graphics/Camera.hpp | 2 +- src/graphics/viewport.cpp | 7 ++++++- src/model/Instance.hpp | 2 ++ src/model/Model.hpp | 5 +++++ src/model/model.cpp | 7 ++++++- src/standalone/MasterState.cpp | 7 +++++-- src/world/Entity.hpp | 8 +++++--- src/world/world.cpp | 14 +++++++++++++- 11 files changed, 50 insertions(+), 10 deletions(-) diff --git a/assets b/assets index 03a7d61..ed7b812 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 03a7d614b7e7e7c57c87b022b9bf76b09c5242fb +Subproject commit ed7b81207b13b87a3a9097b9e25ebd06aa814d97 diff --git a/src/app/app.cpp b/src/app/app.cpp index 22a3b58..b3b3300 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -527,6 +527,8 @@ void AssetLoader::LoadModels( in.Skip(Token::EQUALS); if (prop_name == "root") { model.RootPart().Read(in, tex_index, shapes); + } else if (prop_name == "eyes") { + model.SetEyes(in.GetULong()); } else { while (in.HasMore() && in.Peek().type != Token::SEMICOLON) { in.Next(); diff --git a/src/client/client.cpp b/src/client/client.cpp index 89e306b..97292a9 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -65,6 +65,10 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) save.Write(master.GetWorldConf()); } res.Load(master.GetEnv().loader, "default"); + if (res.models.size() < 1) { + throw std::runtime_error("need at least one model to run"); + } + res.models[0].Instantiate(player.GetEntity().GetModel()); sounds.Load(master.GetEnv().loader, res.snd_index); interface.SetInventorySlots(res.block_types.size() - 1); chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index); diff --git a/src/graphics/Camera.hpp b/src/graphics/Camera.hpp index 924b5b4..95175de 100644 --- a/src/graphics/Camera.hpp +++ b/src/graphics/Camera.hpp @@ -19,7 +19,7 @@ public: const glm::mat4 &Projection() const noexcept { return projection; } const glm::mat4 &View() const noexcept { return view; } - void View(const glm::mat4 &v) noexcept { view = v; } + void View(const glm::mat4 &v) noexcept; private: void UpdateProjection() noexcept; diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index ad58b58..e171966 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -45,6 +45,9 @@ void Camera::Clip(float n, float f) noexcept { UpdateProjection(); } +void Camera::View(const glm::mat4 &v) noexcept { + view = v; +} void Camera::UpdateProjection() noexcept { projection = glm::perspective(fov, aspect, near, far); @@ -256,7 +259,9 @@ BlendedSprite &Viewport::SpriteProgram() noexcept { void Viewport::WorldPosition(const glm::mat4 &t) noexcept { - cam.View(glm::inverse(t)); + const glm::vec3 offset(0.0f, 0.0f, 0.0f); + //const glm::vec3 offset(0.0f, 0.0f, -5.0f); + cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(offset, 0.0f)))); ChunkProgram().SetView(cam.View()); sky_prog.Activate(); SkyBoxProgram().SetView(cam.View()); diff --git a/src/model/Instance.hpp b/src/model/Instance.hpp index 8b94bcd..09ab78e 100644 --- a/src/model/Instance.hpp +++ b/src/model/Instance.hpp @@ -26,6 +26,8 @@ public: operator bool() const noexcept { return model; } const Model &GetModel() const noexcept { return *model; } + glm::mat4 EyesTransform() const noexcept; + void Render(const glm::mat4 &, DirectionalLighting &); private: diff --git a/src/model/Model.hpp b/src/model/Model.hpp index c46caf4..55a9734 100644 --- a/src/model/Model.hpp +++ b/src/model/Model.hpp @@ -31,6 +31,10 @@ public: Part &GetPart(std::size_t i) noexcept { return *part[i]; } const Part &GetPart(std::size_t i) const noexcept { return *part[i]; } + void SetEyes(std::uint16_t id) { eyes_id = id; } + + const Part &GetEyesPart() const noexcept { return GetPart(eyes_id); } + void Enumerate(); void Instantiate(Instance &) const; @@ -38,6 +42,7 @@ private: std::uint32_t id; Part root; std::vector part; + std::uint16_t eyes_id; }; diff --git a/src/model/model.cpp b/src/model/model.cpp index 55050e0..451433b 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -27,6 +27,10 @@ Instance::~Instance() { } +glm::mat4 Instance::EyesTransform() const noexcept { + return model->GetEyesPart().GlobalTransform(*this); +} + void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) { model->RootPart().Render(M, *this, prog); } @@ -35,7 +39,8 @@ void Instance::Render(const glm::mat4 &M, DirectionalLighting &prog) { Model::Model() : id(0) , root() -, part() { +, part() +, eyes_id(0) { } diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 3695b20..1035bb2 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -42,8 +42,9 @@ MasterState::MasterState( if (res.models.size() < 2) { throw std::runtime_error("need at least two models to run"); } + res.models[0].Instantiate(player.GetEntity().GetModel()); sounds.Load(env.loader, res.snd_index); - spawner.LimitModels(0, res.models.size()); + spawner.LimitModels(1, res.models.size()); interface.SetInventorySlots(res.block_types.size() - 1); generator.LoadTypes(res.block_types); chunk_renderer.LoadTextures(env.loader, res.tex_index); @@ -131,7 +132,9 @@ void MasterState::Update(int dt) { } void MasterState::Render(Viewport &viewport) { - viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords())); + viewport.WorldPosition( + player.GetEntity().Transform(player.GetEntity().ChunkCoords()) + * player.GetEntity().GetModel().EyesTransform()); if (config.video.world) { chunk_renderer.Render(viewport); world.Render(viewport); diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index 7e85591..0a44303 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -63,9 +63,11 @@ public: const glm::quat &Orientation() const noexcept { return state.orient; } void Orientation(const glm::quat &o) noexcept { state.orient = o; } - glm::mat4 Transform(const glm::ivec3 &reference) const noexcept { - return state.Transform(reference); - } + /// get a transform for this entity's coordinate space + glm::mat4 Transform(const glm::ivec3 &reference) const noexcept; + /// get a transform for this entity's view space + glm::mat4 ViewTransform(const glm::ivec3 &reference) const noexcept; + /// get a ray in entity's face direction originating from center of vision Ray Aim(const Chunk::Pos &chunk_offset) const noexcept; void SetState(const EntityState &s) noexcept { state = s; } diff --git a/src/world/world.cpp b/src/world/world.cpp index 15f1cab..349b82d 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -45,8 +45,20 @@ void Entity::Position(const glm::vec3 &pos) noexcept { state.AdjustPosition(); } +glm::mat4 Entity::Transform(const glm::ivec3 &reference) const noexcept { + return state.Transform(reference); +} + +glm::mat4 Entity::ViewTransform(const glm::ivec3 &reference) const noexcept { + glm::mat4 transform = Transform(reference); + if (model) { + transform *= model.EyesTransform(); + } + return transform; +} + Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept { - glm::mat4 transform = Transform(chunk_offset); + glm::mat4 transform = ViewTransform(chunk_offset); glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); from /= from.w; glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f); -- 2.39.2