From f9683a69bebbac28215c6bdcd21a244f6e766561 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 30 Jan 2025 01:24:28 +0100 Subject: [PATCH] fix: viewport not resizing with window/widget --- client/src/main.cpp | 9 ++++++++- editor/mainglwidget.cpp | 6 +++++- src/rendering/renderer.cpp | 15 +++++++++++---- src/rendering/renderer.h | 5 +++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/client/src/main.cpp b/client/src/main.cpp index 42bd0b0..5cb545c 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -24,6 +24,7 @@ void processInput(GLFWwindow* window); void mouseCallback(GLFWwindow* window, double xpos, double ypos); // void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); +void resizeCallback(GLFWwindow* window, int width, int height); std::shared_ptr lastPart; @@ -35,12 +36,13 @@ int main() { glfwSetKeyCallback(window, keyCallback); glfwSetMouseButtonCallback(window, mouseButtonCallback); glfwSetCursorPosCallback(window, mouseCallback); + glfwSetFramebufferSizeCallback(window, resizeCallback); glfwMakeContextCurrent(window); glewInit(); simulationInit(); - renderInit(window); + renderInit(window, 1200, 900); // Baseplate workspace->AddChild(Part::New({ @@ -204,4 +206,9 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods if (key == GLFW_KEY_M && action == GLFW_PRESS) mode = 0; if (key == GLFW_KEY_E && action == GLFW_PRESS) mode = 1; // Enlarge if (key == GLFW_KEY_R && action == GLFW_PRESS) mode = 2; +} + +void resizeCallback(GLFWwindow* window, int width, int height) { + glViewport(0, 0, width, height); + setViewport(width, height); } \ No newline at end of file diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index 0427a3a..0e8e09f 100644 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -21,14 +21,18 @@ MainGLWidget::MainGLWidget(QWidget* parent): QOpenGLWidget(parent) { void MainGLWidget::initializeGL() { glewInit(); - renderInit(NULL); + renderInit(NULL, width(), height()); } +extern int vpx, vpy; + void MainGLWidget::resizeGL(int w, int h) { // Update projection matrix and other size related settings: // m_projection.setToIdentity(); // m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f); // ... + // glViewport(0, 0, w, h); + setViewport(w, h); } void MainGLWidget::paintGL() { diff --git a/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index 480d4df..c59b32e 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -28,8 +28,11 @@ extern Camera camera; Skybox* skyboxTexture = NULL; Texture3D* studsTexture = NULL; -void renderInit(GLFWwindow* window) { - glViewport(0, 0, 1200, 900); +static int viewportWidth, viewportHeight; + +void renderInit(GLFWwindow* window, int width, int height) { + viewportWidth = width, viewportHeight = height; + glViewport(0, 0, width, height); initMeshes(); @@ -64,7 +67,7 @@ void renderParts() { // shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f)); // view/projection transformations - glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)viewportWidth / (float)viewportHeight, 0.1f, 100.0f); glm::mat4 view = camera.getLookAt(); shader->set("projection", projection); shader->set("view", view); @@ -126,7 +129,7 @@ void renderSkyBox() { skyboxShader->use(); - glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f); + glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)viewportWidth / (float)viewportHeight, 0.1f, 100.0f); // Remove translation component of view, making us always at (0, 0, 0) glm::mat4 view = glm::mat4(glm::mat3(camera.getLookAt())); @@ -145,4 +148,8 @@ void render(GLFWwindow* window) { renderSkyBox(); renderParts(); +} + +void setViewport(int width, int height) { + viewportWidth = width, viewportHeight = height; } \ No newline at end of file diff --git a/src/rendering/renderer.h b/src/rendering/renderer.h index 5ba5435..0596e69 100644 --- a/src/rendering/renderer.h +++ b/src/rendering/renderer.h @@ -1,5 +1,6 @@ #pragma once #include -void renderInit(GLFWwindow* window); -void render(GLFWwindow* window); \ No newline at end of file +void renderInit(GLFWwindow* window, int width, int height); +void render(GLFWwindow* window); +void setViewport(int width, int height); \ No newline at end of file