update
This commit is contained in:
176
main.cpp
176
main.cpp
@@ -4,7 +4,10 @@
|
||||
#include <GL/glext.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <functional>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
@@ -17,19 +20,28 @@ public:
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
GLWidget(ViewPort port) : viewport_(port),
|
||||
window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);}) {
|
||||
GLWidget(ViewPort port) : viewport_(port)
|
||||
, window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);})
|
||||
, deinit_(false) {
|
||||
lastX_ = port.width / 2;
|
||||
lastY_ = port.height / 2;
|
||||
initGeometry();
|
||||
}
|
||||
~GLWidget() {
|
||||
if (bgVao_) {
|
||||
glDeleteVertexArrays(1, &bgVao_);
|
||||
}
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
if (!deinit_) {
|
||||
// OpenGL objects should be deleted while a valid context still exists.
|
||||
if (window_) {
|
||||
glfwMakeContextCurrent(window_.get());
|
||||
}
|
||||
if (bgVao_) {
|
||||
glDeleteVertexArrays(1, &bgVao_);
|
||||
}
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
}
|
||||
deinit_ = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setViewPort(int width, int height) {
|
||||
@@ -37,7 +49,10 @@ public:
|
||||
}
|
||||
|
||||
bool initGeometry() {
|
||||
glfwInit();
|
||||
if (!glfwInit()) {
|
||||
std::cout << "Failed to initialize GLFW" << std::endl;
|
||||
return false;
|
||||
}
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
@@ -48,18 +63,20 @@ public:
|
||||
if (!window_) {
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
glfwMakeContextCurrent(window_.get());
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
glViewport(0, 0, 800, 600);
|
||||
glfwSetWindowUserPointer(window_.get(), this);
|
||||
glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebufferSizeCallback);
|
||||
glfwSetCursorPosCallback(window_.get(), &GLWidget::mouseCallback);
|
||||
glfwSetScrollCallback(window_.get(), &GLWidget::scrollCallback);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setBgShaderPath(std::string vp, std::string fp) {
|
||||
@@ -95,16 +112,100 @@ private:
|
||||
|
||||
}
|
||||
|
||||
void initPanelGeometry_() {
|
||||
if (panelVbo_) {
|
||||
glDeleteBuffers(1, &panelVbo_);
|
||||
panelVbo_ = 0;
|
||||
}
|
||||
if (panelVao_) {
|
||||
glDeleteVertexArrays(1, &panelVao_);
|
||||
panelVao_ = 0;
|
||||
}
|
||||
|
||||
const float y = panelHeight_ * 0.5f;
|
||||
const float x = panelWidth_ * 0.5f;
|
||||
const float z = panelDepth_ * 0.5f;
|
||||
|
||||
struct V {
|
||||
float x, y, z;
|
||||
float nx, ny, nz;
|
||||
};
|
||||
const V verts[24] = {
|
||||
// 顶面
|
||||
{-x, y, -z, 0, 1, 0},
|
||||
{x, y, -z, 0, 1, 0},
|
||||
{x, y, z, 0, 1, 0 },
|
||||
{-x, y, z, 0, 1, 0},
|
||||
|
||||
// 前面
|
||||
{-x, y, z, 0, 0, 1},
|
||||
{x, y, z, 0, 0, 1},
|
||||
{x, -y, z, 0, 0, 1},
|
||||
{-x, -y, z, 0, 0, 1},
|
||||
|
||||
// 底面
|
||||
{-x, -y, -z, 0, -1, 0},
|
||||
{x, -y, -z, 0, -1, 0},
|
||||
{x, -y, z, 0, -1, 0},
|
||||
{-x, -y, z, 0, -1, 0},
|
||||
|
||||
// 后面
|
||||
{-x, y, -z, 0, 0, -1},
|
||||
{x, y, -z, 0, 0, -1},
|
||||
{x, -y, -z, 0, 0, -1},
|
||||
{-x, -y, -z, 0, 0, -1},
|
||||
|
||||
// 左面
|
||||
{-x, y, z, -1, 0, 0},
|
||||
{-x, y, -z, -1, 0, 0},
|
||||
{-x, -y, -z, -1, 0, 0},
|
||||
{-x, -y, z, -1, 0, 0},
|
||||
|
||||
// 右面
|
||||
{x, y, -z, 1, 0, 0},
|
||||
{x, y, z, 1, 0, 0},
|
||||
{x, -y, z, 1, 0, 0},
|
||||
{x, -y, -z, 1, 0, 0},
|
||||
};
|
||||
|
||||
unsigned int idx[36] = {
|
||||
0, 1, 2, 0, 2, 3,
|
||||
4, 5, 6, 4, 6, 7,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
12, 13, 14, 12, 14, 15,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
20, 21, 22, 20, 22, 23,
|
||||
};
|
||||
|
||||
glGenVertexArrays(1, &panelVao_);
|
||||
|
||||
}
|
||||
|
||||
bool initBgProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
vshader->compileShaderFromFile(bgVertShaderPath_);
|
||||
fshader->compileShaderFromFile(bgFragShaderPath_);
|
||||
if (!vshader->compileShaderFromFile(bgVertShaderPath_)) {
|
||||
std::cout << "Vertex shader compile failed: " << bgVertShaderPath_ << "\n"
|
||||
<< vshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!fshader->compileShaderFromFile(bgFragShaderPath_)) {
|
||||
std::cout << "Fragment shader compile failed: " << bgFragShaderPath_ << "\n"
|
||||
<< fshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
bgProg_ = std::make_unique<LOpenGLProgram>();
|
||||
bgProg_->addShader(std::move(vshader));
|
||||
bgProg_->addShader(std::move(fshader));
|
||||
if (!bgProg_->addShader(std::move(vshader))) {
|
||||
std::cout << "Failed to attach vertex shader\n";
|
||||
return false;
|
||||
}
|
||||
if (!bgProg_->addShader(std::move(fshader))) {
|
||||
std::cout << "Failed to attach fragment shader\n";
|
||||
return false;
|
||||
}
|
||||
bool ret = bgProg_->Link();
|
||||
if (!ret) {
|
||||
std::cout << "Failed to link background program\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -119,7 +220,7 @@ private:
|
||||
bgProg_->Use();
|
||||
bgProg_->setUniformValue("uViewport", glm::vec2(viewport_.width, viewport_.height));
|
||||
bgProg_->setUniformValue("uMajorStep", 120.0f);
|
||||
bgProg_->setUniformValue("uMinjorStep", 24.0f);
|
||||
bgProg_->setUniformValue("uMinorStep", 24.0f);
|
||||
glBindVertexArray(bgVao_);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
@@ -131,11 +232,20 @@ private:
|
||||
public:
|
||||
|
||||
void eventLoop() {
|
||||
if (!window_) {
|
||||
return;
|
||||
}
|
||||
initBgGeometry();
|
||||
initBgProgram();
|
||||
if (!initBgProgram()) {
|
||||
return;
|
||||
}
|
||||
while (!glfwWindowShouldClose(window_.get())) {
|
||||
processInput(window_.get());
|
||||
glClearColor(0.08f, 0.08f, 0.10f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
drawBg();
|
||||
glfwSwapBuffers(window_.get());
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +259,7 @@ private:
|
||||
}
|
||||
|
||||
void onFramebufferSize(int width, int height) {
|
||||
viewport_ = {width, height};
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
@@ -171,6 +282,7 @@ private:
|
||||
float ypos = static_cast<float>(y);
|
||||
|
||||
if (camera_.firstMouse()) {
|
||||
std::cout << "in firstMouse()\n";
|
||||
lastX_ = x;
|
||||
lastY_ = y;
|
||||
camera_.triggleFirstMouse();
|
||||
@@ -186,8 +298,10 @@ private:
|
||||
}
|
||||
|
||||
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||
std::cout << "scrollCallback\n";
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
std::cout << "auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window)) failed\n";
|
||||
return;
|
||||
}
|
||||
self->onScrollRoll(xoffset, yoffset);
|
||||
@@ -202,23 +316,35 @@ private:
|
||||
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
|
||||
std::string bgFragShaderPath_;
|
||||
std::string bgVertShaderPath_;
|
||||
unsigned int bgVao_;
|
||||
unsigned int bgVbo_;
|
||||
unsigned int bgVao_ = 0;
|
||||
unsigned int bgVbo_ = 0;
|
||||
|
||||
std::unique_ptr<LOpenGLProgram> panelProg_;
|
||||
std::string panelVertShaderPath_;
|
||||
std::string panelFragShaderPath_;
|
||||
unsigned int panelVao_ = 0;
|
||||
unsigned int panelVbo_ = 0;
|
||||
float panelWidth_ = 0.25;
|
||||
float panelHeight_ = 0.35;
|
||||
float panelDepth_ = 0.05;
|
||||
|
||||
|
||||
ViewPort viewport_{800, 600};
|
||||
Camera camera_;
|
||||
|
||||
bool firstMouse_ = true;
|
||||
float lastX_;
|
||||
float lastY_;
|
||||
|
||||
bool deinit_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
GLWidget glw({600, 800});
|
||||
glw.setBgShaderPath("bg.vert", "bg.frag");
|
||||
|
||||
glw.eventLoop();
|
||||
|
||||
{
|
||||
GLWidget glw({600, 800});
|
||||
glw.setBgShaderPath("../shader/bg.vert", "../shader/bg.frag");
|
||||
glw.eventLoop();
|
||||
}
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user