diff --git a/main/shaders/main.frag b/main/shaders/main.frag index 234034d..bd378eb 100644 --- a/main/shaders/main.frag +++ b/main/shaders/main.frag @@ -1,7 +1,12 @@ #version 440 +layout(binding=1) uniform Unifs { + mat4 uProj; + vec4 uColor; +}; + out vec4 color; void main() { - color = vec4(1); + color = uColor; } \ No newline at end of file diff --git a/main/shaders/main.vert b/main/shaders/main.vert index 6eca279..673f702 100644 --- a/main/shaders/main.vert +++ b/main/shaders/main.vert @@ -1,7 +1,12 @@ #version 440 +layout(binding=1) uniform Unifs { + mat4 uProj; + vec4 uColor; +}; + in vec4 iPos; void main(){ - gl_Position = vec4(iPos.xyz, 1); + gl_Position = uProj * vec4(iPos.xyz, 1); } \ No newline at end of file diff --git a/main/src/main.cpp b/main/src/main.cpp index fca5219..bd6090d 100644 --- a/main/src/main.cpp +++ b/main/src/main.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -18,20 +19,33 @@ #include "util.h" -#define RES_MAJOR 128 +#define RES_MAJOR 256 #define RES_MINOR 8 +#define WIDTH_BARR .050f +#define WIDTH_LINE .02f + +#define Z_RADIUS 10.f + #define PI (float) (M_PI) +struct Unifs { + glm::mat4 proj = glm::identity(); + glm::vec4 color = glm::vec4(1); +}; + struct State { GLuint vao{}; GLuint vbo{}; GLuint ibo{}; + GLuint ubo{}; GLuint prog{}; + const GLuint UNIF_BINDING_POINT = 1; std::vector verts{}; std::vector inds{}; + Unifs unifs; void add_ring(float xi, float eta) { std::vector _verts; @@ -47,6 +61,7 @@ struct State { sin((nu - xi) / 2) * cos(eta) ); + // todo build torus about projection _verts.emplace_back(v.xzy() / (1.f - v.w), 1); } @@ -65,6 +80,11 @@ struct State { inds.push_back(offset + i); } + void updateUnifs() { + glBindBuffer(GL_UNIFORM_BUFFER, ubo); + util::bufferData(GL_UNIFORM_BUFFER, unifs, GL_STREAM_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + } void init(GLFWwindow *window) { GLuint vs = util::buildShader(GL_VERTEX_SHADER, "vs", {"shaders/main.vert"}); @@ -74,11 +94,16 @@ struct State { for (int i = 0; i < 32; ++i) { for (int j = 0; j <= 4; ++j) { float xi = 2 * PI * i / 32; - float eta = PI / 2 * j / 4; + const float buf = 0.0f; + float eta = buf + (PI / 2 - 2 * buf) * j / 4; add_ring(xi, eta); } } + glGenBuffers(1, &ubo); + glBindBufferBase(GL_UNIFORM_BUFFER, UNIF_BINDING_POINT, ubo); + updateUnifs(); + glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); util::bufferData(GL_ARRAY_BUFFER, verts, GL_STATIC_DRAW); @@ -110,6 +135,8 @@ struct State { glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); + unifs.color = glm::vec4(1, 0, 0, 1); + updateUnifs(); glUseProgram(prog); glDrawElements(GL_LINES, (GLsizei) inds.size(), GL_UNSIGNED_INT, 0); @@ -117,7 +144,11 @@ struct State { } void update(GLFWwindow *window, float dt, int frame) { + int w, h; + glfwGetFramebufferSize(window, &w, &h); + float ar = (float) w / (float) h; + unifs.proj = glm::ortho(-ar, ar, -1.f, 1.f, -Z_RADIUS, Z_RADIUS); } void deinit(GLFWwindow *window) {