添加纯OpenGL实现demo
This commit is contained in:
26
test/onlygl/CMakeLists.txt
Normal file
26
test/onlygl/CMakeLists.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(base-project)
|
||||
find_package(X11 REQUIRED)
|
||||
add_executable(
|
||||
${PROJECT_NAME}
|
||||
main.cpp
|
||||
camera.h
|
||||
sensor.h
|
||||
glad.c
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE
|
||||
libglfw3.a
|
||||
${X11_LIBRARIES}
|
||||
${OPENGL_LIBRARIES}
|
||||
GL
|
||||
Xrandr
|
||||
Xi
|
||||
Xinerama
|
||||
Xcursor
|
||||
pthread
|
||||
dl
|
||||
)
|
||||
109
test/onlygl/camera.h
Normal file
109
test/onlygl/camera.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <cmath>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/ext/matrix_float4x4.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <glm/geometric.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
|
||||
enum CameraMovement {
|
||||
FORWARD,
|
||||
BACKWARD,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
};
|
||||
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
glm::vec3 position_;
|
||||
glm::vec3 front_;
|
||||
glm::vec3 up_;
|
||||
glm::vec3 right_;
|
||||
glm::vec3 worldup_;
|
||||
|
||||
float yaw_;
|
||||
float pitch_;
|
||||
float movement_speed_;
|
||||
float mouse_sensitivity_;
|
||||
float zoom_;
|
||||
|
||||
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 = YAW, float pitch = PITCH) :
|
||||
front_(glm::vec3(0.0f, 0.0f, -1.0f)),
|
||||
movement_speed_(SPEED),
|
||||
mouse_sensitivity_(SENSITIVITY),
|
||||
zoom_(ZOOM) {
|
||||
position_ = position;
|
||||
worldup_ = up;
|
||||
yaw_ = yaw;
|
||||
pitch_ = pitch;
|
||||
update_camera_vectors();
|
||||
}
|
||||
|
||||
Camera(float posx, float posy, float posz, float upx, float upy, float upz, float yaw, float pitch) :
|
||||
front_(glm::vec3(0.0f, 0.0f, -1.0f))
|
||||
, mouse_sensitivity_(SENSITIVITY),
|
||||
zoom_(ZOOM){
|
||||
position_ = glm::vec3(posx, posy, posz);
|
||||
worldup_ = glm::vec3(upx, upy, upz);
|
||||
yaw_ = yaw;
|
||||
pitch_ = pitch;
|
||||
update_camera_vectors();
|
||||
}
|
||||
|
||||
glm::mat4 get_view_matrix() {
|
||||
return glm::lookAt(position_, position_ + front_, up_);
|
||||
}
|
||||
|
||||
void process_keyboard(CameraMovement direction, float deltatime) {
|
||||
float velocity = movement_speed_ * deltatime;
|
||||
if (direction == FORWARD) {
|
||||
position_ += front_ * velocity;
|
||||
}
|
||||
if (direction == BACKWARD) {
|
||||
position_ -= front_ * velocity;
|
||||
}
|
||||
if (direction == LEFT) {
|
||||
position_ -= right_ * velocity;
|
||||
}
|
||||
if (direction == RIGHT) {
|
||||
position_ += right_ * velocity;
|
||||
}
|
||||
|
||||
position_.y = 0.0f;
|
||||
}
|
||||
|
||||
void process_mouse_movement(float xoffset, float yoffset, GLboolean constrain_pitch = true) {
|
||||
|
||||
}
|
||||
|
||||
void process_mouse_scroll(float yoffset) {
|
||||
zoom_ -= (float)yoffset;
|
||||
if (zoom_ < 1.0f) {
|
||||
zoom_ = 1.0f;
|
||||
}
|
||||
if (zoom_ > 45.0f) {
|
||||
zoom_ = 45.0f;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void update_camera_vectors() {
|
||||
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_));
|
||||
}
|
||||
};
|
||||
1140
test/onlygl/glad.c
Normal file
1140
test/onlygl/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
203
test/onlygl/main.cpp
Normal file
203
test/onlygl/main.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
#include <cstddef>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <iostream>
|
||||
#include "camera.h"
|
||||
#include "myshader.hh"
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float yaw = -90.0f;
|
||||
float pitch = 0.0f;
|
||||
float fov = 45.0f;
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 3.0));
|
||||
|
||||
int dot_rows = 3;
|
||||
int dot_cols = 4;
|
||||
float panel_width = 1.2f;
|
||||
float panel_height = 0.08f;
|
||||
float panel_deep = 0.08f;
|
||||
|
||||
Shader bg_shader();
|
||||
Shader panel_shader();
|
||||
Shader dots_shader();
|
||||
|
||||
unsigned int panel_vao = 0;
|
||||
unsigned int panel_vbo = 0;
|
||||
unsigned int panel_ibo = 0;
|
||||
|
||||
unsigned int dots_vao = 0;
|
||||
unsigned int dots_vbo = 0;
|
||||
unsigned int instance_vbo = 0;
|
||||
|
||||
unsigned int bg_vao = 0;
|
||||
unsigned int bg_vbo = 0;
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void process_input(GLFWwindow* window) {
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
}
|
||||
|
||||
GLFWwindow* glfw_init() {
|
||||
glfwInit();
|
||||
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
|
||||
GLFWwindow* window = glfwCreateWindow(800, 600, "Tactile Module Test", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
std::cout << "Failed to create GLFW window" << std::endl;
|
||||
glfwTerminate();
|
||||
return NULL;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
glViewport(0, 0, 800, 600);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
void glfw_window_loop(GLFWwindow* window) {
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
process_input(window);
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_context() {
|
||||
if (panel_vao) {
|
||||
glDeleteVertexArrays(1, &panel_vao);
|
||||
}
|
||||
if (panel_vbo) {
|
||||
glDeleteBuffers(1, &panel_vbo);
|
||||
}
|
||||
if (panel_ibo) {
|
||||
glDeleteBuffers(1, &panel_ibo);
|
||||
}
|
||||
if (dots_vao) {
|
||||
glDeleteVertexArrays(1, &dots_vao);
|
||||
}
|
||||
if (dots_vbo) {
|
||||
glDeleteBuffers(1, &dots_vbo);
|
||||
}
|
||||
}
|
||||
|
||||
void init_panel_geometry() {
|
||||
if (panel_ibo) {
|
||||
glDeleteBuffers(1, &panel_ibo);
|
||||
panel_ibo = 0;
|
||||
}
|
||||
if (panel_vbo) {
|
||||
glDeleteBuffers(1, &panel_vbo);
|
||||
panel_vbo = 0;
|
||||
}
|
||||
if (panel_vao) {
|
||||
glDeleteVertexArrays(1, &panel_vao);
|
||||
panel_vao = 0;
|
||||
}
|
||||
|
||||
const float y = panel_height * 0.5f;
|
||||
const float hw = panel_width * 0.5f;
|
||||
const float hd = panel_deep * 0.5;
|
||||
using V = struct {
|
||||
float x, y, z;
|
||||
float nx, ny, nz;
|
||||
};
|
||||
V verts[24] = {
|
||||
// +Y 顶面 (normal 0, +1, 0)
|
||||
{-hw, +y, -hd, 0, +1, 0}, // 0
|
||||
{+hw, +y, -hd, 0, +1, 0}, // 1
|
||||
{+hw, +y, +hd, 0, +1, 0}, // 2
|
||||
{-hw, +y, +hd, 0, +1, 0}, // 3
|
||||
|
||||
// +Z 前面 (normal 0, 0, +1)
|
||||
{-hw, +y, +hd, 0, 0, +1}, // 4
|
||||
{+hw, +y, +hd, 0, 0, +1}, // 5
|
||||
{+hw, -y, +hd, 0, 0, +1}, // 6
|
||||
{-hw, -y, +hd, 0, 0, +1}, // 7
|
||||
|
||||
// -Y 底面 (normal 0, -1, 0)
|
||||
{-hw, -y, +hd, 0, -1, 0}, // 8
|
||||
{+hw, -y, +hd, 0, -1, 0}, // 9
|
||||
{+hw, -y, -hd, 0, -1, 0}, // 10
|
||||
{-hw, -y, -hd, 0, -1, 0}, // 11
|
||||
|
||||
// -Z 后面 (normal 0, 0, -1)
|
||||
{+hw, +y, -hd, 0, 0, -1}, // 12
|
||||
{-hw, +y, -hd, 0, 0, -1}, // 13
|
||||
{-hw, -y, -hd, 0, 0, -1}, // 14
|
||||
{+hw, -y, -hd, 0, 0, -1}, // 15
|
||||
|
||||
// -X 左面 (normal -1, 0, 0)
|
||||
{-hw, +y, -hd, -1, 0, 0}, // 16
|
||||
{-hw, +y, +hd, -1, 0, 0}, // 17
|
||||
{-hw, -y, +hd, -1, 0, 0}, // 18
|
||||
{-hw, -y, -hd, -1, 0, 0}, // 19
|
||||
|
||||
// +X 右面 (normal +1, 0, 0)
|
||||
{+hw, +y, +hd, +1, 0, 0}, // 20
|
||||
{+hw, +y, -hd, +1, 0, 0}, // 21
|
||||
{+hw, -y, -hd, +1, 0, 0}, // 22
|
||||
{+hw, -y, +hd, +1, 0, 0}, // 23
|
||||
};
|
||||
|
||||
unsigned int idx[36] = {
|
||||
0, 1, 2, 0, 2, 3, // top
|
||||
4, 5, 6, 4, 6, 7, // front
|
||||
8, 9, 10, 8, 10,11, // bottom
|
||||
12,13,14, 12,14,15, // back
|
||||
16,17,18, 16,18,19, // left
|
||||
20,21,22, 20,22,23 // right
|
||||
};
|
||||
int panel_index_count = 36;
|
||||
glGenVertexArrays(1, &panel_vao);
|
||||
glBindVertexArray(panel_vao);
|
||||
glGenBuffers(1, &panel_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, panel_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
||||
glGenBuffers(1, &panel_ibo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, panel_ibo);
|
||||
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_FLOAT, sizeof(V), (void*)(3 * sizeof(float)));
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void set_panel_size(float w, float h, float d) {
|
||||
panel_width = w;
|
||||
panel_height = h;
|
||||
panel_deep = d;
|
||||
}
|
||||
|
||||
void set_spec(int rows, int cols, float pitch);
|
||||
|
||||
int main() {
|
||||
GLFWwindow* window = glfw_init();
|
||||
|
||||
if (window == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
glfw_window_loop(window);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
94
test/onlygl/myshader.hh
Normal file
94
test/onlygl/myshader.hh
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <iterator>
|
||||
#include <string.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
// 程序ID
|
||||
unsigned int ID;
|
||||
// 构造器读取并构建着色器
|
||||
Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
vShaderFile.open(vertexPath);
|
||||
fShaderFile.open(fragmentPath);
|
||||
std::stringstream vShaderStream, fShaderStream;
|
||||
vShaderStream << vShaderFile.rdbuf();
|
||||
fShaderStream << fShaderFile.rdbuf();
|
||||
vShaderFile.close();
|
||||
fShaderFile.close();
|
||||
vertexCode = vShaderStream.str();
|
||||
fragmentCode = fShaderStream.str();
|
||||
}
|
||||
catch(std::ifstream::failure e) {
|
||||
std::cout << "Error::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
||||
}
|
||||
|
||||
const char* vShaderCode = vertexCode.c_str();
|
||||
const char* fShaderCode = fragmentCode.c_str();
|
||||
|
||||
// 编译着色器
|
||||
unsigned int vertex, fragment;
|
||||
int success;
|
||||
char infoLog[512];
|
||||
|
||||
// 顶点着色器
|
||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||
glCompileShader(vertex);
|
||||
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
// 片段着色器
|
||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||
glCompileShader(fragment);
|
||||
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
ID = glCreateProgram();
|
||||
glAttachShader(ID, vertex);
|
||||
glAttachShader(ID, fragment);
|
||||
glLinkProgram(ID);
|
||||
glGetProgramiv(ID, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetShaderInfoLog(ID, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
// 使用/激活程序
|
||||
void use() {
|
||||
glUseProgram(ID);
|
||||
}
|
||||
// uniform工具函数
|
||||
void setBool(const std::string& name, bool value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
||||
}
|
||||
void setInt(const std::string& name, int value) const {
|
||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
void setFloat(const std::string& name, float value) const {
|
||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||
}
|
||||
};
|
||||
1
test/onlygl/sensor.h
Normal file
1
test/onlygl/sensor.h
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
37
test/onlygl/shaders/bg.frag
Normal file
37
test/onlygl/shaders/bg.frag
Normal file
@@ -0,0 +1,37 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec2 uViewport;
|
||||
uniform float uMinorStep;
|
||||
uniform float uMajorStep;
|
||||
|
||||
float gridLine(float stepPx) {
|
||||
// 当前坐标像素
|
||||
vec2 coord = gl_FragCoord.xy;
|
||||
vec2 q = coord / stepPx;
|
||||
|
||||
vec2 g = abs(fract(q - 0.5) - 0.5) / fwidth(q);
|
||||
float line = 1.0 - min(min(g.x, g.y), 1.0);
|
||||
return line;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 viewport = max(uViewport, vec2(1.0));
|
||||
vec2 uv = gl_FragCoord.xy / viewport;
|
||||
|
||||
vec3 topCol = vec3(0.99, 0.99, 1.00);
|
||||
vec3 botCol = vec3(0.94, 0.95, 0.98);
|
||||
vec3 col = mix(botCol, topCol, uv.y);
|
||||
|
||||
float minor = gridLine(max(uMinorStep, 1.0));
|
||||
float major = gridLine(max(uMajorStep, 1.0));
|
||||
vec3 minorCol = vec3(0.80, 0.82, 0.87);
|
||||
vec3 majorcol = vec3(0.70, 0.73, 0.80);
|
||||
col = mix(col, minorCol, minor * 0.22);
|
||||
col = mix(col, majorcol, major * 0.35);
|
||||
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);
|
||||
}
|
||||
6
test/onlygl/shaders/bg.vert
Normal file
6
test/onlygl/shaders/bg.vert
Normal file
@@ -0,0 +1,6 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec2 aPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
7988
test/onlygl/stb_image.h
Normal file
7988
test/onlygl/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
2
test/onlygl/stb_image_wrap.h
Normal file
2
test/onlygl/stb_image_wrap.h
Normal file
@@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
Reference in New Issue
Block a user