rotating tesseract

This commit is contained in:
2018-12-20 20:23:08 -05:00
parent 1ee97c5154
commit 1e842b8dda
4 changed files with 80 additions and 34 deletions

View File

@@ -15,7 +15,7 @@ target_include_directories(${PROJECT_NAME}
include)
set(SHADERS
shaders/main.frag
shaders/wire.frag
shaders/main.vert)
add_custom_target(shaders DEPENDS ${SHADERS})

View File

@@ -13,7 +13,7 @@ vec3 hsv2rgb(vec3 c) {
void main() {
float h = pos.w / 3 + .6;
float s = 1;
float v = smoothstep(1, -1, pos.z);
float v = smoothstep(2, -2, pos.z);
fcolor = vec4(hsv2rgb(vec3(h, s, v)), 1);
}

19
simplex/shaders/wire.frag Normal file
View File

@@ -0,0 +1,19 @@
#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() {
float h = pos.w / 3 + .6;
float s = 1;
float v = smoothstep(2, -2, pos.z);
fcolor = vec4(hsv2rgb(vec3(h, s, v)), 1);
}

View File

@@ -22,6 +22,20 @@ struct Matrices {
glm::mat4 proj;
};
void push_cube(std::vector<unsigned> &inds, std::vector<unsigned> cube) {
std::vector<unsigned> cube_inds = {
cube[0], cube[1], cube[2], cube[4],
cube[1], cube[4], cube[5], cube[7],
cube[1], cube[2], cube[3], cube[7],
cube[2], cube[4], cube[6], cube[7],
cube[1], cube[2], cube[4], cube[7],
};
for (auto ind : cube_inds) {
inds.push_back(ind);
}
}
class GLApp : public App {
std::vector<glm::vec4> cell_verts{};
std::vector<unsigned> cell_elems{};
@@ -38,25 +52,33 @@ class GLApp : public App {
void init() override {
cell_verts = {
glm::vec4(+1, +1, +1, -.447) / 2.f,
glm::vec4(+1, -1, -1, -.447) / 2.f,
glm::vec4(-1, +1, -1, -.447) / 2.f,
glm::vec4(-1, -1, +1, -.447) / 2.f,
glm::vec4(+0, +0, +0, 1.788) / 2.f,
glm::vec4(+1, +1, +1, +1) / 2.f,
glm::vec4(+1, +1, +1, -1) / 2.f,
glm::vec4(+1, +1, -1, +1) / 2.f,
glm::vec4(+1, +1, -1, -1) / 2.f,
glm::vec4(+1, -1, +1, +1) / 2.f,
glm::vec4(+1, -1, +1, -1) / 2.f,
glm::vec4(+1, -1, -1, +1) / 2.f,
glm::vec4(+1, -1, -1, -1) / 2.f,
glm::vec4(-1, +1, +1, +1) / 2.f,
glm::vec4(-1, +1, +1, -1) / 2.f,
glm::vec4(-1, +1, -1, +1) / 2.f,
glm::vec4(-1, +1, -1, -1) / 2.f,
glm::vec4(-1, -1, +1, +1) / 2.f,
glm::vec4(-1, -1, +1, -1) / 2.f,
glm::vec4(-1, -1, -1, +1) / 2.f,
glm::vec4(-1, -1, -1, -1) / 2.f,
};
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,
0, 1, 2, 3,
0, 1, 2, 4,
0, 1, 3, 4,
0, 2, 3, 4,
1, 2, 3, 4,
};
cell_elems = {};
push_cube(cell_elems, {0, 1, 2, 3, 4, 5, 6, 7});
push_cube(cell_elems, {8, 9, 10, 11, 12, 13, 14, 15});
push_cube(cell_elems, {0, 1, 2, 3, 8, 9, 10, 11});
push_cube(cell_elems, {4, 5, 6, 7, 12, 13, 14, 15});
push_cube(cell_elems, {0, 1, 4, 5, 8, 9, 12, 13});
push_cube(cell_elems, {2, 3, 6, 7, 10, 11, 14, 15});
push_cube(cell_elems, {0, 2, 4, 6, 8, 10, 12, 14});
push_cube(cell_elems, {1, 3, 5, 7, 9, 11, 13, 15});
matrices = {
glm::identity<glm::mat4>(),
@@ -66,13 +88,17 @@ class GLApp : public App {
//region Shaders
GLuint main_vs = util::buildShader(GL_VERTEX_SHADER, {"shaders/main.vert"});
GLuint main_fs = util::buildShader(GL_FRAGMENT_SHADER, {"shaders/main.frag"});
GLuint sect_fs = util::buildShader(GL_FRAGMENT_SHADER, {"shaders/sect.frag"});
GLuint wire_fs = util::buildShader(GL_FRAGMENT_SHADER, {"shaders/wire.frag"});
GLuint sect_gs = util::buildShader(GL_GEOMETRY_SHADER, {"shaders/sect.geom"});
GLuint wire_gs = util::buildShader(GL_GEOMETRY_SHADER, {"shaders/wire.geom"});
wire_prog = util::buildProgram(false, {main_vs, main_fs, wire_gs});
sect_prog = util::buildProgram(false, {main_vs, main_fs, sect_gs});
wire_prog = util::buildProgram(false, {main_vs, wire_fs, wire_gs});
sect_prog = util::buildProgram(false, {main_vs, sect_fs, sect_gs});
glDeleteShader(main_vs);
glDeleteShader(main_fs);
glDeleteShader(sect_fs);
glDeleteShader(wire_fs);
glDeleteShader(sect_gs);
glDeleteShader(wire_gs);
//endregion
@@ -106,13 +132,6 @@ class GLApp : public App {
glVertexAttribIPointer(ind_loc, 4, GL_UNSIGNED_INT, sizeof(int) * 4, (void *) nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// 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
};
@@ -123,8 +142,8 @@ class GLApp : public App {
float ratio = (float) width / height;
matrices.model =
rotor(glm::vec4(1, 0, 0, 0), glm::vec4(0, 0, 1, 0), getTime() / 4) *
rotor(glm::vec4(0, 1, 0, 0), glm::vec4(0, 0, 0, 1), getTime() / 4);
rotor(glm::vec4(0, 1, 0, 0), glm::vec4(0, 0, 1, 0), getTime() / 4) *
rotor(glm::vec4(1, 1, 1, 0), glm::vec4(0, 0, 0, 1), getTime() / 5);
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);
@@ -145,11 +164,19 @@ class GLApp : public App {
glLineWidth(2);
glEnable(GL_DEPTH_TEST);
glBindVertexArray(cell_array);
glUseProgram(sect_prog);
glDrawArrays(GL_POINTS, 0, (GLsizei) cell_elems.size() / 4);
glUseProgram(wire_prog);
glDrawArrays(GL_POINTS, 0, (GLsizei) cell_elems.size() / 4);
glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(sect_prog);
glDrawArrays(GL_POINTS, 0, (GLsizei) cell_elems.size() / 4);
glUseProgram(wire_prog);
glDrawArrays(GL_POINTS, 0, (GLsizei) cell_elems.size() / 4);
glBindVertexArray(0);
glFinish();