From 66004b8c75175227eb432ab0f56274d98a834eab Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 13 Jun 2024 13:14:47 +0200 Subject: 2-pass rendering working w/ fixed resolution + upscaling --- consts.h | 7 +++++++ main.c | 32 +++++++++++++++++--------------- makefile | 8 ++++---- output.frag | 12 ------------ pass1.frag | 25 +++++++++++++++++++++++++ pass2.frag | 15 +++++++++++++++ uniform.c | 4 ++-- visuals.frag | 22 ---------------------- 8 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 consts.h delete mode 100644 output.frag create mode 100644 pass1.frag create mode 100644 pass2.frag delete mode 100644 visuals.frag diff --git a/consts.h b/consts.h new file mode 100644 index 0000000..08a1801 --- /dev/null +++ b/consts.h @@ -0,0 +1,7 @@ +#ifndef CONSTS_H +#define CONSTS_H + +#define RES_H 800 +#define RES_V 600 + +#endif diff --git a/main.c b/main.c index 50cf16d..b247c04 100644 --- a/main.c +++ b/main.c @@ -2,13 +2,14 @@ #include #include +#include "consts.h" #include "shader.h" #include "die.h" #include "uniform.h" #include "fill_vert.h" -#include "visuals_frag.h" -#include "output_frag.h" +#include "pass1_frag.h" +#include "pass2_frag.h" void resize_handler(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); @@ -24,8 +25,7 @@ GLFWwindow* init_window() { glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // initialize window - int width = 800, height = 600; // default size - GLFWwindow* window = glfwCreateWindow(width, height, "vis", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(RES_H, RES_V, "vis", NULL, NULL); if (window == NULL) die("error: could not create window\n"); glfwMakeContextCurrent(window); @@ -70,17 +70,17 @@ int main(int argc, char** argv) { init_tri(); - GLuint visuals = link_shaders( + GLuint pass1 = link_shaders( vert_shader(fill_vert, fill_vert_size), - frag_shader(visuals_frag, visuals_frag_size) + frag_shader(pass1_frag, pass1_frag_size) ); - uniforms_t * visuals_uniforms = init_uniforms(visuals, window); + uniforms_t * pass1_uniforms = init_uniforms(pass1, window); - GLuint output = link_shaders( + GLuint pass2 = link_shaders( vert_shader(fill_vert, fill_vert_size), - frag_shader(output_frag, output_frag_size) + frag_shader(pass2_frag, pass2_frag_size) ); - uniforms_t * output_uniforms = init_uniforms(output, window); + uniforms_t * pass2_uniforms = init_uniforms(pass2, window); GLuint fbo; glGenFramebuffers(1, &fbo); @@ -90,24 +90,26 @@ int main(int argc, char** argv) { glGenTextures(1, &last_frame); glBindTexture(GL_TEXTURE_2D, last_frame); // todo: what to do when the size changes? - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RES_H, RES_V, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, last_frame, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) die("can't initialize framebuffer??\n"); glBindFramebuffer(GL_FRAMEBUFFER, 0); - glUseProgram(visuals); // main draw loop while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - update_uniforms(visuals_uniforms); + glUseProgram(pass1); + update_uniforms(pass1_uniforms); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawArrays(GL_TRIANGLES, 0, 3); + glUseProgram(pass2); + update_uniforms(pass2_uniforms); glBindFramebuffer(GL_FRAMEBUFFER, 0); glDrawArrays(GL_TRIANGLES, 0, 3); diff --git a/makefile b/makefile index d22eaec..d1383fb 100644 --- a/makefile +++ b/makefile @@ -15,15 +15,15 @@ all: main FORCE main: main.o main: die.o main: shader.o -main: visuals_frag.o +main: pass1_frag.o main: fill_vert.o -main: output_frag.o +main: pass2_frag.o main: uniform.o # fix compile order main.o: fill_vert.h -main.o: visuals_frag.h -main.o: output_frag.h +main.o: pass1_frag.h +main.o: pass2_frag.h %.s %.h &: %.spv ./blob $< $* diff --git a/output.frag b/output.frag deleted file mode 100644 index df4d103..0000000 --- a/output.frag +++ /dev/null @@ -1,12 +0,0 @@ -#version 460 core - -out vec4 color; -in vec4 gl_FragCoord; - -uniform ivec2 window; -uniform sampler2D buf; - -void main() { - color = texture(buf, gl_FragCoord.xy / window.xy); -} - diff --git a/pass1.frag b/pass1.frag new file mode 100644 index 0000000..725c854 --- /dev/null +++ b/pass1.frag @@ -0,0 +1,25 @@ +#version 460 core + +#include "consts.h" + +out vec4 color; +in vec4 gl_FragCoord; + +uniform float time; +uniform sampler2D buf; + +ivec2 window = ivec2(RES_H, RES_V); + +void main() { + vec2 point = vec2(sin(time), cos(time)) * 0.7; + point.x *= float(window.y) / float(window.x); // adjust for aspect ratio + point = ((point + 1.0) / 2.0) * window.xy; // convert to screen coords + float r = 20.0; // circle radius + + color = vec4(0.0); + if (length(gl_FragCoord.xy - point) < r) + color = vec4(1.0); + + color += texture(buf, gl_FragCoord.xy / window.xy) * 0.95; +} + diff --git a/pass2.frag b/pass2.frag new file mode 100644 index 0000000..3193cb0 --- /dev/null +++ b/pass2.frag @@ -0,0 +1,15 @@ +#version 460 core + +#include "consts.h" + +out vec4 color; +in vec4 gl_FragCoord; + +uniform ivec2 window; +uniform sampler2D buf; + +void main() { + vec2 uv = gl_FragCoord.xy / window.xy; + color = texture(buf, uv); +} + diff --git a/uniform.c b/uniform.c index a6a7772..dbfd0d0 100644 --- a/uniform.c +++ b/uniform.c @@ -19,7 +19,7 @@ uniforms_t * init_uniforms(GLuint shader, GLFWwindow* _window) { return uniforms; } -/** @brief update `uniform float time` */ +//! update `uniform float time` static void update_u_time(GLuint u_time) { struct timeval t; gettimeofday(&t, NULL); @@ -28,7 +28,7 @@ static void update_u_time(GLuint u_time) { glUniform1f(u_time, time); } -/** @brief update `uniform ivec2 window` */ +//! update `uniform ivec2 window` static void update_u_window(GLuint u_window) { int width, height; glfwGetWindowSize(window, &width, &height); diff --git a/visuals.frag b/visuals.frag deleted file mode 100644 index 35f9a6a..0000000 --- a/visuals.frag +++ /dev/null @@ -1,22 +0,0 @@ -#version 460 core - -out vec4 color; -in vec4 gl_FragCoord; - -uniform float time; -uniform ivec2 window; -uniform sampler2D buf; - -void main() { - vec2 point = vec2(sin(time), cos(time)) * 0.7; - point.x *= float(window.y) / float(window.x); // adjust for aspect ratio - point = ((point + 1.0) / 2.0) * window.xy; // convert to screen coords - float r = 20.0; // circle radius - - color = vec4(0.0); - if (length(gl_FragCoord.xy - point) < r) - color = vec4(1.0); - - color += texture(buf, gl_FragCoord.xy / window.xy) * 0.90; -} - -- cgit v1.2.3