aboutsummaryrefslogtreecommitdiff
path: root/src/shader/util.cc
blob: be59ac1d4417310c8cd90a04cfb6076c51c4c3da (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
#include "util.h"

#include <iostream>
#include <vector>

namespace util {

GLint getUniform(GLuint program, const std::string& name) {
	const GLint uniform = glGetUniformLocation(program, name.c_str());

	if ( uniform == -1 ) {
		std::cerr << "Could not bind uniform " << name << std::endl;
		return -1;
	}

	return uniform;
}

GLint compileShader(const std::string& source, GLenum type) {
	GLint shader = glCreateShader(type);

	if ( !shader ) {
		std::cerr << "Cannot create a shader of type " << type << std::endl;
		return -1;
	}

	const char* source_data = source.c_str();
	const int source_length = source.size();

	glShaderSource(shader, 1, &source_data, &source_length);
	glCompileShader(shader);

	GLint compiled;
	glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

	if ( !compiled ) {
		std::cerr << "Cannot compile shader" << std::endl;

		GLint maxLength = 0;
		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);

		std::vector<GLchar> errorLog(maxLength);
		glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);

		for( auto c : errorLog ) {
			std::cerr << c;
		}
		std::cerr << std::endl;

		return -1;
	}

	return shader;
}

}