tesseract wireframe and project rename/refactor
This commit is contained in:
32
simplex/CMakeLists.txt
Normal file
32
simplex/CMakeLists.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
project(simplex)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
src/main.cpp)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
glad
|
||||
glm
|
||||
glfw
|
||||
framework)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
include)
|
||||
|
||||
set(SHADERS
|
||||
shaders/main.frag
|
||||
shaders/main.vert)
|
||||
add_custom_target(shaders DEPENDS ${SHADERS})
|
||||
|
||||
add_custom_command(
|
||||
TARGET shaders PRE_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/shaders/
|
||||
COMMENT "clearing shaders"
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET shaders POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders/ ${CMAKE_CURRENT_BINARY_DIR}/shaders/
|
||||
COMMENT "copying shaders"
|
||||
)
|
||||
|
||||
add_dependencies(${PROJECT_NAME} shaders)
|
||||
28
simplex/include/glmutil.h
Normal file
28
simplex/include/glmutil.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef GL_TEMPLATE_GLMUTILS_H
|
||||
#define GL_TEMPLATE_GLMUTILS_H
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
namespace glmutil {
|
||||
glm::mat4 rotation(float angle, glm::vec3 axis) {
|
||||
auto mat = glm::identity<glm::mat4>();
|
||||
return glm::rotate(mat, angle, axis);
|
||||
}
|
||||
|
||||
glm::mat4 scale(glm::vec3 v) {
|
||||
auto mat = glm::identity<glm::mat4>();
|
||||
return glm::scale(mat, v);
|
||||
}
|
||||
|
||||
glm::mat4 eulerAngles(glm::vec3 angles){
|
||||
auto mat = glm::identity<glm::mat4>();
|
||||
mat = glm::rotate(mat, angles.x, glm::vec3(1, 0, 0));
|
||||
mat = glm::rotate(mat, angles.y, glm::vec3(0, 1, 0));
|
||||
mat = glm::rotate(mat, angles.z, glm::vec3(0, 0, 1));
|
||||
return mat;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //GL_TEMPLATE_GLMUTILS_H
|
||||
80
simplex/include/rotor.h
Normal file
80
simplex/include/rotor.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef GL_TEMPLATE_ROTOR_H
|
||||
#define GL_TEMPLATE_ROTOR_H
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
glm::mat4 rotor_xy(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
+c, s, 0, 0,
|
||||
-s, c, 0, 0,
|
||||
+0, 0, 1, 0,
|
||||
+0, 0, 0, 1
|
||||
);
|
||||
}
|
||||
|
||||
glm::mat4 rotor_xz(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
+c, 0, s, 0,
|
||||
+0, 1, 0, 0,
|
||||
-s, 0, c, 0,
|
||||
+0, 0, 0, 1
|
||||
);
|
||||
}
|
||||
|
||||
glm::mat4 rotor_xw(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
+c, 0, 0, s,
|
||||
+0, 1, 0, 0,
|
||||
+0, 0, 1, 0,
|
||||
-s, 0, 0, c
|
||||
);
|
||||
}
|
||||
|
||||
glm::mat4 rotor_yz(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
1, +0, 0, 0,
|
||||
0, +c, s, 0,
|
||||
0, -s, c, 0,
|
||||
0, +0, 0, 1
|
||||
);
|
||||
}
|
||||
|
||||
glm::mat4 rotor_yw(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
1, +0, 0, 0,
|
||||
0, +c, 0, s,
|
||||
0, +0, 1, 0,
|
||||
0, -s, 0, c
|
||||
);
|
||||
}
|
||||
|
||||
glm::mat4 rotor_zw(float t) {
|
||||
float c = cos(t);
|
||||
float s = sin(t);
|
||||
|
||||
return glm::mat4(
|
||||
1, 0, +0, 0,
|
||||
0, 1, +0, 0,
|
||||
0, 0, +c, s,
|
||||
0, 0, -s, c
|
||||
);
|
||||
}
|
||||
|
||||
#endif //GL_TEMPLATE_ROTOR_H
|
||||
17
simplex/shaders/main.frag
Normal file
17
simplex/shaders/main.frag
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 440 core
|
||||
|
||||
out vec4 fcolor;
|
||||
|
||||
in vec4 pos;
|
||||
|
||||
vec3 hsv2rgb(vec3 c) {
|
||||
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
||||
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
|
||||
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 hsv = vec3(pos.w/3 + .6, 1, 1);
|
||||
|
||||
fcolor = vec4(hsv2rgb(hsv), 1);
|
||||
}
|
||||
16
simplex/shaders/main.vert
Normal file
16
simplex/shaders/main.vert
Normal file
@@ -0,0 +1,16 @@
|
||||
#version 440 core
|
||||
|
||||
layout(location=0) in vec4 pos4;
|
||||
|
||||
layout(std140, binding=1) uniform Matrices {
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
};
|
||||
|
||||
out vec4 pos;
|
||||
|
||||
void main() {
|
||||
pos = model * pos4;
|
||||
gl_Position = proj * view * vec4(pos.xyz, -pos.w + 2);
|
||||
}
|
||||
161
simplex/src/main.cpp
Normal file
161
simplex/src/main.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <framework.h>
|
||||
#include <util.h>
|
||||
|
||||
#include "glmutil.h"
|
||||
#include "rotor.h"
|
||||
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
struct Matrices {
|
||||
glm::mat4 model;
|
||||
glm::mat4 view;
|
||||
glm::mat4 proj;
|
||||
};
|
||||
|
||||
class GLApp : public App {
|
||||
std::vector<glm::vec4> cell_verts{};
|
||||
std::vector<unsigned> cell_elems{};
|
||||
Matrices matrices{};
|
||||
|
||||
GLuint cell_array{};
|
||||
GLuint cell_vert_buf{}, cell_elem_arr_buf{};
|
||||
GLuint wire_prog{};
|
||||
|
||||
GLuint matrix_buffer{};
|
||||
GLuint matrix_binding_point = 1;
|
||||
|
||||
void init() override {
|
||||
cell_verts = {
|
||||
glm::vec4(-0.5, -0.5, -0.5, -0.5),
|
||||
glm::vec4(-0.5, -0.5, -0.5, +0.5),
|
||||
glm::vec4(-0.5, -0.5, +0.5, -0.5),
|
||||
glm::vec4(-0.5, -0.5, +0.5, +0.5),
|
||||
glm::vec4(-0.5, +0.5, -0.5, -0.5),
|
||||
glm::vec4(-0.5, +0.5, -0.5, +0.5),
|
||||
glm::vec4(-0.5, +0.5, +0.5, -0.5),
|
||||
glm::vec4(-0.5, +0.5, +0.5, +0.5),
|
||||
glm::vec4(+0.5, -0.5, -0.5, -0.5),
|
||||
glm::vec4(+0.5, -0.5, -0.5, +0.5),
|
||||
glm::vec4(+0.5, -0.5, +0.5, -0.5),
|
||||
glm::vec4(+0.5, -0.5, +0.5, +0.5),
|
||||
glm::vec4(+0.5, +0.5, -0.5, -0.5),
|
||||
glm::vec4(+0.5, +0.5, -0.5, +0.5),
|
||||
glm::vec4(+0.5, +0.5, +0.5, -0.5),
|
||||
glm::vec4(+0.5, +0.5, +0.5, +0.5),
|
||||
};
|
||||
|
||||
cell_elems = {
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15,
|
||||
0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15,
|
||||
0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15,
|
||||
};
|
||||
|
||||
matrices = {
|
||||
glm::identity<glm::mat4>(),
|
||||
glm::identity<glm::mat4>(),
|
||||
glm::identity<glm::mat4>(),
|
||||
};
|
||||
|
||||
//region Shaders
|
||||
GLuint vs = util::buildShader(GL_VERTEX_SHADER, {"shaders/main.vert"});
|
||||
GLuint fs = util::buildShader(GL_FRAGMENT_SHADER, {"shaders/main.frag"});
|
||||
wire_prog = util::buildProgram(false, {vs, fs});
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
//endregion
|
||||
|
||||
//region Buffers
|
||||
glGenBuffers(1, &cell_vert_buf);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cell_vert_buf);
|
||||
util::bufferData(GL_ARRAY_BUFFER, cell_verts, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glGenBuffers(1, &cell_elem_arr_buf);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cell_elem_arr_buf);
|
||||
util::bufferData(GL_ELEMENT_ARRAY_BUFFER, cell_elems, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
glGenBuffers(1, &matrix_buffer);
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, matrix_binding_point, matrix_buffer);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, matrix_buffer);
|
||||
util::bufferData(GL_UNIFORM_BUFFER, matrices, GL_STREAM_DRAW);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
//endregion
|
||||
|
||||
//region Vertex Arrays
|
||||
glGenVertexArrays(1, &cell_array);
|
||||
glBindVertexArray(cell_array);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cell_vert_buf);
|
||||
auto pos4_loc = (GLuint) glGetAttribLocation(wire_prog, "pos4");
|
||||
glEnableVertexAttribArray(pos4_loc);
|
||||
glVertexAttribPointer(pos4_loc, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void *) nullptr);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cell_elem_arr_buf);
|
||||
glBindVertexArray(0);
|
||||
//endregion
|
||||
};
|
||||
|
||||
void update() override {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(getWindow(), &width, &height);
|
||||
float ratio = (float) width / height;
|
||||
|
||||
matrices.model = rotor_yw(getTime() / 5) * rotor_xz(getTime() / 2);
|
||||
|
||||
matrices.view = glm::lookAt(glm::vec3(0, 0, -2), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
|
||||
matrices.proj = glm::perspective(1.f, ratio, 0.1f, 10.0f);
|
||||
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, matrix_buffer);
|
||||
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(Matrices), &matrices);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
}
|
||||
|
||||
void display() override {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(getWindow(), &width, &height);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glPointSize(10);
|
||||
glLineWidth(2);
|
||||
|
||||
glEnable(GL_DEPTH_BUFFER_BIT);
|
||||
glUseProgram(wire_prog);
|
||||
glBindVertexArray(cell_array);
|
||||
glDrawElements(GL_LINES, (GLsizei) cell_elems.size(), GL_UNSIGNED_INT, nullptr);
|
||||
// glDrawArrays(GL_POINTS, 0, (GLsizei) cell_verts.size());
|
||||
glBindVertexArray(0);
|
||||
|
||||
glFinish();
|
||||
swapBuffers();
|
||||
}
|
||||
|
||||
void onKey(int key, int scan_code, int action, int mods) override {
|
||||
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) {
|
||||
glfwSetWindowShouldClose(getWindow(), true);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
GLApp() : App(4, 4) {}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
GLApp app = GLApp();
|
||||
app.launch();
|
||||
}
|
||||
Reference in New Issue
Block a user