aboutsummaryrefslogtreecommitdiff
path: root/uniform.c
blob: a6a77728c846ba7b0c3dbb8db9e59ccb875135d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <sys/time.h>
#include <time.h>
#include <GL/glew.h>

#include "uniform.h"

int time_start = 0;
GLFWwindow* window = NULL;

uniforms_t * init_uniforms(GLuint shader, GLFWwindow* _window) {
	window = _window;
	time_start = time(NULL);

	uniforms_t * uniforms = malloc(sizeof(uniforms_t));

	uniforms->time = glGetUniformLocation(shader, "time");
	uniforms->window = glGetUniformLocation(shader, "window");

	return uniforms;
}

/** @brief update `uniform float time` */
static void update_u_time(GLuint 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);
}

/** @brief update `uniform ivec2 window` */
static void update_u_window(GLuint u_window) {
	int width, height;
	glfwGetWindowSize(window, &width, &height);
	glUniform2i(u_window, width, height);
}

void update_uniforms(uniforms_t * uniforms) {
	update_u_time(uniforms->time);
	update_u_window(uniforms->window);
}