aboutsummaryrefslogtreecommitdiff
path: root/src/shader.c
blob: 52c8df8669289176cc20d354e6abf3af73e6c160 (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
#include "mandel.h"

#define MANDEL_SHADER_VERT_FILE "shader/vertex.glsl"
#define MANDEL_SHADER_FRAG_FILE "shader/fragment.glsl"

static unsigned int	st_compile(char *filepath, unsigned int type);
static int			st_get_location(unsigned int shader_id, const char *name);

bool				shader_init(Shader *shader)
{
	unsigned int	shader_vert;
	unsigned int	shader_frag;

	if ((shader_vert = st_compile(MANDEL_SHADER_VERT_FILE, GL_VERTEX_SHADER)) == 0
		|| (shader_frag = st_compile(MANDEL_SHADER_FRAG_FILE, GL_FRAGMENT_SHADER)) == 0)
		return false;

	GL_CALL(shader->id = glCreateProgram());
	GL_CALL(glAttachShader(shader->id, shader_vert));
	GL_CALL(glAttachShader(shader->id, shader_frag));
	GL_CALL(glLinkProgram(shader->id));
	GL_CALL(glValidateProgram(shader->id));
	GL_CALL(glDeleteShader(shader_vert));
	GL_CALL(glDeleteShader(shader_frag));

	if ((shader->location.width = st_get_location(shader->id, "u_width")) == -1
		|| (shader->location.height = st_get_location(shader->id, "u_height")) == -1
		|| (shader->location.real_start = st_get_location(shader->id, "u_real_start")) == -1
		|| (shader->location.real_end = st_get_location(shader->id, "u_real_end")) == -1
		|| (shader->location.imag_start = st_get_location(shader->id, "u_imag_start")) == -1
		|| (shader->location.imag_end = st_get_location(shader->id, "u_imag_end")) == -1
		|| (shader->location.iterations = st_get_location(shader->id, "u_iterations")) == -1
		|| (shader->location.smooth = st_get_location(shader->id, "u_smooth")) == -1
		|| (shader->location.samples = st_get_location(shader->id, "u_samples")) == -1
		|| (shader->location.texture = st_get_location(shader->id, "u_texture")) == -1)
		return false;
	return true;
}

static int			st_get_location(unsigned int shader_id, const char *name)
{
	int	location;

	GL_CALL(location = glGetUniformLocation(shader_id, name));
	return location;
}

void				shader_set_uniforms(Shader *shader, State *state)
{
	GL_CALL(glUniform1i(shader->location.width, state->width));
	GL_CALL(glUniform1i(shader->location.height, state->height));

	GL_CALL(glUniform1f(shader->location.real_start, state->real_start));
	GL_CALL(glUniform1f(shader->location.real_end, state->real_end));
	GL_CALL(glUniform1f(shader->location.imag_start, state->imag_start));
	GL_CALL(glUniform1f(shader->location.imag_end, state->imag_end));

	GL_CALL(glUniform1i(shader->location.iterations, state->iterations));

	GL_CALL(glUniform1i(shader->location.smooth, state->smooth));

	GL_CALL(glUniform1f(shader->location.samples, state->samples));

	GL_CALL(glUniform1i(shader->location.texture, 0));
	GL_CALL(glActiveTexture(GL_TEXTURE0));
	GL_CALL(glBindTexture(GL_TEXTURE_1D, state->texture));
}

static unsigned int	st_compile(char *filepath, unsigned int type)
{
	unsigned int	id;
	int				result;
	FILE			*file;
	char			*source;

	if ((file = fopen(filepath, "r")) == NULL)
		return 0;
	fseek(file, 0, SEEK_END);
	long file_size = ftell(file);
	rewind(file);

	if ((source = malloc(sizeof(char) * (file_size + 1))) == NULL
		|| fread(source, sizeof(char), file_size, file) != (unsigned long)file_size)
	{
		free(source);
		fclose(file);
		return 0;
	}
	fclose(file);
	source[file_size] = '\0';

	GL_CALL(id = glCreateShader(type));
	GL_CALL(glShaderSource(id, 1, (const char**)&source, (int*)&file_size));
	free(source);
	GL_CALL(glCompileShader(id));

	GL_CALL(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
	if (result == GL_FALSE)
	{
		int		len;
		char	*msg;
		GL_CALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &len));
		if ((msg = malloc(sizeof(char) * (len + 1))) == NULL)
			return 0;
		GL_CALL(glGetShaderInfoLog(id, len, &len, msg));
		fputs(msg, stderr);
		free(msg);
		GL_CALL(glDeleteShader(id));
		return 0;
	}
	return id;
}