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 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<Part> 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({
@ -205,3 +207,8 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
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);
}

View file

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

View file

@ -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()));
@ -146,3 +149,7 @@ void render(GLFWwindow* window) {
renderSkyBox();
renderParts();
}
void setViewport(int width, int height) {
viewportWidth = width, viewportHeight = height;
}

View file

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