fix: viewport not resizing with window/widget

This commit is contained in:
maelstrom 2025-01-30 01:24:28 +01:00
parent 9932f2ed53
commit f9683a69be
4 changed files with 27 additions and 8 deletions

View file

@ -24,6 +24,7 @@ void processInput(GLFWwindow* window);
void mouseCallback(GLFWwindow* window, double xpos, double ypos); void mouseCallback(GLFWwindow* window, double xpos, double ypos);
// void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); // void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void resizeCallback(GLFWwindow* window, int width, int height);
std::shared_ptr<Part> lastPart; std::shared_ptr<Part> lastPart;
@ -35,12 +36,13 @@ int main() {
glfwSetKeyCallback(window, keyCallback); glfwSetKeyCallback(window, keyCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback); glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, mouseCallback); glfwSetCursorPosCallback(window, mouseCallback);
glfwSetFramebufferSizeCallback(window, resizeCallback);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glewInit(); glewInit();
simulationInit(); simulationInit();
renderInit(window); renderInit(window, 1200, 900);
// Baseplate // Baseplate
workspace->AddChild(Part::New({ 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_M && action == GLFW_PRESS) mode = 0;
if (key == GLFW_KEY_E && action == GLFW_PRESS) mode = 1; // Enlarge if (key == GLFW_KEY_E && action == GLFW_PRESS) mode = 1; // Enlarge
if (key == GLFW_KEY_R && action == GLFW_PRESS) mode = 2; 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);
} }

View file

@ -21,14 +21,18 @@ MainGLWidget::MainGLWidget(QWidget* parent): QOpenGLWidget(parent) {
void MainGLWidget::initializeGL() { void MainGLWidget::initializeGL() {
glewInit(); glewInit();
renderInit(NULL); renderInit(NULL, width(), height());
} }
extern int vpx, vpy;
void MainGLWidget::resizeGL(int w, int h) { void MainGLWidget::resizeGL(int w, int h) {
// Update projection matrix and other size related settings: // Update projection matrix and other size related settings:
// m_projection.setToIdentity(); // m_projection.setToIdentity();
// m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f); // m_projection.perspective(45.0f, w / float(h), 0.01f, 100.0f);
// ... // ...
// glViewport(0, 0, w, h);
setViewport(w, h);
} }
void MainGLWidget::paintGL() { void MainGLWidget::paintGL() {

View file

@ -28,8 +28,11 @@ extern Camera camera;
Skybox* skyboxTexture = NULL; Skybox* skyboxTexture = NULL;
Texture3D* studsTexture = NULL; Texture3D* studsTexture = NULL;
void renderInit(GLFWwindow* window) { static int viewportWidth, viewportHeight;
glViewport(0, 0, 1200, 900);
void renderInit(GLFWwindow* window, int width, int height) {
viewportWidth = width, viewportHeight = height;
glViewport(0, 0, width, height);
initMeshes(); initMeshes();
@ -64,7 +67,7 @@ void renderParts() {
// shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f)); // shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
// view/projection transformations // 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(); glm::mat4 view = camera.getLookAt();
shader->set("projection", projection); shader->set("projection", projection);
shader->set("view", view); shader->set("view", view);
@ -126,7 +129,7 @@ void renderSkyBox() {
skyboxShader->use(); 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) // Remove translation component of view, making us always at (0, 0, 0)
glm::mat4 view = glm::mat4(glm::mat3(camera.getLookAt())); glm::mat4 view = glm::mat4(glm::mat3(camera.getLookAt()));
@ -145,4 +148,8 @@ void render(GLFWwindow* window) {
renderSkyBox(); renderSkyBox();
renderParts(); renderParts();
}
void setViewport(int width, int height) {
viewportWidth = width, viewportHeight = height;
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
void renderInit(GLFWwindow* window); void renderInit(GLFWwindow* window, int width, int height);
void render(GLFWwindow* window); void render(GLFWwindow* window);
void setViewport(int width, int height);