update
This commit is contained in:
2
camera.h
2
camera.h
@@ -23,7 +23,7 @@ public:
|
||||
: front_(glm::vec3(0.0f, 0.0f, -1.0f)), yaw_(yaw), pitch_(pitch), position_(position)
|
||||
, worldUp_(up){
|
||||
zoom_ = 45.0f;
|
||||
mouseSensitivity_ = 0.1f;
|
||||
mouseSensitivity_ = 0.08f;
|
||||
moveSpeed_ = 2.5f;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
136
main.cpp
136
main.cpp
@@ -1,7 +1,5 @@
|
||||
#include "lopenglprogram.h"
|
||||
#include "camera.h"
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <functional>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
@@ -10,6 +8,7 @@
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
|
||||
@@ -33,6 +32,15 @@ public:
|
||||
if (window_) {
|
||||
glfwMakeContextCurrent(window_.get());
|
||||
}
|
||||
if (panelIbo_) {
|
||||
glDeleteBuffers(1, &panelIbo_);
|
||||
}
|
||||
if (panelVbo_) {
|
||||
glDeleteBuffers(1, &panelVbo_);
|
||||
}
|
||||
if (panelVao_) {
|
||||
glDeleteVertexArrays(1, &panelVao_);
|
||||
}
|
||||
if (bgVao_) {
|
||||
glDeleteVertexArrays(1, &bgVao_);
|
||||
}
|
||||
@@ -59,7 +67,7 @@ public:
|
||||
#if __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
window_.reset(glfwCreateWindow(800, 600, "3dviewer", nullptr, nullptr));
|
||||
window_.reset(glfwCreateWindow(viewport_.width, viewport_.height, "3dviewer", nullptr, nullptr));
|
||||
if (!window_) {
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
@@ -71,7 +79,9 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
glViewport(0, 0, 800, 600);
|
||||
glViewport(0, 0, viewport_.width, viewport_.height);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glfwSetInputMode(window_.get(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetWindowUserPointer(window_.get(), this);
|
||||
glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebufferSizeCallback);
|
||||
glfwSetCursorPosCallback(window_.get(), &GLWidget::mouseCallback);
|
||||
@@ -83,6 +93,11 @@ public:
|
||||
bgVertShaderPath_ = vp;
|
||||
bgFragShaderPath_ = fp;
|
||||
}
|
||||
|
||||
void setPanelShaderPath(std::string vp, std::string fp) {
|
||||
panelVertShaderPath_ = vp;
|
||||
panelFragShaderPath_ = fp;
|
||||
}
|
||||
private:
|
||||
void initBgGeometry() {
|
||||
if (bgVbo_) {
|
||||
@@ -121,6 +136,10 @@ private:
|
||||
glDeleteVertexArrays(1, &panelVao_);
|
||||
panelVao_ = 0;
|
||||
}
|
||||
if (panelIbo_) {
|
||||
glDeleteBuffers(1, &panelIbo_);
|
||||
panelIbo_ = 0;
|
||||
}
|
||||
|
||||
const float y = panelHeight_ * 0.5f;
|
||||
const float x = panelWidth_ * 0.5f;
|
||||
@@ -178,7 +197,22 @@ private:
|
||||
};
|
||||
|
||||
glGenVertexArrays(1, &panelVao_);
|
||||
|
||||
glBindVertexArray(panelVao_);
|
||||
|
||||
glGenBuffers(1, &panelVbo_);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, panelVbo_);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &panelIbo_);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, panelIbo_);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(V), (void*)0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(V), (void*)(3 * sizeof(float)));
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
bool initBgProgram() {
|
||||
@@ -212,6 +246,35 @@ private:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool initPanelProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
if (!vshader->compileShaderFromFile(panelVertShaderPath_)) {
|
||||
std::cout << "Vertex shader compile failed: " << panelVertShaderPath_ << "\n" << vshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!fshader->compileShaderFromFile(panelFragShaderPath_)) {
|
||||
std::cout << "Fragment shader compile failed: " << panelFragShaderPath_ << "\n" << fshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
panelProg_ = std::make_unique<LOpenGLProgram>();
|
||||
if (!panelProg_->addShader(std::move(vshader))) {
|
||||
std::cout << "Failed to attach vertex shader\n";
|
||||
return false;
|
||||
}
|
||||
if (!panelProg_->addShader(std::move(fshader))) {
|
||||
std::cout << "Failed to attach fragment shader\n";
|
||||
return false;
|
||||
}
|
||||
bool ret = panelProg_->Link();
|
||||
if (!ret) {
|
||||
std::cout << "Failed to link panel program\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void drawBg() {
|
||||
if (!bgProg_ || !bgVao_ || !bgVbo_) {
|
||||
std::cout << "check !bgProg_ || !bgVao_ || !bgVbo_ failed\n";
|
||||
@@ -221,14 +284,39 @@ private:
|
||||
bgProg_->setUniformValue("uViewport", glm::vec2(viewport_.width, viewport_.height));
|
||||
bgProg_->setUniformValue("uMajorStep", 120.0f);
|
||||
bgProg_->setUniformValue("uMinorStep", 24.0f);
|
||||
bgProg_->setUniformValue("uLightMode", lightMode_);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glBindVertexArray(bgVao_);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glDepthMask(GL_TRUE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
void drawPanel() {
|
||||
if (!panelProg_ || !panelVao_ || !panelVbo_ || !panelIbo_) {
|
||||
std:: cout << "check !panelProg_ || !panelVao || !panelVbo || !panelIbo\n";
|
||||
return;
|
||||
}
|
||||
auto [model, view, projection] = getMVP();
|
||||
panelProg_->Use();
|
||||
panelProg_->setUniformValue("model", model);
|
||||
panelProg_->setUniformValue("view", view);
|
||||
panelProg_->setUniformValue("projection", projection);
|
||||
panelProg_->setUniformValue("uLightMode", lightMode_);
|
||||
glBindVertexArray(panelVao_);
|
||||
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
std::tuple<glm::mat4, glm::mat4, glm::mat4> getMVP() {
|
||||
auto model = glm::mat4(1.0f);
|
||||
auto view = camera_.getViewMatrix();
|
||||
auto projection = glm::perspective(glm::radians(camera_.Zoom()),
|
||||
(float)viewport_.width / (float)viewport_.height, 0.1f, 100.0f);
|
||||
return {model, view, projection};
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void eventLoop() {
|
||||
@@ -239,11 +327,19 @@ public:
|
||||
if (!initBgProgram()) {
|
||||
return;
|
||||
}
|
||||
initPanelGeometry_();
|
||||
if (!initPanelProgram()) {
|
||||
return;
|
||||
}
|
||||
while (!glfwWindowShouldClose(window_.get())) {
|
||||
processInput(window_.get());
|
||||
const float now = static_cast<float>(glfwGetTime());
|
||||
const float deltaTime = now - lastFrame_;
|
||||
lastFrame_ = now;
|
||||
processInput(window_.get(), deltaTime);
|
||||
glClearColor(0.08f, 0.08f, 0.10f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
drawBg();
|
||||
drawPanel();
|
||||
glfwSwapBuffers(window_.get());
|
||||
glfwPollEvents();
|
||||
}
|
||||
@@ -263,10 +359,23 @@ private:
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window) {
|
||||
void processInput(GLFWwindow* window, float deltaTime) {
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
camera_.keyboardCallback(Camera::FORWARD, deltaTime);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
camera_.keyboardCallback(Camera::BACKWARD, deltaTime);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
camera_.keyboardCallback(Camera::LEFT, deltaTime);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
camera_.keyboardCallback(Camera::RIGHT, deltaTime);
|
||||
}
|
||||
lightMode_ = glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS;
|
||||
}
|
||||
|
||||
static void mouseCallback(GLFWwindow* window, double x, double y) {
|
||||
@@ -282,7 +391,6 @@ private:
|
||||
float ypos = static_cast<float>(y);
|
||||
|
||||
if (camera_.firstMouse()) {
|
||||
std::cout << "in firstMouse()\n";
|
||||
lastX_ = x;
|
||||
lastY_ = y;
|
||||
camera_.triggleFirstMouse();
|
||||
@@ -298,10 +406,8 @@ 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);
|
||||
@@ -324,25 +430,29 @@ private:
|
||||
std::string panelFragShaderPath_;
|
||||
unsigned int panelVao_ = 0;
|
||||
unsigned int panelVbo_ = 0;
|
||||
unsigned int panelIbo_ = 0;
|
||||
float panelWidth_ = 0.25;
|
||||
float panelHeight_ = 0.35;
|
||||
float panelDepth_ = 0.05;
|
||||
|
||||
|
||||
ViewPort viewport_{800, 600};
|
||||
Camera camera_;
|
||||
Camera camera_{glm::vec3(0.0f, 0.0f, 2.0f)};
|
||||
|
||||
bool firstMouse_ = true;
|
||||
float lastX_;
|
||||
float lastY_;
|
||||
float lastFrame_ = 0.0f;
|
||||
bool lightMode_ = false;
|
||||
|
||||
bool deinit_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
{
|
||||
GLWidget glw({600, 800});
|
||||
GLWidget glw({800, 600});
|
||||
glw.setBgShaderPath("../shader/bg.vert", "../shader/bg.frag");
|
||||
glw.setPanelShaderPath("../shader/panel.vert", "../shader/panel.frag");
|
||||
glw.eventLoop();
|
||||
}
|
||||
glfwTerminate();
|
||||
|
||||
27
shader/heatmap.frag
Normal file
27
shader/heatmap.frag
Normal file
@@ -0,0 +1,27 @@
|
||||
#version 330 core
|
||||
in vec2 vUV;
|
||||
in vec3 vWorldPos;
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform sampler2D uHeightTex;
|
||||
uniform float uMinV;
|
||||
uniform float uMaxV;
|
||||
uniform float uHeightScale;
|
||||
uniform vec2 uTexelSize;
|
||||
uniform vec2 uPlaneSize;
|
||||
uniform vec3 uCameraPos;
|
||||
uniform vec3 uLightDir;
|
||||
uniform vec3 uColorZero;
|
||||
uniform vec3 uColorLow;
|
||||
uniform vec3 uColorMid;
|
||||
uniform vec3 uColorHigh;
|
||||
|
||||
float saturate(float x) {
|
||||
return clamp(x, 0.0, 1.0);
|
||||
}
|
||||
|
||||
float value01(float v) {
|
||||
return saturate((v - uMinV) / max(uMaxV - uMinV, 1e-6));
|
||||
}
|
||||
|
||||
float maxNeighborValue(vec2 uv)
|
||||
28
shader/heatmap.vert
Normal file
28
shader/heatmap.vert
Normal file
@@ -0,0 +1,28 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec2 aUV;
|
||||
|
||||
out vec2 vUV;
|
||||
out vec3 vWorldPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform sampler2D uHeightTex;
|
||||
uniform float uMinV;
|
||||
uniform float uMaxV;
|
||||
uniform float uHeightScale;
|
||||
uniform float uBaseZ;
|
||||
|
||||
float value01(float v) {
|
||||
return clamp((v - uMinV) / max(uMaxV - uMinV, 1e-6), 0.0, 1.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vUV = aUV;
|
||||
float v = texture(uHeightTex, aUV).r;
|
||||
float h = value01(v) * uHeightScale;
|
||||
vec3 world = aPos + vec3(0.0, 0.0, uBaseZ + h);
|
||||
vWorldPos = world;
|
||||
gl_Position = projection * view * model * vec4(world, 1.0);
|
||||
}
|
||||
29
shader/panel.frag
Normal file
29
shader/panel.frag
Normal file
@@ -0,0 +1,29 @@
|
||||
#version 330 core
|
||||
|
||||
in vec3 vWorldPos;
|
||||
in vec3 vWorldNormal;
|
||||
|
||||
out vec4 FragColor;
|
||||
uniform bool uLightMode;
|
||||
|
||||
void main() {
|
||||
vec3 N = normalize(vWorldNormal);
|
||||
float isTop = step(0.75, N.y);
|
||||
|
||||
vec3 topBase;
|
||||
vec3 sideBase;
|
||||
|
||||
if (uLightMode) {
|
||||
// 浅色主题 偏亮的工业灰
|
||||
topBase = vec3(0.78, 0.80, 0.84);
|
||||
sideBase = vec3(0.68, 0.70, 0.74);
|
||||
}
|
||||
else {
|
||||
// 深色主题 偏暗的工业灰
|
||||
topBase = vec3(0.30, 0.31, 0.32);
|
||||
sideBase = vec3(0.27, 0.28, 0.29);
|
||||
}
|
||||
|
||||
vec3 baseColor = mix(sideBase, topBase, isTop);
|
||||
FragColor = vec4(clamp(baseColor, 0.0, 1.0), 1.0);
|
||||
}
|
||||
19
shader/panel.vert
Normal file
19
shader/panel.vert
Normal file
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec3 aN;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
out vec3 vWorldPos;
|
||||
out vec3 vWorldNormal;
|
||||
|
||||
void main() {
|
||||
vec4 worldPos = model * vec4(aPos, 1.0);
|
||||
mat3 normalMat = transpose(inverse(mat3(model)));
|
||||
vWorldPos = worldPos.xyz;
|
||||
vWorldNormal = normalize(normalMat * aN);
|
||||
gl_Position = projection * view * worldPos;
|
||||
}
|
||||
Reference in New Issue
Block a user