From aa9601e577e5ce7ac02f28b0a6f2d5dffc901c5f Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 17 May 2024 08:55:48 +0200 Subject: clean up uniform init/update code + fix window resizing --- main.c | 66 ++++++++++++++++++++++++++++++------------------------------ makefile | 1 + uniform.c | 40 ++++++++++++++++++++++++++++++++++++ uniform.h | 10 +++++++++ visuals.frag | 6 +++--- 5 files changed, 87 insertions(+), 36 deletions(-) create mode 100644 uniform.c create mode 100644 uniform.h diff --git a/main.c b/main.c index c4da904..00f5723 100644 --- a/main.c +++ b/main.c @@ -1,17 +1,42 @@ #include #include #include -#include -#include #include "shader.h" #include "die.h" -#include "config.h" +#include "uniform.h" #include "fill_vert.h" #include "visuals_frag.h" -void prepare_tri() { +void resize_handler(GLFWwindow* window, int width, int height) { + glViewport(0, 0, width, height); +} + +GLFWwindow* init_window() { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + // creating a non-resizable floating window makes the window float with i3 + glfwWindowHint(GLFW_FLOATING, GL_TRUE); + glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + + // initialize window + int width = 800, height = 600; // default size + GLFWwindow* window = glfwCreateWindow(width, height, "vis", NULL, NULL); + if (window == NULL) + die("error: could not create window\n"); + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, resize_handler); + + // but i still want a resizable window + glfwSetWindowAttrib(window, GLFW_RESIZABLE, GL_TRUE); + + return window; +} + +void init_tri() { const float vertices[] = { 0, 1, 0, 1, -1, 0, @@ -39,46 +64,21 @@ void prepare_tri() { int main(int argc, char** argv) { glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - // force floating window w/ i3 - glfwWindowHint(GLFW_FLOATING, GL_TRUE); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - // initialize window - GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "vis", NULL, NULL); - if (window == NULL) - die("error: could not create window\n"); - glfwMakeContextCurrent(window); - - // initialize GLEW + GLFWwindow* window = init_window(); glewInit(); - // create single triangle - prepare_tri(); - - // prepare shaders + init_tri(); GLuint shader = link_shaders( vert_shader(fill_vert, fill_vert_size), frag_shader(visuals_frag, visuals_frag_size) ); - - // prepare uniform variables - GLint u_time = glGetUniformLocation(shader, "time"); - int time_start = time(NULL); + init_uniform(shader, window); // main draw loop while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - // set `time` uniform - struct timeval t; - gettimeofday(&t, NULL); - t.tv_sec -= time_start; - float time = t.tv_sec + (t.tv_usec / 1e6); - glUniform1f(u_time, time); + update_uniform(); glUseProgram(shader); glDrawArrays(GL_TRIANGLES, 0, 3); diff --git a/makefile b/makefile index daf3750..ef01ae8 100644 --- a/makefile +++ b/makefile @@ -17,6 +17,7 @@ main: die.o main: shader.o main: visuals_frag.o main: fill_vert.o +main: uniform.o # fix compile order main.o: fill_vert.h diff --git a/uniform.c b/uniform.c new file mode 100644 index 0000000..6c66b7a --- /dev/null +++ b/uniform.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#include "uniform.h" + +int time_start = 0; +GLFWwindow* window = NULL; +GLint u_time, u_window; + +void init_uniform(GLuint shader, GLFWwindow* _window) { + window = _window; + + u_time = glGetUniformLocation(shader, "time"); + u_window = glGetUniformLocation(shader, "window"); + + time_start = time(NULL); +} + +/** @brief update `uniform float time` */ +static void update_u_time() { + struct timeval t; + gettimeofday(&t, NULL); + t.tv_sec -= time_start; + float time = t.tv_sec + (t.tv_usec / 1e6); + glUniform1f(u_time, time); +} + +/** @brief update `uniform ivec2 window` */ +static void update_u_window() { + int width, height; + glfwGetWindowSize(window, &width, &height); + glUniform2i(u_window, width, height); +} + +void update_uniform() { + update_u_time(); + update_u_window(); +} + diff --git a/uniform.h b/uniform.h new file mode 100644 index 0000000..d7c58b3 --- /dev/null +++ b/uniform.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include +#include + +void init_uniform(GLuint shader, GLFWwindow* window); +void update_uniform(); + diff --git a/visuals.frag b/visuals.frag index 16e7712..d680834 100644 --- a/visuals.frag +++ b/visuals.frag @@ -1,8 +1,7 @@ #version 330 core -#include "config.h" - uniform float time; +uniform ivec2 window; // adapted from float hash12(vec2 p) { @@ -12,7 +11,8 @@ float hash12(vec2 p) { } void main() { + vec2 uv = gl_FragCoord.xy / window; vec2 pos = gl_FragCoord.xy + time; - gl_FragColor = vec4(vec3(hash12(pos)), 1.0); + gl_FragColor = vec4(hash12(pos), uv.xy, 1.0); } -- cgit v1.2.3