From 0a88bec7fb2a3afd747124e7813da82dbc328b49 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Tue, 28 Jul 2020 09:48:50 +0200 Subject: [PATCH] [SettingsMenuState] Added menu option to change camera FOV. [Minimap] Now updating blue triangle when FOV or render distance changes. --- source/client/hud/Minimap.cpp | 58 ++++++++++++++-------- source/client/hud/Minimap.hpp | 2 + source/client/states/SettingsMenuState.cpp | 5 ++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/source/client/hud/Minimap.cpp b/source/client/hud/Minimap.cpp index db684f8b..ca6abb03 100644 --- a/source/client/hud/Minimap.cpp +++ b/source/client/hud/Minimap.cpp @@ -39,28 +39,7 @@ Minimap::Minimap() { m_playerChunk.setSize(chunkSize, chunkSize); m_playerChunk.setFillColor(gk::Color::Red); - // FOV/render distance viewer - gk::Vertex vertices[3]; - vertices[0].coord3d[0] = 0.f; - vertices[0].coord3d[1] = 0.f; - - vertices[1].coord3d[0] = -sin(glm::radians(Config::cameraFOV / 2.f)) * Config::renderDistance * (chunkSize + 2) / cos(glm::radians(Config::cameraFOV / 2.f)); - vertices[1].coord3d[1] = -(Config::renderDistance * (chunkSize + 2)); - - vertices[2].coord3d[0] = sin(glm::radians(Config::cameraFOV / 2.f)) * Config::renderDistance * (chunkSize + 2) / cos(glm::radians(Config::cameraFOV / 2.f)); - vertices[2].coord3d[1] = -(Config::renderDistance * (chunkSize + 2)); - - gk::Color color = gk::Color::Blue; - for (u8 i = 0 ; i < 3 ; ++i) { - vertices[i].color[0] = color.r; - vertices[i].color[1] = color.g; - vertices[i].color[2] = color.b; - vertices[i].color[3] = color.a; - } - - gk::VertexBuffer::bind(&m_vbo); - m_vbo.setData(sizeof(vertices), vertices, GL_STATIC_DRAW); - gk::VertexBuffer::bind(nullptr); + updatePlayerFovVertexBuffer(); } void Minimap::update(const ClientPlayer &player, class ClientWorld &world) { @@ -94,6 +73,16 @@ void Minimap::update(const ClientPlayer &player, class ClientWorld &world) { m_playerFovRotationTransform = gk::Transform::Identity; m_playerFovRotationTransform.rotate(player.cameraYaw() - 90.f, {0, 0, -1}); + + static float oldCameraFov = Config::cameraFOV; + static u16 oldRenderDistance = Config::renderDistance; + + if (oldCameraFov != Config::cameraFOV || oldRenderDistance != Config::renderDistance) { + updatePlayerFovVertexBuffer(); + + oldCameraFov = Config::cameraFOV; + oldRenderDistance = Config::renderDistance; + } } void Minimap::onChunkCreatedEvent(const ChunkCreatedEvent &event) { @@ -109,6 +98,31 @@ void Minimap::onChunkRemovedEvent(const ChunkRemovedEvent &event) { m_chunks.erase(event.chunkPos); } +void Minimap::updatePlayerFovVertexBuffer() { + // FOV/render distance viewer + gk::Vertex vertices[3]; + vertices[0].coord3d[0] = 0.f; + vertices[0].coord3d[1] = 0.f; + + vertices[1].coord3d[0] = -sin(glm::radians(Config::cameraFOV / 2.f)) * Config::renderDistance * (chunkSize + 2) / cos(glm::radians(Config::cameraFOV / 2.f)); + vertices[1].coord3d[1] = -(Config::renderDistance * (chunkSize + 2)); + + vertices[2].coord3d[0] = sin(glm::radians(Config::cameraFOV / 2.f)) * Config::renderDistance * (chunkSize + 2) / cos(glm::radians(Config::cameraFOV / 2.f)); + vertices[2].coord3d[1] = -(Config::renderDistance * (chunkSize + 2)); + + gk::Color color = gk::Color::Blue; + for (u8 i = 0 ; i < 3 ; ++i) { + vertices[i].color[0] = color.r; + vertices[i].color[1] = color.g; + vertices[i].color[2] = color.b; + vertices[i].color[3] = color.a; + } + + gk::VertexBuffer::bind(&m_vbo); + m_vbo.setData(sizeof(vertices), vertices, GL_DYNAMIC_DRAW); + gk::VertexBuffer::bind(nullptr); +} + void Minimap::draw(gk::RenderTarget &target, gk::RenderStates states) const { states.transform *= getTransform(); diff --git a/source/client/hud/Minimap.hpp b/source/client/hud/Minimap.hpp index 43ffe9f1..e524f73f 100644 --- a/source/client/hud/Minimap.hpp +++ b/source/client/hud/Minimap.hpp @@ -48,6 +48,8 @@ class Minimap : public gk::Drawable, public gk::Transformable { static const u16 minimapSize = 50; private: + void updatePlayerFovVertexBuffer(); + void draw(gk::RenderTarget &target, gk::RenderStates states) const override; std::unordered_map m_chunks; diff --git a/source/client/states/SettingsMenuState.cpp b/source/client/states/SettingsMenuState.cpp index 81eca2a8..e175e1b5 100644 --- a/source/client/states/SettingsMenuState.cpp +++ b/source/client/states/SettingsMenuState.cpp @@ -274,6 +274,11 @@ void SettingsMenuState::addGraphicsButtons() { addToggleButton("Star Rendering", Config::isStarRenderingEnabled, false); + m_menuWidget.addSlider("FOV: " + std::to_string((int)Config::cameraFOV), [] (SliderWidget &slider, u32) { + Config::cameraFOV = slider.getCurrentValue(); + slider.setText("FOV: " + std::to_string((int)Config::cameraFOV)); + }, 45, 135, Config::cameraFOV); + updateWidgetPosition(); }