aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-16 20:18:59 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-16 20:18:59 +0200
commit8d7911dc40c3bfc8f4fae65f29f4c8094aec79f7 (patch)
treee3ded763bc15661cfa7cf420e5e96643eb865722
parent68081ef4f1f51b5753e25a1be6f870eb2e84f76e (diff)
add uniform variable to shader
-rw-r--r--main.c13
-rw-r--r--makefile7
-rw-r--r--visuals.frag12
3 files changed, 29 insertions, 3 deletions
diff --git a/main.c b/main.c
index 09cf2fb..c4da904 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
+#include <sys/time.h>
+#include <time.h>
#include "shader.h"
#include "die.h"
@@ -63,10 +65,21 @@ int main(int argc, char** argv) {
frag_shader(visuals_frag, visuals_frag_size)
);
+ // prepare uniform variables
+ GLint u_time = glGetUniformLocation(shader, "time");
+ int time_start = time(NULL);
+
// 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);
+
glUseProgram(shader);
glDrawArrays(GL_TRIANGLES, 0, 3);
diff --git a/makefile b/makefile
index fa39ee9..daf3750 100644
--- a/makefile
+++ b/makefile
@@ -4,6 +4,9 @@ LDFLAGS += -lGLEW
CFLAGS += -g
+GLFLAGS += --target-env=opengl
+GLFLAGS += -fauto-map-locations
+
.PHONY: FORCE
.SECONDARY: # do not remove intermediate files
@@ -23,9 +26,9 @@ main.o: visuals_frag.h
./blob $< $*
%_frag.spv: %.frag
- glslc -o $@ $<
+ glslc $(GLFLAGS) -o $@ $<
%_vert.spv: %.vert
- glslc -o $@ $<
+ glslc $(GLFLAGS) -o $@ $<
clean: FORCE
git clean -fxdi
diff --git a/visuals.frag b/visuals.frag
index 3539bdc..16e7712 100644
--- a/visuals.frag
+++ b/visuals.frag
@@ -2,7 +2,17 @@
#include "config.h"
+uniform float time;
+
+// adapted from <https://www.shadertoy.com/view/4djSRW>
+float hash12(vec2 p) {
+ vec3 p3 = fract(vec3(p.xyx) * .1031);
+ p3 += dot(p3, p3.yzx + 33.33);
+ return fract((p3.x + p3.y) * p3.z);
+}
+
void main() {
- gl_FragColor = vec4(gl_FragCoord.x / WIDTH, gl_FragCoord.y / HEIGHT, 0, 1);
+ vec2 pos = gl_FragCoord.xy + time;
+ gl_FragColor = vec4(vec3(hash12(pos)), 1.0);
}