update
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
.cache/
|
.cache/
|
||||||
.idea/
|
.idea/
|
||||||
build/
|
build/
|
||||||
cmake-build*/
|
cmake-build*/
|
||||||
@@ -1,31 +1,46 @@
|
|||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
project(3dviewer)
|
project(3dviewer)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
include_directories(.)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
find_package(glfw3 REQUIRED)
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
|
||||||
find_package(OpenGL REQUIRED)
|
|
||||||
|
include_directories(.)
|
||||||
add_executable(
|
find_package(glfw3 REQUIRED)
|
||||||
${PROJECT_NAME}
|
find_package(OpenGL REQUIRED)
|
||||||
main.cpp
|
|
||||||
glad.c
|
add_executable(
|
||||||
lopenglprogram.cpp
|
${PROJECT_NAME}
|
||||||
camera.h
|
main.cpp
|
||||||
)
|
glad.c
|
||||||
|
lopenglprogram.cpp
|
||||||
target_link_libraries(
|
camera.h
|
||||||
${PROJECT_NAME}
|
)
|
||||||
PRIVATE
|
|
||||||
glfw3
|
if(WIN32)
|
||||||
OpenGL::GL
|
target_link_libraries(
|
||||||
X11
|
${PROJECT_NAME}
|
||||||
Xrandr
|
PRIVATE
|
||||||
Xi
|
glfw3
|
||||||
Xcursor
|
)
|
||||||
Xinerama
|
elseif(APPLE)
|
||||||
pthread
|
|
||||||
dl
|
elseif(UNIX)
|
||||||
m
|
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.
|
// Created by Lenn on 2026/1/30.
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef INC_3DVIEWER_CAMERA_H
|
#ifndef INC_3DVIEWER_CAMERA_H
|
||||||
#define INC_3DVIEWER_CAMERA_H
|
#define INC_3DVIEWER_CAMERA_H
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
typedef enum CameraMoveBit {
|
typedef enum CameraMoveBit {
|
||||||
FORWARD = 0x0001,
|
FORWARD = 0x0001,
|
||||||
BACKWARD = 0x0002,
|
BACKWARD = 0x0002,
|
||||||
LEFT = 0x0004,
|
LEFT = 0x0004,
|
||||||
RIGHT = 0x0008,
|
RIGHT = 0x0008,
|
||||||
} CameraMove;
|
} CameraMove;
|
||||||
|
|
||||||
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
|
||||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
|
||||||
float yaw = -90.0f, float pitch = 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)
|
: front_(glm::vec3(0.0f, 0.0f, -1.0f)), yaw_(yaw), pitch_(pitch), position_(position)
|
||||||
, worldUp_(up){
|
, worldUp_(up){
|
||||||
zoom_ = 45.0f;
|
zoom_ = 45.0f;
|
||||||
mouseSensitivity_ = 0.08f;
|
mouseSensitivity_ = 0.08f;
|
||||||
moveSpeed_ = 2.5f;
|
moveSpeed_ = 2.5f;
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void keyboardCallback(CameraMove direction, float deltaTime) {
|
void keyboardCallback(CameraMove direction, float deltaTime) {
|
||||||
float velocity = moveSpeed_ * deltaTime;
|
float velocity = moveSpeed_ * deltaTime;
|
||||||
if (direction == CameraMove::FORWARD) {
|
if (direction == CameraMove::FORWARD) {
|
||||||
position_ += front_ * velocity;
|
position_ += front_ * velocity;
|
||||||
}
|
}
|
||||||
else if (direction == CameraMove::BACKWARD) {
|
else if (direction == CameraMove::BACKWARD) {
|
||||||
position_ -= front_ * velocity;
|
position_ -= front_ * velocity;
|
||||||
}
|
}
|
||||||
else if (direction == CameraMove::LEFT) {
|
else if (direction == CameraMove::LEFT) {
|
||||||
position_ -= right_ * velocity;
|
position_ -= right_ * velocity;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
position_ += right_ * velocity;
|
position_ += right_ * velocity;
|
||||||
}
|
}
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mouseMoveCallback(float xoffset, float yoffset, GLboolean constrainPitch = true) {
|
void mouseMoveCallback(float xoffset, float yoffset, GLboolean constrainPitch = true) {
|
||||||
xoffset *= mouseSensitivity_;
|
xoffset *= mouseSensitivity_;
|
||||||
yoffset *= mouseSensitivity_;
|
yoffset *= mouseSensitivity_;
|
||||||
|
|
||||||
yaw_ += xoffset;
|
yaw_ += xoffset;
|
||||||
pitch_ += yoffset;
|
pitch_ += yoffset;
|
||||||
|
|
||||||
if (constrainPitch) {
|
if (constrainPitch) {
|
||||||
if (pitch_ > 89.0f) {
|
if (pitch_ > 89.0f) {
|
||||||
pitch_ = 89.0f;
|
pitch_ = 89.0f;
|
||||||
}
|
}
|
||||||
if (pitch_ < -89.0f) {
|
if (pitch_ < -89.0f) {
|
||||||
pitch_ = -89.0f;
|
pitch_ = -89.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mouseScrollCallback(float yoffset) {
|
void mouseScrollCallback(float yoffset) {
|
||||||
zoom_ -= yoffset;
|
zoom_ -= yoffset;
|
||||||
if (zoom_ < 1.0f) {
|
if (zoom_ < 1.0f) {
|
||||||
zoom_ = 1.0;
|
zoom_ = 1.0;
|
||||||
}
|
}
|
||||||
if (zoom_ > 45.0f) {
|
if (zoom_ > 45.0f) {
|
||||||
zoom_ = 45.0;
|
zoom_ = 45.0;
|
||||||
}
|
}
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 getViewMatrix() {
|
glm::mat4 getViewMatrix() {
|
||||||
return glm::lookAt(position_, position_ + front_, up_);
|
return glm::lookAt(position_, position_ + front_, up_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool firstMouse() const { return firstMouse_; }
|
bool firstMouse() const { return firstMouse_; }
|
||||||
|
|
||||||
void triggleFirstMouse() { firstMouse_ = !firstMouse_; }
|
void triggleFirstMouse() { firstMouse_ = !firstMouse_; }
|
||||||
|
|
||||||
float Zoom() const { return zoom_; }
|
float Zoom() const { return zoom_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateCameraVectors() {
|
void updateCameraVectors() {
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
front.x = std::cos(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
front.x = std::cos(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||||
front.y = std::sin(glm::radians(pitch_));
|
front.y = std::sin(glm::radians(pitch_));
|
||||||
front.z = std::sin(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
front.z = std::sin(glm::radians(yaw_)) * std::cos(glm::radians(pitch_));
|
||||||
front_ = glm::normalize(front);
|
front_ = glm::normalize(front);
|
||||||
|
|
||||||
right_ = glm::normalize(glm::cross(front_, worldUp_));
|
right_ = glm::normalize(glm::cross(front_, worldUp_));
|
||||||
up_ = glm::normalize(glm::cross(right_, front_));
|
up_ = glm::normalize(glm::cross(right_, front_));
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
glm::vec3 position_;
|
glm::vec3 position_;
|
||||||
glm::vec3 up_;
|
glm::vec3 up_;
|
||||||
glm::vec3 right_;
|
glm::vec3 right_;
|
||||||
glm::vec3 front_;
|
glm::vec3 front_;
|
||||||
glm::vec3 worldUp_;
|
glm::vec3 worldUp_;
|
||||||
|
|
||||||
float yaw_;
|
float yaw_;
|
||||||
float pitch_;
|
float pitch_;
|
||||||
|
|
||||||
float moveSpeed_;
|
float moveSpeed_;
|
||||||
float mouseSensitivity_;
|
float mouseSensitivity_;
|
||||||
float zoom_;
|
float zoom_;
|
||||||
|
|
||||||
bool firstMouse_ = true;
|
bool firstMouse_ = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //INC_3DVIEWER_CAMERA_H
|
#endif //INC_3DVIEWER_CAMERA_H
|
||||||
@@ -1,208 +1,208 @@
|
|||||||
#include "lopenglprogram.h"
|
#include "lopenglprogram.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
LOpenGLShader::LOpenGLShader(LOpenGLShader::ShaderType type)
|
LOpenGLShader::LOpenGLShader(LOpenGLShader::ShaderType type)
|
||||||
: shaderType_(type), compiled_(false) {}
|
: shaderType_(type), compiled_(false) {}
|
||||||
|
|
||||||
LOpenGLShader::~LOpenGLShader() {
|
LOpenGLShader::~LOpenGLShader() {
|
||||||
if (shaderId_ != 0) {
|
if (shaderId_ != 0) {
|
||||||
glDeleteShader(shaderId_);
|
glDeleteShader(shaderId_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LOpenGLShader::compileShaderFromSource(const char *source) {
|
bool LOpenGLShader::compileShaderFromSource(const char *source) {
|
||||||
if (!source || compiled_) {
|
if (!source || compiled_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (shaderType_) {
|
switch (shaderType_) {
|
||||||
case Vertex:
|
case Vertex:
|
||||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||||
break;
|
break;
|
||||||
case Fragment:
|
case Fragment:
|
||||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
glShaderSource(shaderId_, 1, &source, NULL);
|
glShaderSource(shaderId_, 1, &source, NULL);
|
||||||
glCompileShader(shaderId_);
|
glCompileShader(shaderId_);
|
||||||
int success = 0;
|
int success = 0;
|
||||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||||
shaderLog_ = std::string(infoLog);
|
shaderLog_ = std::string(infoLog);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
compiled_ = true;
|
compiled_ = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LOpenGLShader::compileShaderFromSource(const std::string &source) {
|
bool LOpenGLShader::compileShaderFromSource(const std::string &source) {
|
||||||
if (source.empty() || compiled_) {
|
if (source.empty() || compiled_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (shaderType_) {
|
switch (shaderType_) {
|
||||||
case Vertex:
|
case Vertex:
|
||||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||||
break;
|
break;
|
||||||
case Fragment:
|
case Fragment:
|
||||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto source_ch = source.c_str();
|
auto source_ch = source.c_str();
|
||||||
glShaderSource(shaderId_, 1, &source_ch, nullptr);
|
glShaderSource(shaderId_, 1, &source_ch, nullptr);
|
||||||
glCompileShader(shaderId_);
|
glCompileShader(shaderId_);
|
||||||
int success = 0;
|
int success = 0;
|
||||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetShaderInfoLog(shaderId_, 512, nullptr, infoLog);
|
glGetShaderInfoLog(shaderId_, 512, nullptr, infoLog);
|
||||||
shaderLog_ = std::string(infoLog);
|
shaderLog_ = std::string(infoLog);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
compiled_ = true;
|
compiled_ = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LOpenGLShader::compileShaderFromFile(const std::string &path) {
|
bool LOpenGLShader::compileShaderFromFile(const std::string &path) {
|
||||||
std::string shaderCode;
|
std::string shaderCode;
|
||||||
std::fstream shaderFile;
|
std::fstream shaderFile;
|
||||||
std::stringstream shaderStream;
|
std::stringstream shaderStream;
|
||||||
|
|
||||||
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
shaderFile.open(path);
|
shaderFile.open(path);
|
||||||
shaderStream << shaderFile.rdbuf();
|
shaderStream << shaderFile.rdbuf();
|
||||||
shaderFile.close();
|
shaderFile.close();
|
||||||
shaderCode = shaderStream.str();
|
shaderCode = shaderStream.str();
|
||||||
} catch (std::ifstream::failure e) {
|
} catch (std::ifstream::failure e) {
|
||||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shaderCode.empty() || compiled_) {
|
if (shaderCode.empty() || compiled_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (shaderType_) {
|
switch (shaderType_) {
|
||||||
case Vertex:
|
case Vertex:
|
||||||
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
shaderId_ = glCreateShader(GL_VERTEX_SHADER);
|
||||||
break;
|
break;
|
||||||
case Fragment:
|
case Fragment:
|
||||||
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
shaderId_ = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto source_ch = shaderCode.c_str();
|
auto source_ch = shaderCode.c_str();
|
||||||
glShaderSource(shaderId_, 1, &source_ch, NULL);
|
glShaderSource(shaderId_, 1, &source_ch, NULL);
|
||||||
glCompileShader(shaderId_);
|
glCompileShader(shaderId_);
|
||||||
int success = 0;
|
int success = 0;
|
||||||
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shaderId_, GL_COMPILE_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
glGetShaderInfoLog(shaderId_, 512, NULL, infoLog);
|
||||||
shaderLog_ = std::string(infoLog);
|
shaderLog_ = std::string(infoLog);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
compiled_ = true;
|
compiled_ = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOpenGLProgram::LOpenGLProgram() { Create(); }
|
LOpenGLProgram::LOpenGLProgram() { Create(); }
|
||||||
|
|
||||||
LOpenGLProgram::~LOpenGLProgram() {
|
LOpenGLProgram::~LOpenGLProgram() {
|
||||||
if (programId_ != 0) {
|
if (programId_ != 0) {
|
||||||
glDeleteProgram(programId_);
|
glDeleteProgram(programId_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::Create() {
|
void LOpenGLProgram::Create() {
|
||||||
programId_ = glCreateProgram();
|
programId_ = glCreateProgram();
|
||||||
created_ = (programId_ != 0);
|
created_ = (programId_ != 0);
|
||||||
linked_ = false;
|
linked_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LOpenGLProgram::Link() {
|
bool LOpenGLProgram::Link() {
|
||||||
if (!created_ || linked_ || shaderList_.empty()) {
|
if (!created_ || linked_ || shaderList_.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
glLinkProgram(programId_);
|
glLinkProgram(programId_);
|
||||||
GLint success = 0;
|
GLint success = 0;
|
||||||
glGetProgramiv(programId_, GL_LINK_STATUS, &success);
|
glGetProgramiv(programId_, GL_LINK_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
glGetProgramInfoLog(programId_, 512, nullptr, infoLog);
|
glGetProgramInfoLog(programId_, 512, nullptr, infoLog);
|
||||||
std::cout << "ERROR::PROGRAM::LINK_FAILED\n" << infoLog << std::endl;
|
std::cout << "ERROR::PROGRAM::LINK_FAILED\n" << infoLog << std::endl;
|
||||||
linked_ = false;
|
linked_ = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
linked_ = true;
|
linked_ = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::Use() const {
|
void LOpenGLProgram::Use() const {
|
||||||
if (!linked_)
|
if (!linked_)
|
||||||
return;
|
return;
|
||||||
glUseProgram(programId_);
|
glUseProgram(programId_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, float value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, float value) const {
|
||||||
glUniform1f(glGetUniformLocation(programId_, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(programId_, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, int value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, int value) const {
|
||||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(programId_, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, bool value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, bool value) const {
|
||||||
glUniform1i(glGetUniformLocation(programId_, name.c_str()), static_cast<int>(value));
|
glUniform1i(glGetUniformLocation(programId_, name.c_str()), static_cast<int>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec3 value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec3 value) const {
|
||||||
glUniform3fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
glUniform3fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, float x, float y, float z) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, float x, float y, float z) const {
|
||||||
glUniform3f(glGetUniformLocation(programId_, name.c_str()), x, y, z);
|
glUniform3f(glGetUniformLocation(programId_, name.c_str()), x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::mat4 value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, glm::mat4 value) const {
|
||||||
glUniformMatrix4fv(glGetUniformLocation(programId_, name.c_str()), 1, GL_FALSE, &value[0][0]);
|
glUniformMatrix4fv(glGetUniformLocation(programId_, name.c_str()), 1, GL_FALSE, &value[0][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LOpenGLProgram::addShader(std::unique_ptr<LOpenGLShader> shader) {
|
bool LOpenGLProgram::addShader(std::unique_ptr<LOpenGLShader> shader) {
|
||||||
if (!shader || !shader->Compiled()) {
|
if (!shader || !shader->Compiled()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!created_) {
|
if (!created_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
glAttachShader(programId_, shader->shaderId_);
|
glAttachShader(programId_, shader->shaderId_);
|
||||||
shaderList_.push_back(std::move(shader));
|
shaderList_.push_back(std::move(shader));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec2 value) const {
|
void LOpenGLProgram::setUniformValue(const std::string &name, glm::vec2 value) const {
|
||||||
glUniform2fv(glGetUniformLocation(programId_, name.c_str()), 1, &value[0]);
|
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) {
|
||||||
glDetachShader(programId_, id);
|
glDetachShader(programId_, id);
|
||||||
shaderList_.erase(ite);
|
shaderList_.erase(ite);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
122
lopenglprogram.h
122
lopenglprogram.h
@@ -1,61 +1,61 @@
|
|||||||
#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>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
class LOpenGLProgram;
|
class LOpenGLProgram;
|
||||||
class LOpenGLShader {
|
class LOpenGLShader {
|
||||||
public:
|
public:
|
||||||
typedef enum ShaderTypeBit {
|
typedef enum ShaderTypeBit {
|
||||||
Vertex = 0x0001,
|
Vertex = 0x0001,
|
||||||
Fragment = 0x0002,
|
Fragment = 0x0002,
|
||||||
} ShaderType;
|
} ShaderType;
|
||||||
LOpenGLShader() = delete;
|
LOpenGLShader() = delete;
|
||||||
LOpenGLShader(LOpenGLShader::ShaderType type);
|
LOpenGLShader(LOpenGLShader::ShaderType type);
|
||||||
~LOpenGLShader();
|
~LOpenGLShader();
|
||||||
bool compileShaderFromSource(const char *source);
|
bool compileShaderFromSource(const char *source);
|
||||||
bool compileShaderFromSource(const std::string &source);
|
bool compileShaderFromSource(const std::string &source);
|
||||||
bool compileShaderFromFile(const std::string &path);
|
bool compileShaderFromFile(const std::string &path);
|
||||||
bool Compiled() const { return compiled_; }
|
bool Compiled() const { return compiled_; }
|
||||||
std::string Log() const { return shaderLog_; }
|
std::string Log() const { return shaderLog_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLuint shaderId_{};
|
GLuint shaderId_{};
|
||||||
std::string shaderSource_;
|
std::string shaderSource_;
|
||||||
LOpenGLShader::ShaderType shaderType_;
|
LOpenGLShader::ShaderType shaderType_;
|
||||||
bool compiled_;
|
bool compiled_;
|
||||||
std::string shaderLog_;
|
std::string shaderLog_;
|
||||||
|
|
||||||
friend class LOpenGLProgram;
|
friend class LOpenGLProgram;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LOpenGLProgram {
|
class LOpenGLProgram {
|
||||||
public:
|
public:
|
||||||
LOpenGLProgram();
|
LOpenGLProgram();
|
||||||
~LOpenGLProgram();
|
~LOpenGLProgram();
|
||||||
|
|
||||||
bool addShader(std::unique_ptr<LOpenGLShader> shader);
|
bool addShader(std::unique_ptr<LOpenGLShader> shader);
|
||||||
void removeShader(GLuint id);
|
void removeShader(GLuint id);
|
||||||
void Create();
|
void Create();
|
||||||
bool Link();
|
bool Link();
|
||||||
void Use() const;
|
void Use() const;
|
||||||
bool Linked() const { return linked_; }
|
bool Linked() const { return linked_; }
|
||||||
|
|
||||||
void setUniformValue(const std::string& name, float value) const;
|
void setUniformValue(const std::string& name, float value) const;
|
||||||
void setUniformValue(const std::string& name, int 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, bool value) const;
|
||||||
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;
|
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_;
|
||||||
GLuint programId_ = 0;
|
GLuint programId_ = 0;
|
||||||
bool linked_ = false;
|
bool linked_ = false;
|
||||||
bool created_ = false;
|
bool created_ = false;
|
||||||
};
|
};
|
||||||
|
|||||||
983
main.cpp
983
main.cpp
@@ -1,475 +1,508 @@
|
|||||||
#include "lopenglprogram.h"
|
#include "lopenglprogram.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <glm/ext/matrix_clip_space.hpp>
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
#include <glm/fwd.hpp>
|
#include <glm/fwd.hpp>
|
||||||
#include <glm/trigonometric.hpp>
|
#include <glm/trigonometric.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <tuple>
|
#include <string>
|
||||||
#include <utility>
|
#include <tuple>
|
||||||
|
#include <utility>
|
||||||
|
#include <print>
|
||||||
class GLWidget {
|
|
||||||
|
|
||||||
public:
|
class GLWidget {
|
||||||
struct ViewPort {
|
|
||||||
int width;
|
public:
|
||||||
int height;
|
struct ViewPort {
|
||||||
};
|
int width;
|
||||||
GLWidget(ViewPort port) : viewport_(port)
|
int height;
|
||||||
, window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);})
|
};
|
||||||
, deinit_(false) {
|
GLWidget(ViewPort port) : viewport_(port)
|
||||||
lastX_ = port.width / 2;
|
, window_(nullptr, [](GLFWwindow* w){if(w) glfwDestroyWindow(w);})
|
||||||
lastY_ = port.height / 2;
|
, deinit_(false) {
|
||||||
initGeometry();
|
lastX_ = port.width / 2;
|
||||||
}
|
lastY_ = port.height / 2;
|
||||||
~GLWidget() {
|
initGeometry();
|
||||||
if (!deinit_) {
|
}
|
||||||
// OpenGL objects should be deleted while a valid context still exists.
|
~GLWidget() {
|
||||||
if (window_) {
|
if (!deinit_) {
|
||||||
glfwMakeContextCurrent(window_.get());
|
// OpenGL objects should be deleted while a valid context still exists.
|
||||||
}
|
if (window_) {
|
||||||
if (panelIbo_) {
|
glfwMakeContextCurrent(window_.get());
|
||||||
glDeleteBuffers(1, &panelIbo_);
|
}
|
||||||
}
|
if (panelIbo_) {
|
||||||
if (panelVbo_) {
|
glDeleteBuffers(1, &panelIbo_);
|
||||||
glDeleteBuffers(1, &panelVbo_);
|
}
|
||||||
}
|
if (panelVbo_) {
|
||||||
if (panelVao_) {
|
glDeleteBuffers(1, &panelVbo_);
|
||||||
glDeleteVertexArrays(1, &panelVao_);
|
}
|
||||||
}
|
if (panelVao_) {
|
||||||
if (bgVao_) {
|
glDeleteVertexArrays(1, &panelVao_);
|
||||||
glDeleteVertexArrays(1, &bgVao_);
|
}
|
||||||
}
|
if (bgVao_) {
|
||||||
if (bgVbo_) {
|
glDeleteVertexArrays(1, &bgVao_);
|
||||||
glDeleteBuffers(1, &bgVbo_);
|
}
|
||||||
}
|
if (bgVbo_) {
|
||||||
deinit_ = true;
|
glDeleteBuffers(1, &bgVbo_);
|
||||||
}
|
}
|
||||||
|
if (heatmapVao_) {
|
||||||
}
|
glDeleteVertexArrays(1, &heatmapVao_);
|
||||||
|
}
|
||||||
void setViewPort(int width, int height) {
|
if (heatmapVbo_) {
|
||||||
viewport_ = {width, height};
|
glDeleteBuffers(1, &heatmapVbo_);
|
||||||
}
|
}
|
||||||
|
deinit_ = true;
|
||||||
bool initGeometry() {
|
}
|
||||||
if (!glfwInit()) {
|
|
||||||
std::cout << "Failed to initialize GLFW" << std::endl;
|
}
|
||||||
return false;
|
|
||||||
}
|
void setViewPort(int width, int height) {
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
viewport_ = {width, height};
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
}
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
#if __APPLE__
|
bool initGeometry() {
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
if (!glfwInit()) {
|
||||||
#endif
|
std::cout << "Failed to initialize GLFW\n";
|
||||||
window_.reset(glfwCreateWindow(viewport_.width, viewport_.height, "3dviewer", nullptr, nullptr));
|
return false;
|
||||||
if (!window_) {
|
}
|
||||||
std::cout << "Failed to create GLFW window" << std::endl;
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwTerminate();
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
return false;
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
}
|
#if __APPLE__
|
||||||
glfwMakeContextCurrent(window_.get());
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
#endif
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
window_.reset(glfwCreateWindow(viewport_.width, viewport_.height, "3dviewer", nullptr, nullptr));
|
||||||
return false;
|
if (!window_) {
|
||||||
}
|
std::cout << "Failed to create GLFW window\n";
|
||||||
|
glfwTerminate();
|
||||||
glViewport(0, 0, viewport_.width, viewport_.height);
|
return false;
|
||||||
glEnable(GL_DEPTH_TEST);
|
}
|
||||||
glfwSetInputMode(window_.get(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
glfwMakeContextCurrent(window_.get());
|
||||||
glfwSetWindowUserPointer(window_.get(), this);
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebufferSizeCallback);
|
std::cout << "Failed to initialize GLAD\n";
|
||||||
glfwSetCursorPosCallback(window_.get(), &GLWidget::mouseCallback);
|
return false;
|
||||||
glfwSetScrollCallback(window_.get(), &GLWidget::scrollCallback);
|
}
|
||||||
return true;
|
|
||||||
}
|
glViewport(0, 0, viewport_.width, viewport_.height);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
void setBgShaderPath(std::string vp, std::string fp) {
|
glfwSetInputMode(window_.get(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
bgVertShaderPath_ = vp;
|
glfwSetWindowUserPointer(window_.get(), this);
|
||||||
bgFragShaderPath_ = fp;
|
glfwSetFramebufferSizeCallback(window_.get(), &GLWidget::framebufferSizeCallback);
|
||||||
}
|
glfwSetCursorPosCallback(window_.get(), &GLWidget::mouseCallback);
|
||||||
|
glfwSetScrollCallback(window_.get(), &GLWidget::scrollCallback);
|
||||||
void setPanelShaderPath(std::string vp, std::string fp) {
|
return true;
|
||||||
panelVertShaderPath_ = vp;
|
}
|
||||||
panelFragShaderPath_ = fp;
|
|
||||||
}
|
void setBgShaderPath(std::string vp, std::string fp) {
|
||||||
private:
|
bgVertShaderPath_ = vp;
|
||||||
void initBgGeometry() {
|
bgFragShaderPath_ = fp;
|
||||||
if (bgVbo_) {
|
}
|
||||||
glDeleteBuffers(1, &bgVbo_);
|
|
||||||
bgVbo_ = 0;
|
void setPanelShaderPath(std::string vp, std::string fp) {
|
||||||
}
|
panelVertShaderPath_ = vp;
|
||||||
if (bgVao_) {
|
panelFragShaderPath_ = fp;
|
||||||
glDeleteVertexArrays(1, &bgVao_);
|
}
|
||||||
bgVao_ = 0;
|
private:
|
||||||
}
|
void initBgGeometry() {
|
||||||
|
if (bgVbo_) {
|
||||||
const float verts[] = {
|
glDeleteBuffers(1, &bgVbo_);
|
||||||
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0,
|
bgVbo_ = 0;
|
||||||
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
|
}
|
||||||
};
|
if (bgVao_) {
|
||||||
|
glDeleteVertexArrays(1, &bgVao_);
|
||||||
glGenVertexArrays(1, &bgVao_);
|
bgVao_ = 0;
|
||||||
glBindVertexArray(bgVao_);
|
}
|
||||||
|
|
||||||
glGenBuffers(1, &bgVbo_);
|
const float verts[] = {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, bgVbo_);
|
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0,
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
|
||||||
|
};
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
glGenVertexArrays(1, &bgVao_);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(bgVao_);
|
||||||
|
|
||||||
}
|
glGenBuffers(1, &bgVbo_);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, bgVbo_);
|
||||||
void initPanelGeometry_() {
|
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||||
if (panelVbo_) {
|
|
||||||
glDeleteBuffers(1, &panelVbo_);
|
glEnableVertexAttribArray(0);
|
||||||
panelVbo_ = 0;
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||||
}
|
glBindVertexArray(0);
|
||||||
if (panelVao_) {
|
|
||||||
glDeleteVertexArrays(1, &panelVao_);
|
}
|
||||||
panelVao_ = 0;
|
|
||||||
}
|
void initPanelGeometry_() {
|
||||||
if (panelIbo_) {
|
if (panelVbo_) {
|
||||||
glDeleteBuffers(1, &panelIbo_);
|
glDeleteBuffers(1, &panelVbo_);
|
||||||
panelIbo_ = 0;
|
panelVbo_ = 0;
|
||||||
}
|
}
|
||||||
|
if (panelVao_) {
|
||||||
const float y = panelHeight_ * 0.5f;
|
glDeleteVertexArrays(1, &panelVao_);
|
||||||
const float x = panelWidth_ * 0.5f;
|
panelVao_ = 0;
|
||||||
const float z = panelDepth_ * 0.5f;
|
}
|
||||||
|
if (panelIbo_) {
|
||||||
struct V {
|
glDeleteBuffers(1, &panelIbo_);
|
||||||
float x, y, z;
|
panelIbo_ = 0;
|
||||||
float nx, ny, nz;
|
}
|
||||||
};
|
|
||||||
const V verts[24] = {
|
const float y = panelHeight_ * 0.5f;
|
||||||
// 顶面
|
const float x = panelWidth_ * 0.5f;
|
||||||
{-x, y, -z, 0, 1, 0},
|
const float z = panelDepth_ * 0.5f;
|
||||||
{x, y, -z, 0, 1, 0},
|
|
||||||
{x, y, z, 0, 1, 0 },
|
struct V {
|
||||||
{-x, y, z, 0, 1, 0},
|
float x, y, z;
|
||||||
|
float nx, ny, nz;
|
||||||
// 前面
|
};
|
||||||
{-x, y, z, 0, 0, 1},
|
const V verts[24] = {
|
||||||
{x, y, z, 0, 0, 1},
|
// 顶面
|
||||||
{x, -y, z, 0, 0, 1},
|
{-x, y, -z, 0, 1, 0},
|
||||||
{-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, -1, 0},
|
// 前面
|
||||||
{x, -y, z, 0, -1, 0},
|
{-x, y, z, 0, 0, 1},
|
||||||
{-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, 0, -1},
|
// 底面
|
||||||
{x, -y, -z, 0, 0, -1},
|
{-x, -y, -z, 0, -1, 0},
|
||||||
{-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, -1, 0, 0},
|
|
||||||
{-x, y, -z, -1, 0, 0},
|
// 后面
|
||||||
{-x, -y, -z, -1, 0, 0},
|
{-x, y, -z, 0, 0, -1},
|
||||||
{-x, -y, z, -1, 0, 0},
|
{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,
|
{x, y, -z, 1, 0, 0},
|
||||||
8, 9, 10, 8, 10, 11,
|
{x, y, z, 1, 0, 0},
|
||||||
12, 13, 14, 12, 14, 15,
|
{x, -y, z, 1, 0, 0},
|
||||||
16, 17, 18, 16, 18, 19,
|
{x, -y, -z, 1, 0, 0},
|
||||||
20, 21, 22, 20, 22, 23,
|
};
|
||||||
};
|
|
||||||
|
unsigned int idx[36] = {
|
||||||
glGenVertexArrays(1, &panelVao_);
|
0, 1, 2, 0, 2, 3,
|
||||||
glBindVertexArray(panelVao_);
|
4, 5, 6, 4, 6, 7,
|
||||||
|
8, 9, 10, 8, 10, 11,
|
||||||
glGenBuffers(1, &panelVbo_);
|
12, 13, 14, 12, 14, 15,
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, panelVbo_);
|
16, 17, 18, 16, 18, 19,
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
20, 21, 22, 20, 22, 23,
|
||||||
|
};
|
||||||
glGenBuffers(1, &panelIbo_);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, panelIbo_);
|
glGenVertexArrays(1, &panelVao_);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
|
glBindVertexArray(panelVao_);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glGenBuffers(1, &panelVbo_);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(V), (void*)0);
|
glBindBuffer(GL_ARRAY_BUFFER, panelVbo_);
|
||||||
glEnableVertexAttribArray(1);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(V), (void*)(3 * sizeof(float)));
|
|
||||||
|
glGenBuffers(1, &panelIbo_);
|
||||||
glBindVertexArray(0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, panelIbo_);
|
||||||
}
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
|
||||||
|
|
||||||
void initHeatmapGeometry_() {
|
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)));
|
||||||
bool initBgProgram() {
|
|
||||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
glBindVertexArray(0);
|
||||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
}
|
||||||
if (!vshader->compileShaderFromFile(bgVertShaderPath_)) {
|
|
||||||
std::cout << "Vertex shader compile failed: " << bgVertShaderPath_ << "\n"
|
void initHeatmapGeometry_() {
|
||||||
<< vshader->Log() << std::endl;
|
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
if (!fshader->compileShaderFromFile(bgFragShaderPath_)) {
|
bool initBgProgram() {
|
||||||
std::cout << "Fragment shader compile failed: " << bgFragShaderPath_ << "\n"
|
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||||
<< fshader->Log() << std::endl;
|
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||||
return false;
|
if (!vshader->compileShaderFromFile(bgVertShaderPath_)) {
|
||||||
}
|
std::cout << "Vertex shader compile failed: " << bgVertShaderPath_ << "\n"
|
||||||
bgProg_ = std::make_unique<LOpenGLProgram>();
|
<< vshader->Log() << std::endl;
|
||||||
if (!bgProg_->addShader(std::move(vshader))) {
|
return false;
|
||||||
std::cout << "Failed to attach vertex shader\n";
|
}
|
||||||
return false;
|
if (!fshader->compileShaderFromFile(bgFragShaderPath_)) {
|
||||||
}
|
std::cout << "Fragment shader compile failed: " << bgFragShaderPath_ << "\n"
|
||||||
if (!bgProg_->addShader(std::move(fshader))) {
|
<< fshader->Log() << std::endl;
|
||||||
std::cout << "Failed to attach fragment shader\n";
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
bgProg_ = std::make_unique<LOpenGLProgram>();
|
||||||
bool ret = bgProg_->Link();
|
if (!bgProg_->addShader(std::move(vshader))) {
|
||||||
if (!ret) {
|
std::cout << "Failed to attach vertex shader\n";
|
||||||
std::cout << "Failed to link background program\n";
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
if (!bgProg_->addShader(std::move(fshader))) {
|
||||||
|
std::cout << "Failed to attach fragment shader\n";
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
bool ret = bgProg_->Link();
|
||||||
bool initPanelProgram() {
|
if (!ret) {
|
||||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
std::cout << "Failed to link background program\n";
|
||||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
return false;
|
||||||
if (!vshader->compileShaderFromFile(panelVertShaderPath_)) {
|
}
|
||||||
std::cout << "Vertex shader compile failed: " << panelVertShaderPath_ << "\n" << vshader->Log() << std::endl;
|
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
if (!fshader->compileShaderFromFile(panelFragShaderPath_)) {
|
|
||||||
std::cout << "Fragment shader compile failed: " << panelFragShaderPath_ << "\n" << fshader->Log() << std::endl;
|
bool initPanelProgram() {
|
||||||
return false;
|
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||||
}
|
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||||
panelProg_ = std::make_unique<LOpenGLProgram>();
|
if (!vshader->compileShaderFromFile(panelVertShaderPath_)) {
|
||||||
if (!panelProg_->addShader(std::move(vshader))) {
|
std::cout << "Vertex shader compile failed: " << panelVertShaderPath_ << "\n" << vshader->Log() << std::endl;
|
||||||
std::cout << "Failed to attach vertex shader\n";
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
if (!fshader->compileShaderFromFile(panelFragShaderPath_)) {
|
||||||
if (!panelProg_->addShader(std::move(fshader))) {
|
std::cout << "Fragment shader compile failed: " << panelFragShaderPath_ << "\n" << fshader->Log() << std::endl;
|
||||||
std::cout << "Failed to attach fragment shader\n";
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
panelProg_ = std::make_unique<LOpenGLProgram>();
|
||||||
bool ret = panelProg_->Link();
|
if (!panelProg_->addShader(std::move(vshader))) {
|
||||||
if (!ret) {
|
std::cout << "Failed to attach vertex shader\n";
|
||||||
std::cout << "Failed to link panel program\n";
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
if (!panelProg_->addShader(std::move(fshader))) {
|
||||||
|
std::cout << "Failed to attach fragment shader\n";
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
bool ret = panelProg_->Link();
|
||||||
bool initHeatmapProgram() {
|
if (!ret) {
|
||||||
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
std::cout << "Failed to link panel program\n";
|
||||||
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
return false;
|
||||||
|
}
|
||||||
}
|
|
||||||
|
return true;
|
||||||
void drawBg() {
|
}
|
||||||
if (!bgProg_ || !bgVao_ || !bgVbo_) {
|
|
||||||
std::cout << "check !bgProg_ || !bgVao_ || !bgVbo_ failed\n";
|
bool initHeatmapProgram() {
|
||||||
return;
|
auto vshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Vertex);
|
||||||
}
|
auto fshader = std::make_unique<LOpenGLShader>(LOpenGLShader::ShaderType::Fragment);
|
||||||
bgProg_->Use();
|
if (!vshader->compileShaderFromFile(heatmapVertShaderPath_)) {
|
||||||
bgProg_->setUniformValue("uViewport", glm::vec2(viewport_.width, viewport_.height));
|
std::cout << "Vertex shader compile failed: " << heatmapVertShaderPath_ << "\n" << vshader->Log() << std::endl;
|
||||||
bgProg_->setUniformValue("uMajorStep", 120.0f);
|
return false;
|
||||||
bgProg_->setUniformValue("uMinorStep", 24.0f);
|
}
|
||||||
bgProg_->setUniformValue("uLightMode", lightMode_);
|
if (!fshader->compileShaderFromFile(heatmapFragShaderPath_)) {
|
||||||
glDisable(GL_DEPTH_TEST);
|
std::cout << "Fragment shader compile failed: " << heatmapFragShaderPath_ << "\n" << fshader->Log() << std::endl;
|
||||||
glBindVertexArray(bgVao_);
|
return false;
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
}
|
||||||
glBindVertexArray(0);
|
|
||||||
|
heatmapProg_ = std::make_unique<LOpenGLProgram>();
|
||||||
glEnable(GL_DEPTH_TEST);
|
if (!heatmapProg_->addShader(std::move(vshader))) {
|
||||||
}
|
std::cout << "Failed to attach vertex shader\n";
|
||||||
|
return false;
|
||||||
void drawPanel() {
|
}
|
||||||
if (!panelProg_ || !panelVao_ || !panelVbo_ || !panelIbo_) {
|
if (!heatmapProg_->addShader(std::move(fshader))) {
|
||||||
std:: cout << "check !panelProg_ || !panelVao || !panelVbo || !panelIbo\n";
|
std::cout << "Failed to attach vertex shader\n";
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
auto [model, view, projection] = getMVP();
|
|
||||||
panelProg_->Use();
|
bool ret = panelProg_->Link();
|
||||||
panelProg_->setUniformValue("model", model);
|
if (!ret) {
|
||||||
panelProg_->setUniformValue("view", view);
|
std::cout << "Failed to link heatmap program\n";
|
||||||
panelProg_->setUniformValue("projection", projection);
|
return false;
|
||||||
panelProg_->setUniformValue("uLightMode", lightMode_);
|
}
|
||||||
glBindVertexArray(panelVao_);
|
|
||||||
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
return true;
|
||||||
glBindVertexArray(0);
|
}
|
||||||
}
|
|
||||||
|
void drawBg() {
|
||||||
std::tuple<glm::mat4, glm::mat4, glm::mat4> getMVP() {
|
if (!bgProg_ || !bgVao_ || !bgVbo_) {
|
||||||
auto model = glm::mat4(1.0f);
|
std::cout << "check !bgProg_ || !bgVao_ || !bgVbo_ failed\n";
|
||||||
auto view = camera_.getViewMatrix();
|
return;
|
||||||
auto projection = glm::perspective(glm::radians(camera_.Zoom()),
|
}
|
||||||
(float)viewport_.width / (float)viewport_.height, 0.1f, 100.0f);
|
bgProg_->Use();
|
||||||
return {model, view, projection};
|
bgProg_->setUniformValue("uViewport", glm::vec2(viewport_.width, viewport_.height));
|
||||||
}
|
bgProg_->setUniformValue("uMajorStep", 120.0f);
|
||||||
|
bgProg_->setUniformValue("uMinorStep", 24.0f);
|
||||||
public:
|
bgProg_->setUniformValue("uLightMode", lightMode_);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
void eventLoop() {
|
glBindVertexArray(bgVao_);
|
||||||
if (!window_) {
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
return;
|
glBindVertexArray(0);
|
||||||
}
|
|
||||||
initBgGeometry();
|
glEnable(GL_DEPTH_TEST);
|
||||||
if (!initBgProgram()) {
|
}
|
||||||
return;
|
|
||||||
}
|
void drawPanel() {
|
||||||
initPanelGeometry_();
|
if (!panelProg_ || !panelVao_ || !panelVbo_ || !panelIbo_) {
|
||||||
if (!initPanelProgram()) {
|
std:: cout << "check !panelProg_ || !panelVao || !panelVbo || !panelIbo\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (!glfwWindowShouldClose(window_.get())) {
|
auto [model, view, projection] = getMVP();
|
||||||
const float now = static_cast<float>(glfwGetTime());
|
panelProg_->Use();
|
||||||
const float deltaTime = now - lastFrame_;
|
panelProg_->setUniformValue("model", model);
|
||||||
lastFrame_ = now;
|
panelProg_->setUniformValue("view", view);
|
||||||
processInput(window_.get(), deltaTime);
|
panelProg_->setUniformValue("projection", projection);
|
||||||
glClearColor(0.08f, 0.08f, 0.10f, 1.0f);
|
panelProg_->setUniformValue("uLightMode", lightMode_);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glBindVertexArray(panelVao_);
|
||||||
drawBg();
|
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
||||||
drawPanel();
|
glBindVertexArray(0);
|
||||||
glfwSwapBuffers(window_.get());
|
}
|
||||||
glfwPollEvents();
|
|
||||||
}
|
std::tuple<glm::mat4, glm::mat4, glm::mat4> getMVP() {
|
||||||
}
|
auto model = glm::mat4(1.0f);
|
||||||
|
auto view = camera_.getViewMatrix();
|
||||||
private:
|
auto projection = glm::perspective(glm::radians(camera_.Zoom()),
|
||||||
static void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
|
(float)viewport_.width / (float)viewport_.height, 0.1f, 100.0f);
|
||||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
return {model, view, projection};
|
||||||
if (!self) {
|
}
|
||||||
return;
|
|
||||||
}
|
public:
|
||||||
self->onFramebufferSize(width, height);
|
|
||||||
}
|
void eventLoop() {
|
||||||
|
if (!window_) {
|
||||||
void onFramebufferSize(int width, int height) {
|
return;
|
||||||
viewport_ = {width, height};
|
}
|
||||||
glViewport(0, 0, width, height);
|
initBgGeometry();
|
||||||
}
|
if (!initBgProgram()) {
|
||||||
|
return;
|
||||||
void processInput(GLFWwindow* window, float deltaTime) {
|
}
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
initPanelGeometry_();
|
||||||
glfwSetWindowShouldClose(window, true);
|
if (!initPanelProgram()) {
|
||||||
}
|
return;
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
}
|
||||||
camera_.keyboardCallback(Camera::FORWARD, deltaTime);
|
while (!glfwWindowShouldClose(window_.get())) {
|
||||||
}
|
const float now = static_cast<float>(glfwGetTime());
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
const float deltaTime = now - lastFrame_;
|
||||||
camera_.keyboardCallback(Camera::BACKWARD, deltaTime);
|
lastFrame_ = now;
|
||||||
}
|
processInput(window_.get(), deltaTime);
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
glClearColor(0.08f, 0.08f, 0.10f, 1.0f);
|
||||||
camera_.keyboardCallback(Camera::LEFT, deltaTime);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
}
|
drawBg();
|
||||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
drawPanel();
|
||||||
camera_.keyboardCallback(Camera::RIGHT, deltaTime);
|
glfwSwapBuffers(window_.get());
|
||||||
}
|
glfwPollEvents();
|
||||||
lightMode_ = glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mouseCallback(GLFWwindow* window, double x, double y) {
|
private:
|
||||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
static void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
|
||||||
if (!self) {
|
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||||
return;
|
if (!self) {
|
||||||
}
|
return;
|
||||||
self->onMouseMove(x, y);
|
}
|
||||||
}
|
self->onFramebufferSize(width, height);
|
||||||
|
}
|
||||||
void onMouseMove(double x, double y) {
|
|
||||||
float xpos = static_cast<float>(x);
|
void onFramebufferSize(int width, int height) {
|
||||||
float ypos = static_cast<float>(y);
|
viewport_ = {width, height};
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
if (camera_.firstMouse()) {
|
}
|
||||||
lastX_ = x;
|
|
||||||
lastY_ = y;
|
void processInput(GLFWwindow* window, float deltaTime) {
|
||||||
camera_.triggleFirstMouse();
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||||
}
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
}
|
||||||
float xoffset = xpos - lastX_;
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||||
float yoffset = lastY_ - ypos;
|
camera_.keyboardCallback(Camera::FORWARD, deltaTime);
|
||||||
|
}
|
||||||
lastX_ = xpos;
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||||
lastY_ = ypos;
|
camera_.keyboardCallback(Camera::BACKWARD, deltaTime);
|
||||||
|
}
|
||||||
camera_.mouseMoveCallback(xoffset, yoffset);
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||||
}
|
camera_.keyboardCallback(Camera::LEFT, deltaTime);
|
||||||
|
}
|
||||||
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||||
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
camera_.keyboardCallback(Camera::RIGHT, deltaTime);
|
||||||
if (!self) {
|
}
|
||||||
return;
|
lightMode_ = glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS;
|
||||||
}
|
}
|
||||||
self->onScrollRoll(xoffset, yoffset);
|
|
||||||
}
|
static void mouseCallback(GLFWwindow* window, double x, double y) {
|
||||||
|
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||||
void onScrollRoll(double xoffset, double yoffset) {
|
if (!self) {
|
||||||
camera_.mouseScrollCallback(yoffset);
|
return;
|
||||||
}
|
}
|
||||||
|
self->onMouseMove(x, y);
|
||||||
private:
|
}
|
||||||
std::unique_ptr<LOpenGLProgram> bgProg_;
|
|
||||||
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
|
void onMouseMove(double x, double y) {
|
||||||
std::string bgFragShaderPath_;
|
float xpos = static_cast<float>(x);
|
||||||
std::string bgVertShaderPath_;
|
float ypos = static_cast<float>(y);
|
||||||
unsigned int bgVao_ = 0;
|
|
||||||
unsigned int bgVbo_ = 0;
|
if (camera_.firstMouse()) {
|
||||||
|
lastX_ = x;
|
||||||
std::unique_ptr<LOpenGLProgram> panelProg_;
|
lastY_ = y;
|
||||||
std::string panelVertShaderPath_;
|
camera_.triggleFirstMouse();
|
||||||
std::string panelFragShaderPath_;
|
}
|
||||||
unsigned int panelVao_ = 0;
|
|
||||||
unsigned int panelVbo_ = 0;
|
float xoffset = xpos - lastX_;
|
||||||
unsigned int panelIbo_ = 0;
|
float yoffset = lastY_ - ypos;
|
||||||
float panelWidth_ = 0.25;
|
|
||||||
float panelHeight_ = 0.35;
|
lastX_ = xpos;
|
||||||
float panelDepth_ = 0.05;
|
lastY_ = ypos;
|
||||||
|
|
||||||
std::unique_ptr<LOpenGLProgram> heatmapProg_;
|
camera_.mouseMoveCallback(xoffset, yoffset);
|
||||||
std::string heatmapVertShaderPath_;
|
}
|
||||||
std::string heatmapFragShaderPath_;
|
|
||||||
unsigned int heatmapVao_ = 0;
|
static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||||
unsigned int heatmapVbo_ = 0;
|
auto* self = static_cast<GLWidget*>(glfwGetWindowUserPointer(window));
|
||||||
|
if (!self) {
|
||||||
ViewPort viewport_{800, 600};
|
return;
|
||||||
Camera camera_{glm::vec3(0.0f, 0.0f, 2.0f)};
|
}
|
||||||
|
self->onScrollRoll(xoffset, yoffset);
|
||||||
bool firstMouse_ = true;
|
}
|
||||||
float lastX_;
|
|
||||||
float lastY_;
|
void onScrollRoll(double xoffset, double yoffset) {
|
||||||
float lastFrame_ = 0.0f;
|
camera_.mouseScrollCallback(yoffset);
|
||||||
bool lightMode_ = false;
|
}
|
||||||
|
|
||||||
bool deinit_;
|
private:
|
||||||
};
|
std::unique_ptr<LOpenGLProgram> bgProg_;
|
||||||
|
std::unique_ptr<GLFWwindow, std::function<void(GLFWwindow*)>> window_;
|
||||||
int main() {
|
std::string bgFragShaderPath_;
|
||||||
{
|
std::string bgVertShaderPath_;
|
||||||
GLWidget glw({800, 600});
|
unsigned int bgVao_ = 0;
|
||||||
glw.setBgShaderPath("../shader/bg.vert", "../shader/bg.frag");
|
unsigned int bgVbo_ = 0;
|
||||||
glw.setPanelShaderPath("../shader/panel.vert", "../shader/panel.frag");
|
|
||||||
glw.eventLoop();
|
std::unique_ptr<LOpenGLProgram> panelProg_;
|
||||||
}
|
std::string panelVertShaderPath_;
|
||||||
glfwTerminate();
|
std::string panelFragShaderPath_;
|
||||||
return 0;
|
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
|
#version 330 core
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
uniform vec2 uViewport;
|
uniform vec2 uViewport;
|
||||||
uniform float uMajorStep;
|
uniform float uMajorStep;
|
||||||
uniform float uMinorStep;
|
uniform float uMinorStep;
|
||||||
uniform bool uLightMode;
|
uniform bool uLightMode;
|
||||||
|
|
||||||
float gridLine(float stepPx) {
|
float gridLine(float stepPx) {
|
||||||
vec2 coord = gl_FragCoord.xy;
|
vec2 coord = gl_FragCoord.xy;
|
||||||
vec2 q = coord / max(stepPx, 1.0);
|
vec2 q = coord / max(stepPx, 1.0);
|
||||||
vec2 fw = max(fwidth(q), vec2(1e-6));
|
vec2 fw = max(fwidth(q), vec2(1e-6));
|
||||||
q = abs(fract(q - 0.5) - 0.5) / fw;
|
q = abs(fract(q - 0.5) - 0.5) / fw;
|
||||||
float line = 1.0 - min(min(q.x, q.y), 1.0);
|
float line = 1.0 - min(min(q.x, q.y), 1.0);
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 viewport = max(uViewport, vec2(1.0));
|
vec2 viewport = max(uViewport, vec2(1.0));
|
||||||
vec2 uv = gl_FragCoord.xy / viewport;
|
vec2 uv = gl_FragCoord.xy / viewport;
|
||||||
|
|
||||||
vec3 topCol, botCol;
|
vec3 topCol, botCol;
|
||||||
float majorStrength, minorStrength;
|
float majorStrength, minorStrength;
|
||||||
|
|
||||||
topCol = vec3(0.99, 0.99, 1.00);
|
topCol = vec3(0.99, 0.99, 1.00);
|
||||||
botCol = vec3(0.94, 0.95, 0.98);
|
botCol = vec3(0.94, 0.95, 0.98);
|
||||||
|
|
||||||
vec3 minorCol = vec3(0.80, 0.82, 0.87);
|
vec3 minorCol = vec3(0.80, 0.82, 0.87);
|
||||||
vec3 majorCol = vec3(0.70, 0.73, 0.80);
|
vec3 majorCol = vec3(0.70, 0.73, 0.80);
|
||||||
|
|
||||||
minorStrength = 0.28;
|
minorStrength = 0.28;
|
||||||
majorStrength = 0.45;
|
majorStrength = 0.45;
|
||||||
|
|
||||||
vec3 col = mix(botCol, topCol, uv.y);
|
vec3 col = mix(botCol, topCol, uv.y);
|
||||||
float minor = gridLine(max(uMinorStep, 1.0));
|
float minor = gridLine(max(uMinorStep, 1.0));
|
||||||
float major = gridLine(max(uMajorStep, 1.0));
|
float major = gridLine(max(uMajorStep, 1.0));
|
||||||
|
|
||||||
col = mix(col, minorCol, minorStrength * minor);
|
col = mix(col, minorCol, minorStrength * minor);
|
||||||
col = mix(col, majorCol, majorStrength * major);
|
col = mix(col, majorCol, majorStrength * major);
|
||||||
|
|
||||||
vec2 p = uv * 2.0 - 1.0;
|
vec2 p = uv * 2.0 - 1.0;
|
||||||
float v = clamp(1.0 - dot(p, p) * 0.12 , 0.0, 1.0);
|
float v = clamp(1.0 - dot(p, p) * 0.12 , 0.0, 1.0);
|
||||||
col *= mix(1.0, v, 0.35);
|
col *= mix(1.0, v, 0.35);
|
||||||
|
|
||||||
FragColor = vec4(col, 1.0);
|
FragColor = vec4(col, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec2 aPos;
|
layout(location = 0) in vec2 aPos;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,149 +1,149 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
in vec2 vUV;
|
in vec2 vUV;
|
||||||
in vec3 vWorldPos;
|
in vec3 vWorldPos;
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
uniform sampler2D uHeightTex;
|
uniform sampler2D uHeightTex;
|
||||||
uniform float uMinV;
|
uniform float uMinV;
|
||||||
uniform float uMaxV;
|
uniform float uMaxV;
|
||||||
uniform float uHeightScale;
|
uniform float uHeightScale;
|
||||||
uniform vec2 uTexelSize;
|
uniform vec2 uTexelSize;
|
||||||
uniform vec2 uPlaneSize;
|
uniform vec2 uPlaneSize;
|
||||||
uniform vec3 uCameraPos;
|
uniform vec3 uCameraPos;
|
||||||
uniform vec3 uLightDir;
|
uniform vec3 uLightDir;
|
||||||
uniform vec3 uColorZero;
|
uniform vec3 uColorZero;
|
||||||
uniform vec3 uColorLow;
|
uniform vec3 uColorLow;
|
||||||
uniform vec3 uColorMid;
|
uniform vec3 uColorMid;
|
||||||
uniform vec3 uColorHigh;
|
uniform vec3 uColorHigh;
|
||||||
|
|
||||||
float saturate(float x) {
|
float saturate(float x) {
|
||||||
return clamp(x, 0.0, 1.0);
|
return clamp(x, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
float value01(float v) {
|
float value01(float v) {
|
||||||
return saturate((v - uMinV) / max(uMaxV - uMinV, 1e-6));
|
return saturate((v - uMinV) / max(uMaxV - uMinV, 1e-6));
|
||||||
}
|
}
|
||||||
|
|
||||||
// float maxNeighborValue(vec2 uv) {
|
// float maxNeighborValue(vec2 uv) {
|
||||||
// vec2 texSizeF = 1.0 / uTexelSize;
|
// vec2 texSizeF = 1.0 / uTexelSize;
|
||||||
// ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
// ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||||
// ivec2 maxCoord = texSizeI - ivec2(1);
|
// ivec2 maxCoord = texSizeI - ivec2(1);
|
||||||
|
|
||||||
// vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
// vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||||
// ivec2 base = ivec2(floor(texelF));
|
// ivec2 base = ivec2(floor(texelF));
|
||||||
// ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
// ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||||
// ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
// ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||||
// ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
// ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||||
|
|
||||||
// float v00 = texelFetch(uHeightTex, base, 0).r;
|
// float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||||
// float v10 = texelFetch(uHeightTex, base10, 0).r;
|
// float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||||
// float v01 = texelFetch(uHeightTex, base01, 0).r;
|
// float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||||
// float v11 = texelFetch(uHeightTex, base11, 0).r;
|
// float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||||
|
|
||||||
// return max(max(v00, v10), max(v01, v11));
|
// return max(max(v00, v10), max(v01, v11));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
float maxNeighborValue(vec2 uv) {
|
float maxNeighborValue(vec2 uv) {
|
||||||
vec2 texSizeF = 1.0 / uTexelSize;
|
vec2 texSizeF = 1.0 / uTexelSize;
|
||||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||||
ivec2 maxCoord = texSizeI - ivec2(1);
|
ivec2 maxCoord = texSizeI - ivec2(1);
|
||||||
|
|
||||||
vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
vec2 texelF = clamp(uv * texSizeF, vec2(0.0), vec2(maxCoord));
|
||||||
ivec2 base = ivec2(floor(texelF));
|
ivec2 base = ivec2(floor(texelF));
|
||||||
ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
ivec2 base10 = min(base + ivec2(1, 0), maxCoord);
|
||||||
ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
ivec2 base01 = min(base + ivec2(0, 1), maxCoord);
|
||||||
ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
ivec2 base11 = min(base + ivec2(1, 1), maxCoord);
|
||||||
|
|
||||||
float v00 = texelFetch(uHeightTex, base, 0).r;
|
float v00 = texelFetch(uHeightTex, base, 0).r;
|
||||||
float v10 = texelFetch(uHeightTex, base10, 0).r;
|
float v10 = texelFetch(uHeightTex, base10, 0).r;
|
||||||
float v01 = texelFetch(uHeightTex, base01, 0).r;
|
float v01 = texelFetch(uHeightTex, base01, 0).r;
|
||||||
float v11 = texelFetch(uHeightTex, base11, 0).r;
|
float v11 = texelFetch(uHeightTex, base11, 0).r;
|
||||||
|
|
||||||
return max(max(v00, v10), max(v01, v11));
|
return max(max(v00, v10), max(v01, v11));
|
||||||
}
|
}
|
||||||
|
|
||||||
ivec2 texSizeI(vec2 texelSize) {
|
ivec2 texSizeI(vec2 texelSize) {
|
||||||
vec2 texSizeF = 1.0 / texelSize;
|
vec2 texSizeF = 1.0 / texelSize;
|
||||||
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
ivec2 texSizeI = ivec2(max(vec2(1.0), floor(texSizeF + 0.5)));
|
||||||
return texSizeI;
|
return texSizeI;
|
||||||
}
|
}
|
||||||
|
|
||||||
ivec2 uvToTexel(vec2 uv, vec2 texelSize) {
|
ivec2 uvToTexel(vec2 uv, vec2 texelSize) {
|
||||||
ivec2 sizeI = texSizeI(texelSize);
|
ivec2 sizeI = texSizeI(texelSize);
|
||||||
ivec2 maxCoord = sizeI - ivec2(1);
|
ivec2 maxCoord = sizeI - ivec2(1);
|
||||||
vec2 texelF = clamp(uv * vec2(maxCoord), vec2(0.0), vec2(maxCoord));
|
vec2 texelF = clamp(uv * vec2(maxCoord), vec2(0.0), vec2(maxCoord));
|
||||||
return ivec2(floor(texelF));
|
return ivec2(floor(texelF));
|
||||||
}
|
}
|
||||||
|
|
||||||
float maxNeighborValueK(vec2 uv, int radius) {
|
float maxNeighborValueK(vec2 uv, int radius) {
|
||||||
radius = clamp(radius, 0, 4);
|
radius = clamp(radius, 0, 4);
|
||||||
ivec2 sizeI = texSizeI(uTexelSize);
|
ivec2 sizeI = texSizeI(uTexelSize);
|
||||||
ivec2 maxCoord = sizeI - ivec2(1);
|
ivec2 maxCoord = sizeI - ivec2(1);
|
||||||
ivec2 p = uvToTexel(uv, uTexelSize);
|
ivec2 p = uvToTexel(uv, uTexelSize);
|
||||||
|
|
||||||
float vmax = -1e30;
|
float vmax = -1e30;
|
||||||
|
|
||||||
for (int i = -4; i <= 4; ++i) {
|
for (int i = -4; i <= 4; ++i) {
|
||||||
for (int j = -4; j <= 4; ++j) {
|
for (int j = -4; j <= 4; ++j) {
|
||||||
if (abs(i) > radius || abs(j) > radius) continue;
|
if (abs(i) > radius || abs(j) > radius) continue;
|
||||||
ivec2 c = clamp(p + ivec2(i, j), ivec2(0), maxCoord);
|
ivec2 c = clamp(p + ivec2(i, j), ivec2(0), maxCoord);
|
||||||
float v = texelFetch(uHeightTex, c, 0).r;
|
float v = texelFetch(uHeightTex, c, 0).r;
|
||||||
vmax = max(vmax, v);
|
vmax = max(vmax, v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return vmax;
|
return vmax;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 colorRamp(float t) {
|
vec3 colorRamp(float t) {
|
||||||
if (t < 0.5) {
|
if (t < 0.5) {
|
||||||
return mix(uColorLow, uColorMid, t / 0.5);
|
return mix(uColorLow, uColorMid, t / 0.5);
|
||||||
}
|
}
|
||||||
return mix(uColorMid, uColorHigh, (t - 0.5) / 0.5);
|
return mix(uColorMid, uColorHigh, (t - 0.5) / 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 colorRampWithZero(float t) {
|
vec3 colorRampWithZero(float t) {
|
||||||
const float zeroW = 0.005;
|
const float zeroW = 0.005;
|
||||||
float blend = smoothstep(0.0, zeroW, t);
|
float blend = smoothstep(0.0, zeroW, t);
|
||||||
float tRamp = saturate((t - zeroW) / max(1.0 - zeroW, 1e-6));
|
float tRamp = saturate((t - zeroW) / max(1.0 - zeroW, 1e-6));
|
||||||
vec3 ramp = colorRamp(tRamp);
|
vec3 ramp = colorRamp(tRamp);
|
||||||
return mix(uColorZero, ramp, blend);
|
return mix(uColorZero, ramp, blend);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float vC = texture(uHeightTex, vUV).r;
|
float vC = texture(uHeightTex, vUV).r;
|
||||||
bool isZero = abs(vC) <= 1e-6;
|
bool isZero = abs(vC) <= 1e-6;
|
||||||
float t = value01(vC);
|
float t = value01(vC);
|
||||||
float m = maxNeighborValue(vUV);
|
float m = maxNeighborValue(vUV);
|
||||||
float eps = max(1e-4, 0.001 * (uMaxV - uMinV));
|
float eps = max(1e-4, 0.001 * (uMaxV - uMinV));
|
||||||
float force = smoothstep(uMaxV - eps, uMaxV, m);
|
float force = smoothstep(uMaxV - eps, uMaxV, m);
|
||||||
t = max(t, force);
|
t = max(t, force);
|
||||||
vec3 base = isZero ? uColorZero : colorRampWithZero(t);
|
vec3 base = isZero ? uColorZero : colorRampWithZero(t);
|
||||||
|
|
||||||
float vL = texture(uHeightTex, vUV - vec2(uTexelSize.x, 0.0)).r;
|
float vL = texture(uHeightTex, vUV - vec2(uTexelSize.x, 0.0)).r;
|
||||||
float vR = 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 vD = texture(uHeightTex, vUV - vec2(0.0, uTexelSize.y)).r;
|
||||||
float vU = 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 hL = value01(vL) * uHeightScale;
|
||||||
float hR = value01(vR) * uHeightScale;
|
float hR = value01(vR) * uHeightScale;
|
||||||
float hD = value01(vD) * uHeightScale;
|
float hD = value01(vD) * uHeightScale;
|
||||||
float hU = value01(vU) * uHeightScale;
|
float hU = value01(vU) * uHeightScale;
|
||||||
|
|
||||||
float dx = max(1e-6, uPlaneSize.x * uTexelSize.x);
|
float dx = max(1e-6, uPlaneSize.x * uTexelSize.x);
|
||||||
float dy = max(1e-6, uPlaneSize.y * uTexelSize.y);
|
float dy = max(1e-6, uPlaneSize.y * uTexelSize.y);
|
||||||
float dHx = (hR - hL) / (2.0 * dx);
|
float dHx = (hR - hL) / (2.0 * dx);
|
||||||
float dHy = (hU - hD) / (2.0 * dy);
|
float dHy = (hU - hD) / (2.0 * dy);
|
||||||
|
|
||||||
vec3 N = normalize(vec3(-dHx, -dHy, -1.0));
|
vec3 N = normalize(vec3(-dHx, -dHy, -1.0));
|
||||||
vec3 L = normalize(uLightDir);
|
vec3 L = normalize(uLightDir);
|
||||||
vec3 V = normalize(uCameraPos - vWorldPos);
|
vec3 V = normalize(uCameraPos - vWorldPos);
|
||||||
vec3 H = normalize(L + V);
|
vec3 H = normalize(L + V);
|
||||||
|
|
||||||
float diff = saturate(dot(N, L));
|
float diff = saturate(dot(N, L));
|
||||||
float spec = pow(saturate(dot(N, H)), 24.0);
|
float spec = pow(saturate(dot(N, H)), 24.0);
|
||||||
|
|
||||||
vec3 col = base * (0.50 + 0.50 * diff);
|
vec3 col = base * (0.50 + 0.50 * diff);
|
||||||
col += vec3(1.0) * spec * 0.18;
|
col += vec3(1.0) * spec * 0.18;
|
||||||
FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
layout(location = 0) in vec3 aPos;
|
layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec2 aUV;
|
layout(location = 1) in vec2 aUV;
|
||||||
|
|
||||||
out vec2 vUV;
|
out vec2 vUV;
|
||||||
out vec3 vWorldPos;
|
out vec3 vWorldPos;
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform sampler2D uHeightTex;
|
uniform sampler2D uHeightTex;
|
||||||
uniform float uMinV;
|
uniform float uMinV;
|
||||||
uniform float uMaxV;
|
uniform float uMaxV;
|
||||||
uniform float uHeightScale;
|
uniform float uHeightScale;
|
||||||
uniform float uBaseZ;
|
uniform float uBaseZ;
|
||||||
|
|
||||||
float value01(float v) {
|
float value01(float v) {
|
||||||
return clamp((v - uMinV) / max(uMaxV - uMinV, 1e-6), 0.0, 1.0);
|
return clamp((v - uMinV) / max(uMaxV - uMinV, 1e-6), 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vUV = aUV;
|
vUV = aUV;
|
||||||
float v = texture(uHeightTex, aUV).r;
|
float v = texture(uHeightTex, aUV).r;
|
||||||
float h = value01(v) * uHeightScale;
|
float h = value01(v) * uHeightScale;
|
||||||
vec3 world = aPos + vec3(0.0, 0.0, uBaseZ + h);
|
vec3 world = aPos + vec3(0.0, 0.0, uBaseZ + h);
|
||||||
vWorldPos = world;
|
vWorldPos = world;
|
||||||
gl_Position = projection * view * model * vec4(world, 1.0);
|
gl_Position = projection * view * model * vec4(world, 1.0);
|
||||||
}
|
}
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in vec3 vWorldPos;
|
in vec3 vWorldPos;
|
||||||
in vec3 vWorldNormal;
|
in vec3 vWorldNormal;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
uniform bool uLightMode;
|
uniform bool uLightMode;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec3 N = normalize(vWorldNormal);
|
vec3 N = normalize(vWorldNormal);
|
||||||
float isTop = step(0.75, N.y);
|
float isTop = step(0.75, N.y);
|
||||||
|
|
||||||
vec3 topBase;
|
vec3 topBase;
|
||||||
vec3 sideBase;
|
vec3 sideBase;
|
||||||
|
|
||||||
if (uLightMode) {
|
if (uLightMode) {
|
||||||
// 浅色主题 偏亮的工业灰
|
// 浅色主题 偏亮的工业灰
|
||||||
topBase = vec3(0.78, 0.80, 0.84);
|
topBase = vec3(0.78, 0.80, 0.84);
|
||||||
sideBase = vec3(0.68, 0.70, 0.74);
|
sideBase = vec3(0.68, 0.70, 0.74);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 深色主题 偏暗的工业灰
|
// 深色主题 偏暗的工业灰
|
||||||
topBase = vec3(0.30, 0.31, 0.32);
|
topBase = vec3(0.30, 0.31, 0.32);
|
||||||
sideBase = vec3(0.27, 0.28, 0.29);
|
sideBase = vec3(0.27, 0.28, 0.29);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 baseColor = mix(sideBase, topBase, isTop);
|
vec3 baseColor = mix(sideBase, topBase, isTop);
|
||||||
FragColor = vec4(clamp(baseColor, 0.0, 1.0), 1.0);
|
FragColor = vec4(clamp(baseColor, 0.0, 1.0), 1.0);
|
||||||
}
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 aPos;
|
layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec3 aN;
|
layout(location = 1) in vec3 aN;
|
||||||
|
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
|
|
||||||
out vec3 vWorldPos;
|
out vec3 vWorldPos;
|
||||||
out vec3 vWorldNormal;
|
out vec3 vWorldNormal;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 worldPos = model * vec4(aPos, 1.0);
|
vec4 worldPos = model * vec4(aPos, 1.0);
|
||||||
mat3 normalMat = transpose(inverse(mat3(model)));
|
mat3 normalMat = transpose(inverse(mat3(model)));
|
||||||
vWorldPos = worldPos.xyz;
|
vWorldPos = worldPos.xyz;
|
||||||
vWorldNormal = normalize(normalMat * aN);
|
vWorldNormal = normalize(normalMat * aN);
|
||||||
gl_Position = projection * view * worldPos;
|
gl_Position = projection * view * worldPos;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user