update
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
.cache/
|
||||
.idea/
|
||||
build/
|
||||
.cache/
|
||||
.idea/
|
||||
build/
|
||||
cmake-build*/
|
||||
@@ -1,31 +1,46 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(3dviewer)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
include_directories(.)
|
||||
|
||||
find_package(glfw3 REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
add_executable(
|
||||
${PROJECT_NAME}
|
||||
main.cpp
|
||||
glad.c
|
||||
lopenglprogram.cpp
|
||||
camera.h
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
glfw3
|
||||
OpenGL::GL
|
||||
X11
|
||||
Xrandr
|
||||
Xi
|
||||
Xcursor
|
||||
Xinerama
|
||||
pthread
|
||||
dl
|
||||
m
|
||||
)
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(3dviewer)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
|
||||
|
||||
include_directories(.)
|
||||
find_package(glfw3 REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
|
||||
add_executable(
|
||||
${PROJECT_NAME}
|
||||
main.cpp
|
||||
glad.c
|
||||
lopenglprogram.cpp
|
||||
camera.h
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
glfw3
|
||||
)
|
||||
elseif(APPLE)
|
||||
|
||||
elseif(UNIX)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
glfw3
|
||||
OpenGL::GL
|
||||
X11
|
||||
Xrandr
|
||||
Xi
|
||||
Xcursor
|
||||
Xinerama
|
||||
pthread
|
||||
dl
|
||||
m
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
|
||||
228
camera.h
228
camera.h
@@ -1,115 +1,115 @@
|
||||
//
|
||||
// Created by Lenn on 2026/1/30.
|
||||
//
|
||||
|
||||
#ifndef INC_3DVIEWER_CAMERA_H
|
||||
#define INC_3DVIEWER_CAMERA_H
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
typedef enum CameraMoveBit {
|
||||
FORWARD = 0x0001,
|
||||
BACKWARD = 0x0002,
|
||||
LEFT = 0x0004,
|
||||
RIGHT = 0x0008,
|
||||
} CameraMove;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
float yaw = -90.0f, float pitch = 0.0f)
|
||||
: front_(glm::vec3(0.0f, 0.0f, -1.0f)), yaw_(yaw), pitch_(pitch), position_(position)
|
||||
, worldUp_(up){
|
||||
zoom_ = 45.0f;
|
||||
mouseSensitivity_ = 0.08f;
|
||||
moveSpeed_ = 2.5f;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void keyboardCallback(CameraMove direction, float deltaTime) {
|
||||
float velocity = moveSpeed_ * deltaTime;
|
||||
if (direction == CameraMove::FORWARD) {
|
||||
position_ += front_ * velocity;
|
||||
}
|
||||
else if (direction == CameraMove::BACKWARD) {
|
||||
position_ -= front_ * velocity;
|
||||
}
|
||||
else if (direction == CameraMove::LEFT) {
|
||||
position_ -= right_ * velocity;
|
||||
}
|
||||
else {
|
||||
position_ += right_ * velocity;
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void mouseMoveCallback(float xoffset, float yoffset, GLboolean constrainPitch = true) {
|
||||
xoffset *= mouseSensitivity_;
|
||||
yoffset *= mouseSensitivity_;
|
||||
|
||||
yaw_ += xoffset;
|
||||
pitch_ += yoffset;
|
||||
|
||||
if (constrainPitch) {
|
||||
if (pitch_ > 89.0f) {
|
||||
pitch_ = 89.0f;
|
||||
}
|
||||
if (pitch_ < -89.0f) {
|
||||
pitch_ = -89.0f;
|
||||
}
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void mouseScrollCallback(float yoffset) {
|
||||
zoom_ -= yoffset;
|
||||
if (zoom_ < 1.0f) {
|
||||
zoom_ = 1.0;
|
||||
}
|
||||
if (zoom_ > 45.0f) {
|
||||
zoom_ = 45.0;
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
glm::mat4 getViewMatrix() {
|
||||
return glm::lookAt(position_, position_ + front_, up_);
|
||||
}
|
||||
|
||||
bool firstMouse() const { return firstMouse_; }
|
||||
|
||||
void triggleFirstMouse() { firstMouse_ = !firstMouse_; }
|
||||
|
||||
float Zoom() const { return zoom_; }
|
||||
|
||||
private:
|
||||
void updateCameraVectors() {
|
||||
glm::vec3 front;
|
||||
front.x = std::cos(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||
front.y = std::sin(glm::radians(pitch_));
|
||||
front.z = std::sin(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||
front_ = glm::normalize(front);
|
||||
|
||||
right_ = glm::normalize(glm::cross(front_, worldUp_));
|
||||
up_ = glm::normalize(glm::cross(right_, front_));
|
||||
}
|
||||
private:
|
||||
glm::vec3 position_;
|
||||
glm::vec3 up_;
|
||||
glm::vec3 right_;
|
||||
glm::vec3 front_;
|
||||
glm::vec3 worldUp_;
|
||||
|
||||
float yaw_;
|
||||
float pitch_;
|
||||
|
||||
float moveSpeed_;
|
||||
float mouseSensitivity_;
|
||||
float zoom_;
|
||||
|
||||
bool firstMouse_ = true;
|
||||
};
|
||||
|
||||
//
|
||||
// Created by Lenn on 2026/1/30.
|
||||
//
|
||||
|
||||
#ifndef INC_3DVIEWER_CAMERA_H
|
||||
#define INC_3DVIEWER_CAMERA_H
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
typedef enum CameraMoveBit {
|
||||
FORWARD = 0x0001,
|
||||
BACKWARD = 0x0002,
|
||||
LEFT = 0x0004,
|
||||
RIGHT = 0x0008,
|
||||
} CameraMove;
|
||||
|
||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
float yaw = -90.0f, float pitch = 0.0f)
|
||||
: front_(glm::vec3(0.0f, 0.0f, -1.0f)), yaw_(yaw), pitch_(pitch), position_(position)
|
||||
, worldUp_(up){
|
||||
zoom_ = 45.0f;
|
||||
mouseSensitivity_ = 0.08f;
|
||||
moveSpeed_ = 2.5f;
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void keyboardCallback(CameraMove direction, float deltaTime) {
|
||||
float velocity = moveSpeed_ * deltaTime;
|
||||
if (direction == CameraMove::FORWARD) {
|
||||
position_ += front_ * velocity;
|
||||
}
|
||||
else if (direction == CameraMove::BACKWARD) {
|
||||
position_ -= front_ * velocity;
|
||||
}
|
||||
else if (direction == CameraMove::LEFT) {
|
||||
position_ -= right_ * velocity;
|
||||
}
|
||||
else {
|
||||
position_ += right_ * velocity;
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void mouseMoveCallback(float xoffset, float yoffset, GLboolean constrainPitch = true) {
|
||||
xoffset *= mouseSensitivity_;
|
||||
yoffset *= mouseSensitivity_;
|
||||
|
||||
yaw_ += xoffset;
|
||||
pitch_ += yoffset;
|
||||
|
||||
if (constrainPitch) {
|
||||
if (pitch_ > 89.0f) {
|
||||
pitch_ = 89.0f;
|
||||
}
|
||||
if (pitch_ < -89.0f) {
|
||||
pitch_ = -89.0f;
|
||||
}
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void mouseScrollCallback(float yoffset) {
|
||||
zoom_ -= yoffset;
|
||||
if (zoom_ < 1.0f) {
|
||||
zoom_ = 1.0;
|
||||
}
|
||||
if (zoom_ > 45.0f) {
|
||||
zoom_ = 45.0;
|
||||
}
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
glm::mat4 getViewMatrix() {
|
||||
return glm::lookAt(position_, position_ + front_, up_);
|
||||
}
|
||||
|
||||
bool firstMouse() const { return firstMouse_; }
|
||||
|
||||
void triggleFirstMouse() { firstMouse_ = !firstMouse_; }
|
||||
|
||||
float Zoom() const { return zoom_; }
|
||||
|
||||
private:
|
||||
void updateCameraVectors() {
|
||||
glm::vec3 front;
|
||||
front.x = std::cos(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||
front.y = std::sin(glm::radians(pitch_));
|
||||
front.z = std::sin(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||
front_ = glm::normalize(front);
|
||||
|
||||
right_ = glm::normalize(glm::cross(front_, worldUp_));
|
||||
up_ = glm::normalize(glm::cross(right_, front_));
|
||||
}
|
||||
private:
|
||||
glm::vec3 position_;
|
||||
glm::vec3 up_;
|
||||
glm::vec3 right_;
|
||||
glm::vec3 front_;
|
||||
glm::vec3 worldUp_;
|
||||
|
||||
float yaw_;
|
||||
float pitch_;
|
||||
|
||||
float moveSpeed_;
|
||||
float mouseSensitivity_;
|
||||
float zoom_;
|
||||
|
||||
bool firstMouse_ = true;
|
||||
};
|
||||
|
||||
#endif //INC_3DVIEWER_CAMERA_H
|
||||
@@ -1,208 +1,208 @@
|
||||
#include "lopenglprogram.h"
|
||||
#include <iostream>
|
||||
|
||||
LOpenGLShader::LOpenGLShader(LOpenGLShader::ShaderType type)
|
||||
: shaderType_(type), compiled_(false) {}
|
||||
|
||||
LOpenGLShader::~LOpenGLShader() {
|
||||
if (shaderId_ != 0) {
|
||||
glDeleteShader(shaderId_);
|
||||
}
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromSource(const char *source) {
|
||||
if (!source || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
glShaderSource(shaderId_, 1, &source, NULL);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromSource(const std::string &source) {
|
||||
if (source.empty() || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
auto source_ch = source.c_str();
|
||||
glShaderSource(shaderId_, 1, &source_ch, nullptr);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, nullptr, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromFile(const std::string &path) {
|
||||
std::string shaderCode;
|
||||
std::fstream shaderFile;
|
||||
std::stringstream shaderStream;
|
||||
|
||||
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
shaderFile.open(path);
|
||||
shaderStream << shaderFile.rdbuf();
|
||||
shaderFile.close();
|
||||
shaderCode = shaderStream.str();
|
||||
} catch (std::ifstream::failure e) {
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
|
||||
}
|
||||
|
||||
if (shaderCode.empty() || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
auto source_ch = shaderCode.c_str();
|
||||
glShaderSource(shaderId_, 1, &source_ch, NULL);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
LOpenGLProgram::LOpenGLProgram() { Create(); }
|
||||
|
||||
LOpenGLProgram::~LOpenGLProgram() {
|
||||
if (programId_ != 0) {
|
||||
glDeleteProgram(programId_);
|
||||
}
|
||||
}
|
||||
|
||||
void LOpenGLProgram::Create() {
|
||||
programId_ = glCreateProgram();
|
||||
created_ = (programId_ != 0);
|
||||
linked_ = false;
|
||||
}
|
||||
|
||||
bool LOpenGLProgram::Link() {
|
||||
if (!created_ || linked_ || shaderList_.empty()) {
|
||||
return false;
|
||||
}
|
||||
glLinkProgram(programId_);
|
||||
GLint success = 0;
|
||||
glGetProgramiv(programId_, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetProgramInfoLog(programId_, 512, nullptr, infoLog);
|
||||
std::cout << "ERROR::PROGRAM::LINK_FAILED\n" << infoLog << std::endl;
|
||||
linked_ = false;
|
||||
return false;
|
||||
}
|
||||
linked_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void LOpenGLProgram::Use() const {
|
||||
if (!linked_)
|
||||
return;
|
||||
glUseProgram(programId_);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(programId_, name.c_str()), value);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), value);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), static_cast<int>(value));
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec3 value) const {
|
||||
glUniform3fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, float x, float y, float z) const {
|
||||
glUniform3f(glGetUniformLocation(programId_, name.c_str()), x, y, z);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::mat4 value) const {
|
||||
glUniformMatrix4fv(glGetUniformLocation(programId_, name.c_str()), 1, GL_FALSE, &value[0][0]);
|
||||
}
|
||||
|
||||
bool LOpenGLProgram::addShader(std::unique_ptr<LOpenGLShader> shader) {
|
||||
if (!shader || !shader->Compiled()) {
|
||||
return false;
|
||||
}
|
||||
if (!created_) {
|
||||
return false;
|
||||
}
|
||||
glAttachShader(programId_, shader->shaderId_);
|
||||
shaderList_.push_back(std::move(shader));
|
||||
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) {
|
||||
for (auto ite = shaderList_.begin(); ite != shaderList_.end(); ++ite) {
|
||||
if ((*ite)->shaderId_ == id) {
|
||||
glDetachShader(programId_, id);
|
||||
shaderList_.erase(ite);
|
||||
}
|
||||
}
|
||||
}
|
||||
#include "lopenglprogram.h"
|
||||
#include <iostream>
|
||||
|
||||
LOpenGLShader::LOpenGLShader(LOpenGLShader::ShaderType type)
|
||||
: shaderType_(type), compiled_(false) {}
|
||||
|
||||
LOpenGLShader::~LOpenGLShader() {
|
||||
if (shaderId_ != 0) {
|
||||
glDeleteShader(shaderId_);
|
||||
}
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromSource(const char *source) {
|
||||
if (!source || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
glShaderSource(shaderId_, 1, &source, NULL);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromSource(const std::string &source) {
|
||||
if (source.empty() || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
auto source_ch = source.c_str();
|
||||
glShaderSource(shaderId_, 1, &source_ch, nullptr);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, nullptr, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LOpenGLShader::compileShaderFromFile(const std::string &path) {
|
||||
std::string shaderCode;
|
||||
std::fstream shaderFile;
|
||||
std::stringstream shaderStream;
|
||||
|
||||
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
shaderFile.open(path);
|
||||
shaderStream << shaderFile.rdbuf();
|
||||
shaderFile.close();
|
||||
shaderCode = shaderStream.str();
|
||||
} catch (std::ifstream::failure e) {
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
|
||||
}
|
||||
|
||||
if (shaderCode.empty() || compiled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (shaderType_) {
|
||||
case Vertex:
|
||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||
break;
|
||||
case Fragment:
|
||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
auto source_ch = shaderCode.c_str();
|
||||
glShaderSource(shaderId_, 1, &source_ch, NULL);
|
||||
glCompileShader(shaderId_);
|
||||
int success = 0;
|
||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||
shaderLog_ = std::string(infoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
compiled_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
LOpenGLProgram::LOpenGLProgram() { Create(); }
|
||||
|
||||
LOpenGLProgram::~LOpenGLProgram() {
|
||||
if (programId_ != 0) {
|
||||
glDeleteProgram(programId_);
|
||||
}
|
||||
}
|
||||
|
||||
void LOpenGLProgram::Create() {
|
||||
programId_ = glCreateProgram();
|
||||
created_ = (programId_ != 0);
|
||||
linked_ = false;
|
||||
}
|
||||
|
||||
bool LOpenGLProgram::Link() {
|
||||
if (!created_ || linked_ || shaderList_.empty()) {
|
||||
return false;
|
||||
}
|
||||
glLinkProgram(programId_);
|
||||
GLint success = 0;
|
||||
glGetProgramiv(programId_, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
char infoLog[512];
|
||||
glGetProgramInfoLog(programId_, 512, nullptr, infoLog);
|
||||
std::cout << "ERROR::PROGRAM::LINK_FAILED\n" << infoLog << std::endl;
|
||||
linked_ = false;
|
||||
return false;
|
||||
}
|
||||
linked_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void LOpenGLProgram::Use() const {
|
||||
if (!linked_)
|
||||
return;
|
||||
glUseProgram(programId_);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(programId_, name.c_str()), value);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), value);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), static_cast<int>(value));
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec3 value) const {
|
||||
glUniform3fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, float x, float y, float z) const {
|
||||
glUniform3f(glGetUniformLocation(programId_, name.c_str()), x, y, z);
|
||||
}
|
||||
|
||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::mat4 value) const {
|
||||
glUniformMatrix4fv(glGetUniformLocation(programId_, name.c_str()), 1, GL_FALSE, &value[0][0]);
|
||||
}
|
||||
|
||||
bool LOpenGLProgram::addShader(std::unique_ptr<LOpenGLShader> shader) {
|
||||
if (!shader || !shader->Compiled()) {
|
||||
return false;
|
||||
}
|
||||
if (!created_) {
|
||||
return false;
|
||||
}
|
||||
glAttachShader(programId_, shader->shaderId_);
|
||||
shaderList_.push_back(std::move(shader));
|
||||
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) {
|
||||
for (auto ite = shaderList_.begin(); ite != shaderList_.end(); ++ite) {
|
||||
if ((*ite)->shaderId_ == id) {
|
||||
glDetachShader(programId_, id);
|
||||
shaderList_.erase(ite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
122
lopenglprogram.h
122
lopenglprogram.h
@@ -1,61 +1,61 @@
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
class LOpenGLProgram;
|
||||
class LOpenGLShader {
|
||||
public:
|
||||
typedef enum ShaderTypeBit {
|
||||
Vertex = 0x0001,
|
||||
Fragment = 0x0002,
|
||||
} ShaderType;
|
||||
LOpenGLShader() = delete;
|
||||
LOpenGLShader(LOpenGLShader::ShaderType type);
|
||||
~LOpenGLShader();
|
||||
bool compileShaderFromSource(const char *source);
|
||||
bool compileShaderFromSource(const std::string &source);
|
||||
bool compileShaderFromFile(const std::string &path);
|
||||
bool Compiled() const { return compiled_; }
|
||||
std::string Log() const { return shaderLog_; }
|
||||
|
||||
private:
|
||||
GLuint shaderId_{};
|
||||
std::string shaderSource_;
|
||||
LOpenGLShader::ShaderType shaderType_;
|
||||
bool compiled_;
|
||||
std::string shaderLog_;
|
||||
|
||||
friend class LOpenGLProgram;
|
||||
};
|
||||
|
||||
class LOpenGLProgram {
|
||||
public:
|
||||
LOpenGLProgram();
|
||||
~LOpenGLProgram();
|
||||
|
||||
bool addShader(std::unique_ptr<LOpenGLShader> shader);
|
||||
void removeShader(GLuint id);
|
||||
void Create();
|
||||
bool Link();
|
||||
void Use() const;
|
||||
bool Linked() const { return linked_; }
|
||||
|
||||
void setUniformValue(const std::string& name, float value) const;
|
||||
void setUniformValue(const std::string& name, int value) const;
|
||||
void setUniformValue(const std::string& name, bool 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, glm::mat4 value) const;
|
||||
void setUniformValue(const std::string& name, glm::vec2 value) const;
|
||||
|
||||
private:
|
||||
std::list<std::unique_ptr<LOpenGLShader>> shaderList_;
|
||||
GLuint programId_ = 0;
|
||||
bool linked_ = false;
|
||||
bool created_ = false;
|
||||
};
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
class LOpenGLProgram;
|
||||
class LOpenGLShader {
|
||||
public:
|
||||
typedef enum ShaderTypeBit {
|
||||
Vertex = 0x0001,
|
||||
Fragment = 0x0002,
|
||||
} ShaderType;
|
||||
LOpenGLShader() = delete;
|
||||
LOpenGLShader(LOpenGLShader::ShaderType type);
|
||||
~LOpenGLShader();
|
||||
bool compileShaderFromSource(const char *source);
|
||||
bool compileShaderFromSource(const std::string &source);
|
||||
bool compileShaderFromFile(const std::string &path);
|
||||
bool Compiled() const { return compiled_; }
|
||||
std::string Log() const { return shaderLog_; }
|
||||
|
||||
private:
|
||||
GLuint shaderId_{};
|
||||
std::string shaderSource_;
|
||||
LOpenGLShader::ShaderType shaderType_;
|
||||
bool compiled_;
|
||||
std::string shaderLog_;
|
||||
|
||||
friend class LOpenGLProgram;
|
||||
};
|
||||
|
||||
class LOpenGLProgram {
|
||||
public:
|
||||
LOpenGLProgram();
|
||||
~LOpenGLProgram();
|
||||
|
||||
bool addShader(std::unique_ptr<LOpenGLShader> shader);
|
||||
void removeShader(GLuint id);
|
||||
void Create();
|
||||
bool Link();
|
||||
void Use() const;
|
||||
bool Linked() const { return linked_; }
|
||||
|
||||
void setUniformValue(const std::string& name, float value) const;
|
||||
void setUniformValue(const std::string& name, int value) const;
|
||||
void setUniformValue(const std::string& name, bool 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, glm::mat4 value) const;
|
||||
void setUniformValue(const std::string& name, glm::vec2 value) const;
|
||||
|
||||
private:
|
||||
std::list<std::unique_ptr<LOpenGLShader>> shaderList_;
|
||||
GLuint programId_ = 0;
|
||||
bool linked_ = false;
|
||||
bool created_ = false;
|
||||
};
|
||||
|
||||
983
main.cpp
983
main.cpp
@@ -1,475 +1,508 @@
|
||||
#include "lopenglprogram.h"
|
||||
#include "camera.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 <tuple>
|
||||
#include <utility>
|
||||
|
||||
|
||||
class GLWidget {
|
||||
|
||||
public:
|
||||
struct ViewPort {
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
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 (!deinit_) {
|
||||
// OpenGL objects should be deleted while a valid context still exists.
|
||||
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_);
|
||||
}
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
}
|
||||
deinit_ = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setViewPort(int width, int height) {
|
||||
viewport_ = {width, height};
|
||||
}
|
||||
|
||||
bool initGeometry() {
|
||||
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);
|
||||
#if __APPLE__
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
window_.reset(glfwCreateWindow(viewport_.width, viewport_.height, "3dviewer", nullptr, nullptr));
|
||||
if (!window_) {
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return false;
|
||||
}
|
||||
glfwMakeContextCurrent(window_.get());
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
glfwSetScrollCallback(window_.get(), &GLWidget::scrollCallback);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setBgShaderPath(std::string vp, std::string fp) {
|
||||
bgVertShaderPath_ = vp;
|
||||
bgFragShaderPath_ = fp;
|
||||
}
|
||||
|
||||
void setPanelShaderPath(std::string vp, std::string fp) {
|
||||
panelVertShaderPath_ = vp;
|
||||
panelFragShaderPath_ = fp;
|
||||
}
|
||||
private:
|
||||
void initBgGeometry() {
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
bgVbo_ = 0;
|
||||
}
|
||||
if (bgVao_) {
|
||||
glDeleteVertexArrays(1, &bgVao_);
|
||||
bgVao_ = 0;
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
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 initPanelGeometry_() {
|
||||
if (panelVbo_) {
|
||||
glDeleteBuffers(1, &panelVbo_);
|
||||
panelVbo_ = 0;
|
||||
}
|
||||
if (panelVao_) {
|
||||
glDeleteVertexArrays(1, &panelVao_);
|
||||
panelVao_ = 0;
|
||||
}
|
||||
if (panelIbo_) {
|
||||
glDeleteBuffers(1, &panelIbo_);
|
||||
panelIbo_ = 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_);
|
||||
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);
|
||||
}
|
||||
|
||||
void initHeatmapGeometry_() {
|
||||
|
||||
}
|
||||
|
||||
bool initBgProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
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>();
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool initHeatmapProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
|
||||
}
|
||||
|
||||
void drawBg() {
|
||||
if (!bgProg_ || !bgVao_ || !bgVbo_) {
|
||||
std::cout << "check !bgProg_ || !bgVao_ || !bgVbo_ failed\n";
|
||||
return;
|
||||
}
|
||||
bgProg_->Use();
|
||||
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);
|
||||
|
||||
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() {
|
||||
if (!window_) {
|
||||
return;
|
||||
}
|
||||
initBgGeometry();
|
||||
if (!initBgProgram()) {
|
||||
return;
|
||||
}
|
||||
initPanelGeometry_();
|
||||
if (!initPanelProgram()) {
|
||||
return;
|
||||
}
|
||||
while (!glfwWindowShouldClose(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();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onFramebufferSize(width, height);
|
||||
}
|
||||
|
||||
void onFramebufferSize(int width, int height) {
|
||||
viewport_ = {width, height};
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
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) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onMouseMove(x, y);
|
||||
}
|
||||
|
||||
void onMouseMove(double x, double y) {
|
||||
float xpos = static_cast<float>(x);
|
||||
float ypos = static_cast<float>(y);
|
||||
|
||||
if (camera_.firstMouse()) {
|
||||
lastX_ = x;
|
||||
lastY_ = y;
|
||||
camera_.triggleFirstMouse();
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX_;
|
||||
float yoffset = lastY_ - ypos;
|
||||
|
||||
lastX_ = xpos;
|
||||
lastY_ = ypos;
|
||||
|
||||
camera_.mouseMoveCallback(xoffset, yoffset);
|
||||
}
|
||||
|
||||
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onScrollRoll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
void onScrollRoll(double xoffset, double yoffset) {
|
||||
camera_.mouseScrollCallback(yoffset);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<LOpenGLProgram> bgProg_;
|
||||
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
|
||||
std::string bgFragShaderPath_;
|
||||
std::string bgVertShaderPath_;
|
||||
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;
|
||||
unsigned int panelIbo_ = 0;
|
||||
float panelWidth_ = 0.25;
|
||||
float panelHeight_ = 0.35;
|
||||
float panelDepth_ = 0.05;
|
||||
|
||||
std::unique_ptr<LOpenGLProgram> heatmapProg_;
|
||||
std::string heatmapVertShaderPath_;
|
||||
std::string heatmapFragShaderPath_;
|
||||
unsigned int heatmapVao_ = 0;
|
||||
unsigned int heatmapVbo_ = 0;
|
||||
|
||||
ViewPort viewport_{800, 600};
|
||||
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({800, 600});
|
||||
glw.setBgShaderPath("../shader/bg.vert", "../shader/bg.frag");
|
||||
glw.setPanelShaderPath("../shader/panel.vert", "../shader/panel.frag");
|
||||
glw.eventLoop();
|
||||
}
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
#include "lopenglprogram.h"
|
||||
#include "camera.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 <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <print>
|
||||
|
||||
|
||||
class GLWidget {
|
||||
|
||||
public:
|
||||
struct ViewPort {
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
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 (!deinit_) {
|
||||
// OpenGL objects should be deleted while a valid context still exists.
|
||||
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_);
|
||||
}
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
}
|
||||
if (heatmapVao_) {
|
||||
glDeleteVertexArrays(1, &heatmapVao_);
|
||||
}
|
||||
if (heatmapVbo_) {
|
||||
glDeleteBuffers(1, &heatmapVbo_);
|
||||
}
|
||||
deinit_ = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void setViewPort(int width, int height) {
|
||||
viewport_ = {width, height};
|
||||
}
|
||||
|
||||
bool initGeometry() {
|
||||
if (!glfwInit()) {
|
||||
std::cout << "Failed to initialize GLFW\n";
|
||||
return false;
|
||||
}
|
||||
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
|
||||
window_.reset(glfwCreateWindow(viewport_.width, viewport_.height, "3dviewer", nullptr, nullptr));
|
||||
if (!window_) {
|
||||
std::cout << "Failed to create GLFW window\n";
|
||||
glfwTerminate();
|
||||
return false;
|
||||
}
|
||||
glfwMakeContextCurrent(window_.get());
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
glfwSetScrollCallback(window_.get(), &GLWidget::scrollCallback);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setBgShaderPath(std::string vp, std::string fp) {
|
||||
bgVertShaderPath_ = vp;
|
||||
bgFragShaderPath_ = fp;
|
||||
}
|
||||
|
||||
void setPanelShaderPath(std::string vp, std::string fp) {
|
||||
panelVertShaderPath_ = vp;
|
||||
panelFragShaderPath_ = fp;
|
||||
}
|
||||
private:
|
||||
void initBgGeometry() {
|
||||
if (bgVbo_) {
|
||||
glDeleteBuffers(1, &bgVbo_);
|
||||
bgVbo_ = 0;
|
||||
}
|
||||
if (bgVao_) {
|
||||
glDeleteVertexArrays(1, &bgVao_);
|
||||
bgVao_ = 0;
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
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 initPanelGeometry_() {
|
||||
if (panelVbo_) {
|
||||
glDeleteBuffers(1, &panelVbo_);
|
||||
panelVbo_ = 0;
|
||||
}
|
||||
if (panelVao_) {
|
||||
glDeleteVertexArrays(1, &panelVao_);
|
||||
panelVao_ = 0;
|
||||
}
|
||||
if (panelIbo_) {
|
||||
glDeleteBuffers(1, &panelIbo_);
|
||||
panelIbo_ = 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_);
|
||||
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);
|
||||
}
|
||||
|
||||
void initHeatmapGeometry_() {
|
||||
|
||||
}
|
||||
|
||||
bool initBgProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
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>();
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool initHeatmapProgram() {
|
||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||
if (!vshader->compileShaderFromFile(heatmapVertShaderPath_)) {
|
||||
std::cout << "Vertex shader compile failed: " << heatmapVertShaderPath_ << "\n" << vshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!fshader->compileShaderFromFile(heatmapFragShaderPath_)) {
|
||||
std::cout << "Fragment shader compile failed: " << heatmapFragShaderPath_ << "\n" << fshader->Log() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
heatmapProg_ = std::make_unique<LOpenGLProgram>();
|
||||
if (!heatmapProg_->addShader(std::move(vshader))) {
|
||||
std::cout << "Failed to attach vertex shader\n";
|
||||
return false;
|
||||
}
|
||||
if (!heatmapProg_->addShader(std::move(fshader))) {
|
||||
std::cout << "Failed to attach vertex shader\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = panelProg_->Link();
|
||||
if (!ret) {
|
||||
std::cout << "Failed to link heatmap program\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void drawBg() {
|
||||
if (!bgProg_ || !bgVao_ || !bgVbo_) {
|
||||
std::cout << "check !bgProg_ || !bgVao_ || !bgVbo_ failed\n";
|
||||
return;
|
||||
}
|
||||
bgProg_->Use();
|
||||
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);
|
||||
|
||||
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() {
|
||||
if (!window_) {
|
||||
return;
|
||||
}
|
||||
initBgGeometry();
|
||||
if (!initBgProgram()) {
|
||||
return;
|
||||
}
|
||||
initPanelGeometry_();
|
||||
if (!initPanelProgram()) {
|
||||
return;
|
||||
}
|
||||
while (!glfwWindowShouldClose(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();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onFramebufferSize(width, height);
|
||||
}
|
||||
|
||||
void onFramebufferSize(int width, int height) {
|
||||
viewport_ = {width, height};
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
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) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onMouseMove(x, y);
|
||||
}
|
||||
|
||||
void onMouseMove(double x, double y) {
|
||||
float xpos = static_cast<float>(x);
|
||||
float ypos = static_cast<float>(y);
|
||||
|
||||
if (camera_.firstMouse()) {
|
||||
lastX_ = x;
|
||||
lastY_ = y;
|
||||
camera_.triggleFirstMouse();
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX_;
|
||||
float yoffset = lastY_ - ypos;
|
||||
|
||||
lastX_ = xpos;
|
||||
lastY_ = ypos;
|
||||
|
||||
camera_.mouseMoveCallback(xoffset, yoffset);
|
||||
}
|
||||
|
||||
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
self->onScrollRoll(xoffset, yoffset);
|
||||
}
|
||||
|
||||
void onScrollRoll(double xoffset, double yoffset) {
|
||||
camera_.mouseScrollCallback(yoffset);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<LOpenGLProgram> bgProg_;
|
||||
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
|
||||
std::string bgFragShaderPath_;
|
||||
std::string bgVertShaderPath_;
|
||||
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;
|
||||
unsigned int panelIbo_ = 0;
|
||||
float panelWidth_ = 0.25;
|
||||
float panelHeight_ = 0.35;
|
||||
float panelDepth_ = 0.05;
|
||||
|
||||
std::unique_ptr<LOpenGLProgram> heatmapProg_;
|
||||
std::string heatmapVertShaderPath_;
|
||||
std::string heatmapFragShaderPath_;
|
||||
unsigned int heatmapVao_ = 0;
|
||||
unsigned int heatmapVbo_ = 0;
|
||||
|
||||
ViewPort viewport_{800, 600};
|
||||
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({800, 600});
|
||||
glw.setBgShaderPath("../shader/bg.vert", "../shader/bg.frag");
|
||||
glw.setPanelShaderPath("../shader/panel.vert", "../shader/panel.frag");
|
||||
glw.eventLoop();
|
||||
}
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec2 uViewport;
|
||||
uniform float uMajorStep;
|
||||
uniform float uMinorStep;
|
||||
uniform bool uLightMode;
|
||||
|
||||
float gridLine(float stepPx) {
|
||||
vec2 coord = gl_FragCoord.xy;
|
||||
vec2 q = coord / max(stepPx, 1.0);
|
||||
vec2 fw = max(fwidth(q), vec2(1e-6));
|
||||
q = abs(fract(q - 0.5) - 0.5) / fw;
|
||||
float line = 1.0 - min(min(q.x, q.y), 1.0);
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 viewport = max(uViewport, vec2(1.0));
|
||||
vec2 uv = gl_FragCoord.xy / viewport;
|
||||
|
||||
vec3 topCol, botCol;
|
||||
float majorStrength, minorStrength;
|
||||
|
||||
topCol = vec3(0.99, 0.99, 1.00);
|
||||
botCol = vec3(0.94, 0.95, 0.98);
|
||||
|
||||
vec3 minorCol = vec3(0.80, 0.82, 0.87);
|
||||
vec3 majorCol = vec3(0.70, 0.73, 0.80);
|
||||
|
||||
minorStrength = 0.28;
|
||||
majorStrength = 0.45;
|
||||
|
||||
vec3 col = mix(botCol, topCol, uv.y);
|
||||
float minor = gridLine(max(uMinorStep, 1.0));
|
||||
float major = gridLine(max(uMajorStep, 1.0));
|
||||
|
||||
col = mix(col, minorCol, minorStrength * minor);
|
||||
col = mix(col, majorCol, majorStrength * major);
|
||||
|
||||
vec2 p = uv * 2.0 - 1.0;
|
||||
float v = clamp(1.0 - dot(p, p) * 0.12 , 0.0, 1.0);
|
||||
col *= mix(1.0, v, 0.35);
|
||||
|
||||
FragColor = vec4(col, 1.0);
|
||||
}
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec2 uViewport;
|
||||
uniform float uMajorStep;
|
||||
uniform float uMinorStep;
|
||||
uniform bool uLightMode;
|
||||
|
||||
float gridLine(float stepPx) {
|
||||
vec2 coord = gl_FragCoord.xy;
|
||||
vec2 q = coord / max(stepPx, 1.0);
|
||||
vec2 fw = max(fwidth(q), vec2(1e-6));
|
||||
q = abs(fract(q - 0.5) - 0.5) / fw;
|
||||
float line = 1.0 - min(min(q.x, q.y), 1.0);
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 viewport = max(uViewport, vec2(1.0));
|
||||
vec2 uv = gl_FragCoord.xy / viewport;
|
||||
|
||||
vec3 topCol, botCol;
|
||||
float majorStrength, minorStrength;
|
||||
|
||||
topCol = vec3(0.99, 0.99, 1.00);
|
||||
botCol = vec3(0.94, 0.95, 0.98);
|
||||
|
||||
vec3 minorCol = vec3(0.80, 0.82, 0.87);
|
||||
vec3 majorCol = vec3(0.70, 0.73, 0.80);
|
||||
|
||||
minorStrength = 0.28;
|
||||
majorStrength = 0.45;
|
||||
|
||||
vec3 col = mix(botCol, topCol, uv.y);
|
||||
float minor = gridLine(max(uMinorStep, 1.0));
|
||||
float major = gridLine(max(uMajorStep, 1.0));
|
||||
|
||||
col = mix(col, minorCol, minorStrength * minor);
|
||||
col = mix(col, majorCol, majorStrength * major);
|
||||
|
||||
vec2 p = uv * 2.0 - 1.0;
|
||||
float v = clamp(1.0 - dot(p, p) * 0.12 , 0.0, 1.0);
|
||||
col *= mix(1.0, v, 0.35);
|
||||
|
||||
FragColor = vec4(col, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec2 aPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec2 aPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,149 +1,149 @@
|
||||
#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) {
|
||||
// vec2 texSizeF = 1.0 / uTexelSize;
|
||||
// ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
// ivec2 maxCoord = texSizeI - ivec2(1);
|
||||
|
||||
// vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||
// ivec2 base = ivec2(floor(texelF));
|
||||
// ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||
// ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||
// ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||
|
||||
// float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||
// float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||
// float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||
// float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||
|
||||
// return max(max(v00, v10), max(v01, v11));
|
||||
// }
|
||||
|
||||
float maxNeighborValue(vec2 uv) {
|
||||
vec2 texSizeF = 1.0 / uTexelSize;
|
||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
ivec2 maxCoord = texSizeI - ivec2(1);
|
||||
|
||||
vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||
ivec2 base = ivec2(floor(texelF));
|
||||
ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||
ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||
ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||
|
||||
float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||
float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||
float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||
float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||
|
||||
return max(max(v00, v10), max(v01, v11));
|
||||
}
|
||||
|
||||
ivec2 texSizeI(vec2 texelSize) {
|
||||
vec2 texSizeF = 1.0 / texelSize;
|
||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
return texSizeI;
|
||||
}
|
||||
|
||||
ivec2 uvToTexel(vec2 uv, vec2 texelSize) {
|
||||
ivec2 sizeI = texSizeI(texelSize);
|
||||
ivec2 maxCoord = sizeI - ivec2(1);
|
||||
vec2 texelF = clamp(uv * vec2(maxCoord), vec2(0.0), vec2(maxCoord));
|
||||
return ivec2(floor(texelF));
|
||||
}
|
||||
|
||||
float maxNeighborValueK(vec2 uv, int radius) {
|
||||
radius = clamp(radius, 0, 4);
|
||||
ivec2 sizeI = texSizeI(uTexelSize);
|
||||
ivec2 maxCoord = sizeI - ivec2(1);
|
||||
ivec2 p = uvToTexel(uv, uTexelSize);
|
||||
|
||||
float vmax = -1e30;
|
||||
|
||||
for (int i = -4; i <= 4; ++i) {
|
||||
for (int j = -4; j <= 4; ++j) {
|
||||
if (abs(i) > radius || abs(j) > radius) continue;
|
||||
ivec2 c = clamp(p + ivec2(i, j), ivec2(0), maxCoord);
|
||||
float v = texelFetch(uHeightTex, c, 0).r;
|
||||
vmax = max(vmax, v);
|
||||
}
|
||||
}
|
||||
|
||||
return vmax;
|
||||
}
|
||||
|
||||
vec3 colorRamp(float t) {
|
||||
if (t < 0.5) {
|
||||
return mix(uColorLow, uColorMid, t / 0.5);
|
||||
}
|
||||
return mix(uColorMid, uColorHigh, (t - 0.5) / 0.5);
|
||||
}
|
||||
|
||||
vec3 colorRampWithZero(float t) {
|
||||
const float zeroW = 0.005;
|
||||
float blend = smoothstep(0.0, zeroW, t);
|
||||
float tRamp = saturate((t - zeroW) / max(1.0 - zeroW, 1e-6));
|
||||
vec3 ramp = colorRamp(tRamp);
|
||||
return mix(uColorZero, ramp, blend);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float vC = texture(uHeightTex, vUV).r;
|
||||
bool isZero = abs(vC) <= 1e-6;
|
||||
float t = value01(vC);
|
||||
float m = maxNeighborValue(vUV);
|
||||
float eps = max(1e-4, 0.001 * (uMaxV - uMinV));
|
||||
float force = smoothstep(uMaxV - eps, uMaxV, m);
|
||||
t = max(t, force);
|
||||
vec3 base = isZero ? uColorZero : colorRampWithZero(t);
|
||||
|
||||
float vL = texture(uHeightTex, vUV - vec2(uTexelSize.x, 0.0)).r;
|
||||
float vR = texture(uHeightTex, vUV + vec2(uTexelSize.x, 0.0)).r;
|
||||
float vD = texture(uHeightTex, vUV - vec2(0.0, uTexelSize.y)).r;
|
||||
float vU = texture(uHeightTex, vUV + vec2(0.0, uTexelSize.y)).r;
|
||||
|
||||
float hL = value01(vL) * uHeightScale;
|
||||
float hR = value01(vR) * uHeightScale;
|
||||
float hD = value01(vD) * uHeightScale;
|
||||
float hU = value01(vU) * uHeightScale;
|
||||
|
||||
float dx = max(1e-6, uPlaneSize.x * uTexelSize.x);
|
||||
float dy = max(1e-6, uPlaneSize.y * uTexelSize.y);
|
||||
float dHx = (hR - hL) / (2.0 * dx);
|
||||
float dHy = (hU - hD) / (2.0 * dy);
|
||||
|
||||
vec3 N = normalize(vec3(-dHx, -dHy, -1.0));
|
||||
vec3 L = normalize(uLightDir);
|
||||
vec3 V = normalize(uCameraPos - vWorldPos);
|
||||
vec3 H = normalize(L + V);
|
||||
|
||||
float diff = saturate(dot(N, L));
|
||||
float spec = pow(saturate(dot(N, H)), 24.0);
|
||||
|
||||
vec3 col = base * (0.50 + 0.50 * diff);
|
||||
col += vec3(1.0) * spec * 0.18;
|
||||
FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
||||
#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) {
|
||||
// vec2 texSizeF = 1.0 / uTexelSize;
|
||||
// ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
// ivec2 maxCoord = texSizeI - ivec2(1);
|
||||
|
||||
// vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||
// ivec2 base = ivec2(floor(texelF));
|
||||
// ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||
// ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||
// ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||
|
||||
// float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||
// float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||
// float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||
// float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||
|
||||
// return max(max(v00, v10), max(v01, v11));
|
||||
// }
|
||||
|
||||
float maxNeighborValue(vec2 uv) {
|
||||
vec2 texSizeF = 1.0 / uTexelSize;
|
||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
ivec2 maxCoord = texSizeI - ivec2(1);
|
||||
|
||||
vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||
ivec2 base = ivec2(floor(texelF));
|
||||
ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||
ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||
ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||
|
||||
float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||
float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||
float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||
float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||
|
||||
return max(max(v00, v10), max(v01, v11));
|
||||
}
|
||||
|
||||
ivec2 texSizeI(vec2 texelSize) {
|
||||
vec2 texSizeF = 1.0 / texelSize;
|
||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||
return texSizeI;
|
||||
}
|
||||
|
||||
ivec2 uvToTexel(vec2 uv, vec2 texelSize) {
|
||||
ivec2 sizeI = texSizeI(texelSize);
|
||||
ivec2 maxCoord = sizeI - ivec2(1);
|
||||
vec2 texelF = clamp(uv * vec2(maxCoord), vec2(0.0), vec2(maxCoord));
|
||||
return ivec2(floor(texelF));
|
||||
}
|
||||
|
||||
float maxNeighborValueK(vec2 uv, int radius) {
|
||||
radius = clamp(radius, 0, 4);
|
||||
ivec2 sizeI = texSizeI(uTexelSize);
|
||||
ivec2 maxCoord = sizeI - ivec2(1);
|
||||
ivec2 p = uvToTexel(uv, uTexelSize);
|
||||
|
||||
float vmax = -1e30;
|
||||
|
||||
for (int i = -4; i <= 4; ++i) {
|
||||
for (int j = -4; j <= 4; ++j) {
|
||||
if (abs(i) > radius || abs(j) > radius) continue;
|
||||
ivec2 c = clamp(p + ivec2(i, j), ivec2(0), maxCoord);
|
||||
float v = texelFetch(uHeightTex, c, 0).r;
|
||||
vmax = max(vmax, v);
|
||||
}
|
||||
}
|
||||
|
||||
return vmax;
|
||||
}
|
||||
|
||||
vec3 colorRamp(float t) {
|
||||
if (t < 0.5) {
|
||||
return mix(uColorLow, uColorMid, t / 0.5);
|
||||
}
|
||||
return mix(uColorMid, uColorHigh, (t - 0.5) / 0.5);
|
||||
}
|
||||
|
||||
vec3 colorRampWithZero(float t) {
|
||||
const float zeroW = 0.005;
|
||||
float blend = smoothstep(0.0, zeroW, t);
|
||||
float tRamp = saturate((t - zeroW) / max(1.0 - zeroW, 1e-6));
|
||||
vec3 ramp = colorRamp(tRamp);
|
||||
return mix(uColorZero, ramp, blend);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float vC = texture(uHeightTex, vUV).r;
|
||||
bool isZero = abs(vC) <= 1e-6;
|
||||
float t = value01(vC);
|
||||
float m = maxNeighborValue(vUV);
|
||||
float eps = max(1e-4, 0.001 * (uMaxV - uMinV));
|
||||
float force = smoothstep(uMaxV - eps, uMaxV, m);
|
||||
t = max(t, force);
|
||||
vec3 base = isZero ? uColorZero : colorRampWithZero(t);
|
||||
|
||||
float vL = texture(uHeightTex, vUV - vec2(uTexelSize.x, 0.0)).r;
|
||||
float vR = texture(uHeightTex, vUV + vec2(uTexelSize.x, 0.0)).r;
|
||||
float vD = texture(uHeightTex, vUV - vec2(0.0, uTexelSize.y)).r;
|
||||
float vU = texture(uHeightTex, vUV + vec2(0.0, uTexelSize.y)).r;
|
||||
|
||||
float hL = value01(vL) * uHeightScale;
|
||||
float hR = value01(vR) * uHeightScale;
|
||||
float hD = value01(vD) * uHeightScale;
|
||||
float hU = value01(vU) * uHeightScale;
|
||||
|
||||
float dx = max(1e-6, uPlaneSize.x * uTexelSize.x);
|
||||
float dy = max(1e-6, uPlaneSize.y * uTexelSize.y);
|
||||
float dHx = (hR - hL) / (2.0 * dx);
|
||||
float dHy = (hU - hD) / (2.0 * dy);
|
||||
|
||||
vec3 N = normalize(vec3(-dHx, -dHy, -1.0));
|
||||
vec3 L = normalize(uLightDir);
|
||||
vec3 V = normalize(uCameraPos - vWorldPos);
|
||||
vec3 H = normalize(L + V);
|
||||
|
||||
float diff = saturate(dot(N, L));
|
||||
float spec = pow(saturate(dot(N, H)), 24.0);
|
||||
|
||||
vec3 col = base * (0.50 + 0.50 * diff);
|
||||
col += vec3(1.0) * spec * 0.18;
|
||||
FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
||||
}
|
||||
@@ -1,28 +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);
|
||||
#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);
|
||||
}
|
||||
@@ -1,29 +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);
|
||||
#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);
|
||||
}
|
||||
@@ -1,19 +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;
|
||||
}
|
||||
#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