From 8d7911dc40c3bfc8f4fae65f29f4c8094aec79f7 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 16 May 2024 20:18:59 +0200 Subject: add uniform variable to shader --- main.c | 13 +++++++++++++ makefile | 7 +++++-- visuals.frag | 12 +++++++++++- 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 #include #include +#include +#include #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 +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); } -- cgit v1.2.3