diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 4ce9e08..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -gl_template \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6e7f3d0..399786f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + diff --git a/CMakeLists.txt b/CMakeLists.txt index 583d230..6761650 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/framework/include/framework.h b/framework/include/framework.h index 5d105c4..af4d87d 100644 --- a/framework/include/framework.h +++ b/framework/include/framework.h @@ -60,6 +60,8 @@ protected: virtual void display() {} + virtual void update() {} + virtual void deinit() {} public: diff --git a/framework/src/framework.cpp b/framework/src/framework.cpp index bbeb0cd..46c89ba 100644 --- a/framework/src/framework.cpp +++ b/framework/src/framework.cpp @@ -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(); }; diff --git a/glapp/shaders/main.frag b/glapp/shaders/main.frag deleted file mode 100644 index 195b510..0000000 --- a/glapp/shaders/main.frag +++ /dev/null @@ -1,9 +0,0 @@ -#version 440 core - -smooth in vec3 color; - -out vec4 fcolor; - -void main() { - fcolor = vec4(color, 1); -} \ No newline at end of file diff --git a/glapp/shaders/main.vert b/glapp/shaders/main.vert deleted file mode 100644 index defafae..0000000 --- a/glapp/shaders/main.vert +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/glapp/src/main.cpp b/glapp/src/main.cpp deleted file mode 100644 index c180436..0000000 --- a/glapp/src/main.cpp +++ /dev/null @@ -1,176 +0,0 @@ -#include -#include -#include "glmutil.h" - -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -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 vertices; - - Matrices mats{ - glm::identity(), - glm::identity(), - glm::identity(), - }; - - 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(); -} \ No newline at end of file diff --git a/glapp/CMakeLists.txt b/simplex/CMakeLists.txt similarity index 97% rename from glapp/CMakeLists.txt rename to simplex/CMakeLists.txt index ecb3188..c39f5ab 100644 --- a/glapp/CMakeLists.txt +++ b/simplex/CMakeLists.txt @@ -1,4 +1,4 @@ -project(sample) +project(simplex) add_executable(${PROJECT_NAME} src/main.cpp) diff --git a/glapp/include/glmutil.h b/simplex/include/glmutil.h similarity index 100% rename from glapp/include/glmutil.h rename to simplex/include/glmutil.h diff --git a/simplex/include/rotor.h b/simplex/include/rotor.h new file mode 100644 index 0000000..07ba8a5 --- /dev/null +++ b/simplex/include/rotor.h @@ -0,0 +1,80 @@ +#ifndef GL_TEMPLATE_ROTOR_H +#define GL_TEMPLATE_ROTOR_H + +#include + +#include + +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 diff --git a/simplex/shaders/main.frag b/simplex/shaders/main.frag new file mode 100644 index 0000000..0a05d8b --- /dev/null +++ b/simplex/shaders/main.frag @@ -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); +} \ No newline at end of file diff --git a/simplex/shaders/main.vert b/simplex/shaders/main.vert new file mode 100644 index 0000000..3d10154 --- /dev/null +++ b/simplex/shaders/main.vert @@ -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); +} diff --git a/simplex/src/main.cpp b/simplex/src/main.cpp new file mode 100644 index 0000000..0f3f3a8 --- /dev/null +++ b/simplex/src/main.cpp @@ -0,0 +1,161 @@ +#include +#include + +#include "glmutil.h" +#include "rotor.h" + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +struct Matrices { + glm::mat4 model; + glm::mat4 view; + glm::mat4 proj; +}; + +class GLApp : public App { + std::vector cell_verts{}; + std::vector 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::identity(), + glm::identity(), + }; + + //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(); +} \ No newline at end of file