[SettingsMenuState] Added menu option to change camera FOV. [Minimap] Now updating blue triangle when FOV or render distance changes.

This commit is contained in:
Quentin Bazin 2020-07-28 09:48:50 +02:00
parent ee1efccca5
commit 0a88bec7fb
3 changed files with 43 additions and 22 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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();
}