tesseract wireframe and project rename/refactor

This commit is contained in:
2018-12-19 07:36:33 -05:00
parent 7b85d1e4db
commit 83d0ce5ef3
14 changed files with 282 additions and 207 deletions

1
.idea/.name generated
View File

@@ -1 +0,0 @@
gl_template

2
.idea/misc.xml generated
View File

@@ -4,7 +4,7 @@
<component name="CidrRootsConfiguration">
<sourceRoots>
<file path="$PROJECT_DIR$/framework" />
<file path="$PROJECT_DIR$/glapp" />
<file path="$PROJECT_DIR$/simplex" />
</sourceRoots>
<libraryRoots>
<file path="$PROJECT_DIR$/vendor" />

View File

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(gl_template)
project(simplex)
set(CMAKE_CXX_STANDARD 17)
@@ -15,4 +15,4 @@ option(GLM_TEST_ENABLE OFF)
add_subdirectory(vendor/glm)
add_subdirectory(framework)
add_subdirectory(glapp)
add_subdirectory(simplex)

View File

@@ -60,6 +60,8 @@ protected:
virtual void display() {}
virtual void update() {}
virtual void deinit() {}
public:

View File

@@ -190,10 +190,12 @@ int App::run() {
_time += (_glfw_time - _last_glfw_time) * _rate;
update();
display();
_last_time = _time;
_last_glfw_time = _glfw_time;
_frame++;
glfwPollEvents();
};

View File

@@ -1,9 +0,0 @@
#version 440 core
smooth in vec3 color;
out vec4 fcolor;
void main() {
fcolor = vec4(color, 1);
}

View File

@@ -1,17 +0,0 @@
#version 440 core
layout(location=0) in vec3 vCol;
layout(location=1) in vec2 vPos;
smooth out vec3 color;
layout(std140, binding=3) uniform Matrices {
mat4 proj;
mat4 view;
mat4 model;
};
void main() {
gl_Position = proj * view * model * vec4(vPos, 0, 1);
color = vCol;
}

View File

@@ -1,176 +0,0 @@
#include <framework.h>
#include <util.h>
#include "glmutil.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 Vertex {
glm::vec2 pos;
glm::vec3 col;
};
struct Matrices {
glm::mat4 proj;
glm::mat4 view;
glm::mat4 model;
};
class GLApp : public App {
private:
std::vector<Vertex> vertices;
Matrices mats{
glm::identity<glm::mat4>(),
glm::identity<glm::mat4>(),
glm::identity<glm::mat4>(),
};
glm::vec2 mouse_root = glm::vec2(0);
glm::vec2 angle_root = glm::vec2(0);
glm::vec2 angles = glm::vec2(0);
GLuint circle_vert_arr = 0;
GLuint circle_vert_buf = 0, matrix_buffer = 0;
GLuint vertex_shader = 0, fragment_shader = 0, program = 0;
GLuint vpos_location = 0, vcol_location = 0;
GLuint matrices_index = 0;
GLuint matrices_binding_point = 0;
protected:
void onKey(int key, int scan_code, int action, int mods) override {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
close();
}
void onMouseButton(int button, int action, int mods) override {
if (button == 0 && action == GLFW_PRESS) {
double x, y;
glfwGetCursorPos(getWindow(), &x, &y);
mouse_root = glm::vec2(x, y);
angle_root = angles;
}
}
void onCursorPos(double x, double y)
override {
if (glfwGetMouseButton(getWindow(), 0)) {
auto dx = (float) x - mouse_root.x;
auto dy = (float) y - mouse_root.y;
angles = angle_root + glm::vec2(dx, dy) / 100.f;
}
}
void init() override {
vertices.push_back({
glm::vec2(0), glm::vec3(1)
});
const int N = 60;
const float TAU = 6.28318f;
for (int i = 0; i <= N; ++i) {
float t = (float) i / N;
auto p = glm::vec2(t) + glm::vec2(0, 1) / 4.f;
auto c = glm::vec3(t) + glm::vec3(0, 1, 2) / 3.f;
vertices.push_back({
glm::cos(TAU * p),
glm::cos(TAU * c)
});
}
setTitle("Hello Square!");
glGenVertexArrays(1, &circle_vert_arr);
glBindVertexArray(circle_vert_arr);
glGenBuffers(1, &circle_vert_buf);
glBindBuffer(GL_ARRAY_BUFFER, circle_vert_buf);
util::bufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
program = util::buildProgram(false, {
vertex_shader = util::buildShader("shaders/main.vert"),
fragment_shader = util::buildShader("shaders/main.frag"),
});
glGenBuffers(1, &matrix_buffer);
vpos_location = (GLuint) glGetAttribLocation(program, "vPos");
vcol_location = (GLuint) glGetAttribLocation(program, "vCol");
matrices_index = (GLuint) glGetUniformBlockIndex(program, "Matrices");
glBindBufferBase(GL_UNIFORM_BUFFER, matrices_binding_point, matrix_buffer);
glUniformBlockBinding(program, matrices_index, matrices_binding_point);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, circle_vert_buf);
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) nullptr);
glEnableVertexAttribArray(vcol_location);
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void display()
override {
int width, height;
float ratio;
glfwGetFramebufferSize(getWindow(), &width, &height);
ratio = (float) width / height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
mats.proj = glm::ortho(-ratio, ratio, -1.f, 1.f);
mats.view = glmutil::scale(glm::vec3(0.9f)) * glmutil::eulerAngles(glm::vec3(angles.y, 0, angles.x));
mats.model = glmutil::rotation(getTime(), glm::vec3(0.0f, 0.0f, 1.0f));
glBindBuffer(GL_UNIFORM_BUFFER, matrix_buffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(Matrices), &mats, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindVertexArray(circle_vert_arr);
glUseProgram(program);
glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) vertices.size());
glBindVertexArray(0);
swapBuffers();
}
void deinit()
override {
glDeleteBuffers(1, &circle_vert_buf);
glDeleteVertexArrays(1, &circle_vert_arr);
glDeleteProgram(program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
}
public:
GLApp() : App(4, 4) {}
};
int main() {
GLApp app = GLApp();
app.launch();
}

View File

@@ -1,4 +1,4 @@
project(sample)
project(simplex)
add_executable(${PROJECT_NAME}
src/main.cpp)

80
simplex/include/rotor.h Normal file
View 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
View 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
View 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
View 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();
}