Camera updates correctly on window resize

This commit is contained in:
Jack 2024-08-28 09:45:08 -04:00
parent 1b54280c52
commit 97e13c088e
8 changed files with 59 additions and 14 deletions

View File

@ -19,7 +19,7 @@ Collapsed=0
[Window][TerraVisor]
Pos=0,0
Size=2199,944
Size=1676,1017
Collapsed=0
[Window][Scene Window]
@ -30,20 +30,20 @@ DockId=0x00000003,0
[Window][Viewport]
Pos=0,19
Size=1603,925
Size=1224,998
Collapsed=0
DockId=0x00000003,0
[Window][Scene Settings]
Pos=1605,19
Size=594,925
Pos=1226,19
Size=450,998
Collapsed=0
DockId=0x00000004,0
[Docking][Data]
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=2199,925 Split=X Selected=0x9F2D9299
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1676,998 Split=X Selected=0x9F2D9299
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=0,0 Split=X Selected=0x13926F0B
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1603,701 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=594,701 Selected=0x413E6147
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=2107,701 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=450,701 Selected=0x413E6147
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=419,701 Selected=0xF69494A7

View File

@ -13,6 +13,7 @@ struct Framebuffer {
namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height);
void UpdateFrameBuffer(Framebuffer& fbo, int width, int height);
void Bind(const Framebuffer& fbo);
void Unbind();
void Cleanup(const Framebuffer& fbo);

View File

@ -20,6 +20,7 @@ struct Window {
namespace Interface {
void AddWindow(std::vector<Window>& windows, const std::string& name, std::function<void()> renderFunc);
Window* GetWindowByName(std::vector<Window>& windows, const std::string& name);
void RenderWindows(std::vector<Window>& windows);
}

View File

@ -27,11 +27,14 @@ class Scene {
void Idle();
void ReloadShader();
void UpdateCamera();
void OnScreenResize(Window* viewport);
void UpdateViewport();
std::vector<int16_t> LoadHGT(const std::string& filename, int width, int height);
int window_width;
int window_height;
std::vector<Window> windows_;
private:
void InitBuffers();
@ -52,7 +55,6 @@ class Scene {
float near_z_;
float far_z_;
float fov_;
std::vector<Window> windows_;
glm::mat4 view_matrix_;
glm::mat4 projection_matrix_;

View File

@ -1,6 +1,7 @@
#include <glm/glm.hpp>
#include "callbacks.h"
#include "interface.h"
namespace Callbacks {
Scene* scene_ = nullptr;
@ -52,10 +53,13 @@ void Callbacks::MouseButton(GLFWwindow* window, int button, int action, int mods
void Callbacks::Resize(GLFWwindow* window, int width, int height)
{
width = glm::max(1, width);
height = glm::max(1, height);
//Set viewport to cover entire framebuffer
glViewport(0, 0, width, height);
Window* viewport = Interface::GetWindowByName(scene_->windows_, "Viewport");
scene_->window_width = static_cast<int>(viewport->size.x);
scene_->window_height = static_cast<int>(viewport->size.y);
scene_->OnScreenResize(viewport);
//Set aspect ratio used in view matrix calculation
// scene_->window_width = width;
// scene_->window_height = height;

View File

@ -54,6 +54,13 @@ namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height) {
Framebuffer fbo;
fbo.id = CreateFramebuffer();
UpdateFrameBuffer(fbo, width, height);
return fbo;
}
void UpdateFrameBuffer(Framebuffer& fbo, int width, int height) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
fbo.albedoTexture = CreateTexture(width, height, GL_RGBA);
@ -73,8 +80,6 @@ namespace FBO {
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return fbo;
}
void Bind(const Framebuffer& fbo) {

View File

@ -2,6 +2,8 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <algorithm>
#include <iostream>
namespace Interface {
@ -9,6 +11,18 @@ void AddWindow(std::vector<Window>& windows, const std::string& name, std::funct
windows.emplace_back(name, renderFunc);
}
Window* GetWindowByName(std::vector<Window>& windows, const std::string& name) {
auto it = std::find_if(windows.begin(), windows.end(), [&name](const Window& window) {
return window.name == name;
});
if (it != windows.end()) {
return &(*it); // Return pointer to the found window
} else {
return nullptr; // Return nullptr if the window was not found
}
}
void RenderWindows(std::vector<Window>& windows) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();

View File

@ -209,6 +209,9 @@ void Scene::InitUI() {
ImGui::SliderFloat("Tessellation Level", &Uniforms::GeoData.maxTessellation, 1.0f, 64.0f);
ImGui::SliderInt("Patch Grid Density", &Uniforms::GeoData.gridDensity, 1, 24);
ImGui::SliderFloat("Max Height", &Uniforms::GeoData.displacementScale, 0.0f, 1.0f);
Window* viewport = Interface::GetWindowByName(windows_, "Viewport");
ImGui::Text("Viewport size: (%.3f, %.3f)", viewport->size.x, viewport->size.y);
});
}
@ -239,6 +242,12 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Window* viewport = Interface::GetWindowByName(windows_, "Viewport");
if (viewport && viewport->size.x > 0 && viewport->size.y > 0) {
OnScreenResize(viewport);
}
Uniforms::GeoData.PV = projection_matrix_ * CameraControls::GetViewMatrix(activeCamera_); // Projection-View matrix
Uniforms::BufferSceneData();
@ -293,6 +302,15 @@ void Scene::Display(GLFWwindow* window) {
glfwSwapBuffers(window);
}
void Scene::OnScreenResize(Window* viewport) {
if (window_width != static_cast<int>(viewport->size.x) || window_height != static_cast<int>(viewport->size.y)) {
window_width = static_cast<int>(viewport->size.x);
window_height = static_cast<int>(viewport->size.y);
UpdateCamera();
}
}
void Scene::UpdateViewport() {
window_width = ImGui::GetWindowSize().x;
window_height = ImGui::GetWindowSize().y;