diff options
| author | lonkaars <loek@pipeframe.xyz> | 2024-05-17 08:55:48 +0200 | 
|---|---|---|
| committer | lonkaars <loek@pipeframe.xyz> | 2024-05-17 08:55:48 +0200 | 
| commit | aa9601e577e5ce7ac02f28b0a6f2d5dffc901c5f (patch) | |
| tree | ac103be156c568a3758757ed65dedbc09cbc3b6b | |
| parent | f6ce5684117032e6a42a769c01a339e80d945841 (diff) | |
clean up uniform init/update code + fix window resizing
| -rw-r--r-- | main.c | 66 | ||||
| -rw-r--r-- | makefile | 1 | ||||
| -rw-r--r-- | uniform.c | 40 | ||||
| -rw-r--r-- | uniform.h | 10 | ||||
| -rw-r--r-- | visuals.frag | 6 | 
5 files changed, 87 insertions, 36 deletions
| @@ -1,17 +1,42 @@  #include <stdlib.h>  #include <GL/glew.h>  #include <GLFW/glfw3.h> -#include <sys/time.h> -#include <time.h>  #include "shader.h"  #include "die.h" -#include "config.h" +#include "uniform.h"  #include "fill_vert.h"  #include "visuals_frag.h" -void prepare_tri() { +void resize_handler(GLFWwindow* window, int width, int height) { +	glViewport(0, 0, width, height); +} + +GLFWwindow* init_window() { +	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); +	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); +	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +	// creating a non-resizable floating window makes the window float with i3 +	glfwWindowHint(GLFW_FLOATING, GL_TRUE); +	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + +	// initialize window +	int width = 800, height = 600; // default size +	GLFWwindow* window = glfwCreateWindow(width, height, "vis", NULL, NULL); +	if (window == NULL) +		die("error: could not create window\n"); +	glfwMakeContextCurrent(window); +	glfwSetFramebufferSizeCallback(window, resize_handler); + +	// but i still want a resizable window +	glfwSetWindowAttrib(window, GLFW_RESIZABLE, GL_TRUE); + +	return window; +} + +void init_tri() {  	const float vertices[] = {  		0, 1, 0,  		1, -1, 0, @@ -39,46 +64,21 @@ void prepare_tri() {  int main(int argc, char** argv) {  	glfwInit(); -	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); -	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); -	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - -	// force floating window w/ i3 -	glfwWindowHint(GLFW_FLOATING, GL_TRUE); -	glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - -	// initialize window -	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "vis", NULL, NULL); -	if (window == NULL) -		die("error: could not create window\n"); -	glfwMakeContextCurrent(window); - -	// initialize GLEW +	GLFWwindow* window = init_window();  	glewInit(); -	// create single triangle -	prepare_tri(); - -	// prepare shaders +	init_tri();  	GLuint shader = link_shaders(  		vert_shader(fill_vert, fill_vert_size),  		frag_shader(visuals_frag, visuals_frag_size)  	); - -	// prepare uniform variables -	GLint u_time = glGetUniformLocation(shader, "time"); -	int time_start = time(NULL); +	init_uniform(shader, window);  	// 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); +		update_uniform();  		glUseProgram(shader);  		glDrawArrays(GL_TRIANGLES, 0, 3); @@ -17,6 +17,7 @@ main: die.o  main: shader.o  main: visuals_frag.o  main: fill_vert.o +main: uniform.o  # fix compile order  main.o: fill_vert.h diff --git a/uniform.c b/uniform.c new file mode 100644 index 0000000..6c66b7a --- /dev/null +++ b/uniform.c @@ -0,0 +1,40 @@ +#include <sys/time.h> +#include <time.h> +#include <GL/glew.h> + +#include "uniform.h" + +int time_start = 0; +GLFWwindow* window = NULL; +GLint u_time, u_window; + +void init_uniform(GLuint shader, GLFWwindow* _window) { +	window = _window; + +	u_time = glGetUniformLocation(shader, "time"); +	u_window = glGetUniformLocation(shader, "window"); + +	time_start = time(NULL); +} + +/** @brief update `uniform float 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); +} + +/** @brief update `uniform ivec2 window` */ +static void update_u_window() { +	int width, height; +	glfwGetWindowSize(window, &width, &height); +	glUniform2i(u_window, width, height); +} + +void update_uniform() { +	update_u_time(); +	update_u_window(); +} + diff --git a/uniform.h b/uniform.h new file mode 100644 index 0000000..d7c58b3 --- /dev/null +++ b/uniform.h @@ -0,0 +1,10 @@ +#pragma once + +#include <stdlib.h> +#include <GL/glew.h> +#include <GLFW/glfw3.h> +#include <GL/gl.h> + +void init_uniform(GLuint shader, GLFWwindow* window); +void update_uniform(); + diff --git a/visuals.frag b/visuals.frag index 16e7712..d680834 100644 --- a/visuals.frag +++ b/visuals.frag @@ -1,8 +1,7 @@  #version 330 core -#include "config.h" -  uniform float time; +uniform ivec2 window;  // adapted from <https://www.shadertoy.com/view/4djSRW>  float hash12(vec2 p) { @@ -12,7 +11,8 @@ float hash12(vec2 p) {  }  void main() { +	vec2 uv = gl_FragCoord.xy / window;  	vec2 pos = gl_FragCoord.xy + time; -	gl_FragColor = vec4(vec3(hash12(pos)), 1.0); +	gl_FragColor = vec4(hash12(pos), uv.xy, 1.0);  } |