From 1dd41256ff4c033365a0011896ada862feb32b8d Mon Sep 17 00:00:00 2001 From: lenn Date: Mon, 2 Feb 2026 17:49:48 +0800 Subject: [PATCH] daily update --- main.cpp | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/main.cpp b/main.cpp index 8b90385..113061d 100644 --- a/main.cpp +++ b/main.cpp @@ -3,19 +3,12 @@ #include #include #include +#include #include #include #include #include -void framebuffer_size_callback(GLFWwindow* window, int width, int height) { - glViewport(0, 0, width, height); -} -void process_input(GLFWwindow* window) { - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { - glfwSetWindowShouldClose(window, true); - } -} class GLWidget { @@ -24,7 +17,8 @@ public: int width; int height; }; - GLWidget(ViewPort port) : viewport_(port) { + GLWidget(ViewPort port) : viewport_(port), + window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);}) { lastX_ = port.width / 2; lastY_ = port.height / 2; } @@ -36,20 +30,21 @@ public: } bool initGeometry() { - window = std::make_unique(glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL)); - if (window == NULL) { + // window_ = std::make_unique(glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL)); + window_.reset(glfwCreateWindow(800, 600, "3dviewer", nullptr, nullptr)); + if (!window_) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } - glfwMakeContextCurrent(window); + glfwMakeContextCurrent(window_.get()); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } glViewport(0, 0, 800, 600); - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebuffer_size_callback); } public: bool initBgProgram(const std::string& vpath, const std::string fpath) { @@ -109,9 +104,20 @@ public: } } + +private: + void framebuffer_size_callback(GLFWwindow* window, int width, int height) { + glViewport(0, 0, viewport_.width, viewport_.height); + } + + void process_input(GLFWwindow* window) { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(window, true); + } + } private: std::unique_ptr bgProg_; - std::unique_ptr window_; + std::unique_ptr> window_; unsigned int bgVao_; unsigned int bgVbo_; ViewPort viewport_{800, 600};