update
This commit is contained in:
2
camera.h
2
camera.h
@@ -91,6 +91,8 @@ private:
|
|||||||
float moveSpeed_;
|
float moveSpeed_;
|
||||||
float mouseSensitivity_;
|
float mouseSensitivity_;
|
||||||
float zoom_;
|
float zoom_;
|
||||||
|
|
||||||
|
bool firstMouse_ = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //INC_3DVIEWER_CAMERA_H
|
#endif //INC_3DVIEWER_CAMERA_H
|
||||||
@@ -180,6 +180,10 @@ bool LOpenGLProgram::addShader(std::unique_ptr<LOpenGLShader> shader) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec2 value) const {
|
||||||
|
glUniform2fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
||||||
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::removeShader(GLuint id) {
|
void LOpenGLProgram::removeShader(GLuint id) {
|
||||||
for (auto ite = shaderList_.begin(); ite != shaderList_.end(); ++ite) {
|
for (auto ite = shaderList_.begin(); ite != shaderList_.end(); ++ite) {
|
||||||
if ((*ite)->shaderId_ == id) {
|
if ((*ite)->shaderId_ == id) {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
@@ -52,6 +51,7 @@ public:
|
|||||||
void setUniformValue(const std::string& name, glm::vec3 value) const;
|
void setUniformValue(const std::string& name, glm::vec3 value) const;
|
||||||
void setUniformValue(const std::string& name, float x, float y, float z) const;
|
void setUniformValue(const std::string& name, float x, float y, float z) const;
|
||||||
void setUniformValue(const std::string& name, glm::mat4 value) const;
|
void setUniformValue(const std::string& name, glm::mat4 value) const;
|
||||||
|
void setUniformValue(const std::string& name, glm::vec2 value) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<std::unique_ptr<LOpenGLShader>> shaderList_;
|
std::list<std::unique_ptr<LOpenGLShader>> shaderList_;
|
||||||
|
|||||||
141
main.cpp
141
main.cpp
@@ -1,6 +1,12 @@
|
|||||||
#include "lopenglprogram.h"
|
#include "lopenglprogram.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include <GL/glext.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
}
|
}
|
||||||
@@ -11,19 +17,110 @@ void process_input(GLFWwindow* window) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<LOpenGLProgram> initBackgroundGeometry() {
|
class GLWidget {
|
||||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
|
||||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
public:
|
||||||
vshader->compileShaderFromFile("shader/bg.vert");
|
struct ViewPort {
|
||||||
fshader->compileShaderFromFile("shader/bg.frag");
|
int width;
|
||||||
auto program = std::make_unique<LOpenGLProgram>();
|
int height;
|
||||||
program->addShader(std::move(vshader));
|
};
|
||||||
program->addShader(std::move(fshader));
|
GLWidget(ViewPort port) : viewport_(port) {
|
||||||
auto ret = program->Link();
|
lastX_ = port.width / 2;
|
||||||
if (!ret)
|
lastY_ = port.height / 2;
|
||||||
return nullptr;
|
}
|
||||||
return program;
|
~GLWidget() {}
|
||||||
}
|
|
||||||
|
|
||||||
|
void setViewPort(int width, int height) {
|
||||||
|
viewport_ = {width, height};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool initGeometry() {
|
||||||
|
window = std::make_unique(glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL));
|
||||||
|
if (window == NULL) {
|
||||||
|
std::cout << "Failed to create GLFW window" << std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(window, 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:
|
||||||
|
std::unique_ptr<LOpenGLProgram> bgProg_;
|
||||||
|
std::unique_ptr<GLFWwindow> window_;
|
||||||
|
unsigned int bgVao_;
|
||||||
|
unsigned int bgVbo_;
|
||||||
|
ViewPort viewport_{800, 600};
|
||||||
|
Camera camera_;
|
||||||
|
|
||||||
|
bool firstMouse_ = true;
|
||||||
|
float lastX_;
|
||||||
|
float lastY_;
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
@@ -34,26 +131,14 @@ int main() {
|
|||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
||||||
if (window == NULL) {
|
|
||||||
std::cout << "Failed to create GLFW window" << std::endl;
|
|
||||||
glfwTerminate();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
glViewport(0, 0, 800, 600);
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
||||||
|
|
||||||
initBackgroundGeometry();
|
|
||||||
|
|
||||||
|
GLWidget glw;
|
||||||
|
glw.initBgProgram("bg.vert", "bg.frag");
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
|
||||||
|
glw.drawBg();
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|||||||
Reference in New Issue
Block a user