aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-13 13:27:51 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-13 13:27:51 +0200
commit31adf2430974747d1c0d993b8a1a3e0750d79467 (patch)
tree6f11e73e657a100e1ae9ffa31c0106a099677e92
parent66004b8c75175227eb432ab0f56274d98a834eab (diff)
clean up uniform code
-rw-r--r--consts.h4
-rw-r--r--main.c7
-rw-r--r--makefile1
-rw-r--r--pass1.frag15
-rw-r--r--pass2.frag7
-rw-r--r--uniform.c26
-rw-r--r--uniform.h9
7 files changed, 30 insertions, 39 deletions
diff --git a/consts.h b/consts.h
index 08a1801..ea4dbbe 100644
--- a/consts.h
+++ b/consts.h
@@ -4,4 +4,8 @@
#define RES_H 800
#define RES_V 600
+#define U_LOC_TIME 1
+#define U_LOC_PASS1 2
+#define U_LOC_WINDOW 3
+
#endif
diff --git a/main.c b/main.c
index b247c04..e65c829 100644
--- a/main.c
+++ b/main.c
@@ -69,18 +69,17 @@ int main(int argc, char** argv) {
glewInit();
init_tri();
+ init_uniforms();
GLuint pass1 = link_shaders(
vert_shader(fill_vert, fill_vert_size),
frag_shader(pass1_frag, pass1_frag_size)
);
- uniforms_t * pass1_uniforms = init_uniforms(pass1, window);
GLuint pass2 = link_shaders(
vert_shader(fill_vert, fill_vert_size),
frag_shader(pass2_frag, pass2_frag_size)
);
- uniforms_t * pass2_uniforms = init_uniforms(pass2, window);
GLuint fbo;
glGenFramebuffers(1, &fbo);
@@ -104,12 +103,12 @@ int main(int argc, char** argv) {
glfwPollEvents();
glUseProgram(pass1);
- update_uniforms(pass1_uniforms);
+ update_uniforms();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glDrawArrays(GL_TRIANGLES, 0, 3);
glUseProgram(pass2);
- update_uniforms(pass2_uniforms);
+ update_uniforms();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
diff --git a/makefile b/makefile
index d1383fb..719f0f0 100644
--- a/makefile
+++ b/makefile
@@ -5,7 +5,6 @@ LDFLAGS += -lGLEW
CFLAGS += -g
GLFLAGS += --target-env=opengl
-GLFLAGS += -fauto-map-locations
.PHONY: FORCE
.SECONDARY: # do not remove intermediate files
diff --git a/pass1.frag b/pass1.frag
index 725c854..784a9a2 100644
--- a/pass1.frag
+++ b/pass1.frag
@@ -2,24 +2,23 @@
#include "consts.h"
-out vec4 color;
+layout(location = 0) out vec4 color;
in vec4 gl_FragCoord;
-uniform float time;
-uniform sampler2D buf;
-
-ivec2 window = ivec2(RES_H, RES_V);
+layout(location = U_LOC_TIME) uniform float time;
+layout(location = U_LOC_PASS1) uniform sampler2D buf;
+ivec2 res = 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
+ point.x *= float(res.y) / float(res.x); // adjust for aspect ratio
+ point = ((point + 1.0) / 2.0) * res.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;
+ color += texture(buf, gl_FragCoord.xy / res.xy) * 0.95;
}
diff --git a/pass2.frag b/pass2.frag
index 3193cb0..3184475 100644
--- a/pass2.frag
+++ b/pass2.frag
@@ -2,11 +2,12 @@
#include "consts.h"
-out vec4 color;
+layout(location = 0) out vec4 color;
in vec4 gl_FragCoord;
-uniform ivec2 window;
-uniform sampler2D buf;
+layout(location = U_LOC_TIME) uniform float time;
+layout(location = U_LOC_PASS1) uniform sampler2D buf;
+layout(location = U_LOC_WINDOW) uniform ivec2 window;
void main() {
vec2 uv = gl_FragCoord.xy / window.xy;
diff --git a/uniform.c b/uniform.c
index dbfd0d0..2face0a 100644
--- a/uniform.c
+++ b/uniform.c
@@ -3,40 +3,34 @@
#include <GL/glew.h>
#include "uniform.h"
+#include "consts.h"
int time_start = 0;
GLFWwindow* window = NULL;
-uniforms_t * init_uniforms(GLuint shader, GLFWwindow* _window) {
- window = _window;
+void init_uniforms() {
+ window = glfwGetCurrentContext();
time_start = time(NULL);
-
- uniforms_t * uniforms = malloc(sizeof(uniforms_t));
-
- uniforms->time = glGetUniformLocation(shader, "time");
- uniforms->window = glGetUniformLocation(shader, "window");
-
- return uniforms;
}
//! update `uniform float time`
-static void update_u_time(GLuint u_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);
+ glUniform1f(U_LOC_TIME, time);
}
//! update `uniform ivec2 window`
-static void update_u_window(GLuint u_window) {
+static void update_u_window() {
int width, height;
glfwGetWindowSize(window, &width, &height);
- glUniform2i(u_window, width, height);
+ glUniform2i(U_LOC_WINDOW, width, height);
}
-void update_uniforms(uniforms_t * uniforms) {
- update_u_time(uniforms->time);
- update_u_window(uniforms->window);
+void update_uniforms() {
+ update_u_time();
+ update_u_window();
}
diff --git a/uniform.h b/uniform.h
index 0132078..698d5e3 100644
--- a/uniform.h
+++ b/uniform.h
@@ -5,11 +5,6 @@
#include <GLFW/glfw3.h>
#include <GL/gl.h>
-typedef struct {
- GLint time;
- GLint window;
-} uniforms_t;
-
-uniforms_t * init_uniforms(GLuint shader, GLFWwindow* window);
-void update_uniforms(uniforms_t * uniforms);
+void init_uniforms();
+void update_uniforms();