Files
3dviewer/main.cpp
2026-02-02 17:49:48 +08:00

152 lines
4.3 KiB
C++

#include "lopenglprogram.h"
#include "camera.h"
#include <GL/gl.h>
#include <GL/glext.h>
#include <GLFW/glfw3.h>
#include <functional>
#include <glm/fwd.hpp>
#include <iostream>
#include <memory>
#include <utility>
class GLWidget {
public:
struct ViewPort {
int width;
int height;
};
GLWidget(ViewPort port) : viewport_(port),
window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);}) {
lastX_ = port.width / 2;
lastY_ = port.height / 2;
}
~GLWidget() {}
void setViewPort(int width, int height) {
viewport_ = {width, height};
}
bool initGeometry() {
// window_ = std::make_unique<GLFWwindow*>(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_.get());
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebuffer_size_callback);
}
public:
bool initBgProgram(const std::string& vpath, const std::string fpath) {
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
vshader->compileShaderFromFile(vpath);
fshader->compileShaderFromFile(fpath);
bgProg_ = std::make_unique<LOpenGLProgram>();
bgProg_->addShader(std::move(vshader));
bgProg_->addShader(std::move(fshader));
bool ret = bgProg_->Link();
if (!ret) {
return false;
}
const float verts[] = {
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0,
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
};
if (bgVao_) {
glDeleteVertexArrays(1, &bgVao_);
}
if (bgVbo_) {
glDeleteBuffers(1, &bgVbo_);
}
glGenVertexArrays(1, &bgVao_);
glBindVertexArray(bgVao_);
glGenBuffers(1, &bgVbo_);
glBindBuffer(GL_ARRAY_BUFFER, bgVbo_);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glBindVertexArray(0);
}
void drawBg() {
bgProg_->Use();
bgProg_->setUniformValue("uViewport", glm::vec2(viewport_.width, viewport_.height));
bgProg_->setUniformValue("uMajorStep", 120.0f);
bgProg_->setUniformValue("uMinjorStep", 24.0f);
glBindVertexArray(bgVao_);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
}
void mouseCallback(GLFWwindow* window, double x, double y) {
float xpos = static_cast<float>(x);
float ypos = static_cast<float>(y);
if (camera_.fir) {
}
}
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<LOpenGLProgram> bgProg_;
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
unsigned int bgVao_;
unsigned int bgVbo_;
ViewPort viewport_{800, 600};
Camera camera_;
bool firstMouse_ = true;
float lastX_;
float lastY_;
};
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#if __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLWidget glw;
glw.initBgProgram("bg.vert", "bg.frag");
while (!glfwWindowShouldClose(window)) {
glw.drawBg();
}
glfwTerminate();
return 0;
}