aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 50cf16d8d8aac0714c8f4338f2e8e528ab8dc5dd (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "shader.h"
#include "die.h"
#include "uniform.h"

#include "fill_vert.h"
#include "visuals_frag.h"
#include "output_frag.h"

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,
		-1, -1, 0,
	};

	// initialize vertex {buffer,attribute} object buffers
	GLuint VBO, VAO;
	glGenBuffers(1, &VBO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);

	// copy vertex data into VBO
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// set VAO pointers
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);

	// draw (only) this triangle
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(VAO);
}

int main(int argc, char** argv) {
	glfwInit();
	GLFWwindow* window = init_window();
	glewInit();

	init_tri();

	GLuint visuals = link_shaders(
		vert_shader(fill_vert, fill_vert_size),
		frag_shader(visuals_frag, visuals_frag_size)
	);
	uniforms_t * visuals_uniforms = init_uniforms(visuals, window);

	GLuint output = link_shaders(
		vert_shader(fill_vert, fill_vert_size),
		frag_shader(output_frag, output_frag_size)
	);
	uniforms_t * output_uniforms = init_uniforms(output, window);

	GLuint fbo;
	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);

	GLuint last_frame;
	glGenTextures(1, &last_frame);
	glBindTexture(GL_TEXTURE_2D, last_frame);
	// todo: what to do when the size changes?
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, last_frame, 0);
	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
		die("can't initialize framebuffer??\n");
	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	glUseProgram(visuals);

	// main draw loop
	while (!glfwWindowShouldClose(window)) {
		glfwPollEvents();

		update_uniforms(visuals_uniforms);
		glBindFramebuffer(GL_FRAMEBUFFER, fbo);
		glDrawArrays(GL_TRIANGLES, 0, 3);

		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		glDrawArrays(GL_TRIANGLES, 0, 3);

		glfwSwapBuffers(window);
	}

	return EXIT_SUCCESS;
}