aboutsummaryrefslogtreecommitdiff
path: root/src/shaderutils.c
blob: d81db095e6a4744f3b5b752a3975fa2c1358050f (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
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * shaderuitils.c
 *
 * Shader utilities
 *
 * Copyright (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 * This file is part of Thrust3D - a silly game
 *
 * Thrust3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Thrust3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Thrust3D.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glew.h>
#include <stdio.h>

void shaderutils_setunf(GLuint program, const char *name, GLfloat val) {
	GLint loc;
	loc = glGetUniformLocation(program, name);
	glUniform1f(loc, val);
}

void shaderutils_setun2f(GLuint program, const char *name, GLfloat val1, GLfloat val2) {
	GLint loc;
	loc = glGetUniformLocation(program, name);
	glUniform2f(loc, val1, val2);
}

void shaderutils_setuni(GLuint program, const char *name, GLint val) {
	GLint loc;
	loc = glGetUniformLocation(program, name);
	glUniform1i(loc, val);
}

GLuint shaderutils_load_shader(const char *filename, GLenum type) {

	GLuint shader;
	char text[4096];
	size_t len;
	FILE *fh;
	int l;
	GLint status;
	
	fh = fopen(filename, "r");
	if ( fh == NULL ) {
		fprintf(stderr, "Couldn't load shader '%s'\n", filename);
		return 0;
	}
	len = fread(text, 1, 4095, fh);
	fclose(fh);
	text[len] = '\0';
	const GLchar *source = text;
	shader = glCreateShader(type);
	glShaderSource(shader, 1, &source, NULL);
	glCompileShader(shader);
	glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 
	if ( status == GL_FALSE ) {
		printf("Problems loading '%s':\n", filename);
		glGetShaderInfoLog(shader, 4095, &l, text);
		if ( l > 0 ) {
			printf("%s\n", text); fflush(stdout);
		} else {
			printf("Shader compilation failed.\n");
		}
	}
	
	return shader;
	
}

int shaderutils_link_program(GLuint program) {

	int l;
	GLint status;
	char text[4096];
	
	glLinkProgram(program);
	
	glGetProgramiv(program, GL_LINK_STATUS, &status); 
	if ( status == GL_FALSE ) {
		printf("Program linking errors:\n");
		glGetProgramInfoLog(program, 4095, &l, text);
		if ( l > 0 ) {
			printf("%s\n", text); fflush(stdout);
		} else {
			printf("Program linking failed.\n");
		}
	}
	
	return status;
	
}

int shaderutils_validate_program(GLuint program) {
	
	GLint status;
	int l;
	char text[4096];
	
	glValidateProgram(program);
	glGetProgramiv(program, GL_VALIDATE_STATUS, &status); 
	if ( status == GL_FALSE ) {
		printf("Program validation errors:\n");
		glGetProgramInfoLog(program, 4095, &l, text);
		if ( l > 0 ) {
			printf("%s\n", text); fflush(stdout);
		} else {
			printf("Program did not validate successfully.\n");
		}
		return 0;
	}
	
	return 1;
	
}