aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-13 13:14:47 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-13 13:14:47 +0200
commit66004b8c75175227eb432ab0f56274d98a834eab (patch)
tree7bb37ee339218f41c200bb3c35fdf932e41d6118
parent24fc13a036fb10140ed1b60dd1e9005821600fc1 (diff)
2-pass rendering working w/ fixed resolution + upscaling
-rw-r--r--consts.h7
-rw-r--r--main.c32
-rw-r--r--makefile8
-rw-r--r--pass1.frag (renamed from visuals.frag)7
-rw-r--r--pass2.frag (renamed from output.frag)5
-rw-r--r--uniform.c4
6 files changed, 39 insertions, 24 deletions
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 <GL/glew.h>
#include <GLFW/glfw3.h>
+#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/visuals.frag b/pass1.frag
index 35f9a6a..725c854 100644
--- a/visuals.frag
+++ b/pass1.frag
@@ -1,12 +1,15 @@
#version 460 core
+#include "consts.h"
+
out vec4 color;
in vec4 gl_FragCoord;
uniform float time;
-uniform ivec2 window;
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
@@ -17,6 +20,6 @@ void main() {
if (length(gl_FragCoord.xy - point) < r)
color = vec4(1.0);
- color += texture(buf, gl_FragCoord.xy / window.xy) * 0.90;
+ color += texture(buf, gl_FragCoord.xy / window.xy) * 0.95;
}
diff --git a/output.frag b/pass2.frag
index df4d103..3193cb0 100644
--- a/output.frag
+++ b/pass2.frag
@@ -1,5 +1,7 @@
#version 460 core
+#include "consts.h"
+
out vec4 color;
in vec4 gl_FragCoord;
@@ -7,6 +9,7 @@ uniform ivec2 window;
uniform sampler2D buf;
void main() {
- color = texture(buf, gl_FragCoord.xy / window.xy);
+ 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);