Skip to content

Commit

Permalink
[SettingsMenuState] Added menu option to change camera FOV. [Minimap]…
Browse files Browse the repository at this point in the history
… Now updating blue triangle when FOV or render distance changes.
  • Loading branch information
Unarelith committed Jul 28, 2020
1 parent ee1efcc commit 0a88bec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
58 changes: 36 additions & 22 deletions source/client/hud/Minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions source/client/hud/Minimap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gk::Vector3i, gk::RectangleShape> m_chunks;
Expand Down
5 changes: 5 additions & 0 deletions source/client/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 0a88bec

Please sign in to comment.