aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphics/Shader.cpp
blob: 740b055941d84e5bee2397dd4099e6c0723681bb (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
#include <string.h>
#include <errno.h>
#include <GL/glew.h>

#include "Shader.hpp"
#include "../xstdlib/xstdlib.hpp"

Shader::~Shader()
{
	if(shader_id)
		glDeleteShader(shader_id);
	if(error_log)
		delete[] error_log;
}

int Shader::Compile(const char *path, GLenum shaderType)
{
	GLint success;
	char *source;
	error_log = new char[GL_INFO_LOG_LENGTH];

	source = xfread(path, "rb");
	if(!source) {
		strcpy(error_log, strerror(errno));
		return 1;
	}

	shader_id = glCreateShader(shaderType);
	glShaderSource(shader_id, 1, &source, NULL);
	glCompileShader(shader_id);
	glGetShaderiv(shader_id, GL_COMPILE_STATUS, &success);
	if(success == 0) {
		glGetShaderInfoLog(shader_id, GL_INFO_LOG_LENGTH, NULL, error_log);
		return 1;
	}
	return 0;
}

////////////////////////////////////////////////////////////////////

ShaderManager::~ShaderManager()
{
	if(program_id)
		glDeleteProgram(program_id);
	if(error_log)
		delete[] error_log;
}

int ShaderManager::Link(Shader *arr, int nsize)
{
	GLint success;
	int i;

	program_id = glCreateProgram();
	for(i = 0; i < nsize; i++)
		glAttachShader(program_id, arr[i].GetID());
	glLinkProgram(program_id);
	glGetProgramiv(program_id, GL_LINK_STATUS, &success);
	if(success == 0) {
		error_log = new char[GL_INFO_LOG_LENGTH];
		glGetProgramInfoLog(program_id, GL_INFO_LOG_LENGTH, NULL, error_log);
		return 1;
	}
	return 0;
}

void ShaderManager::Use()
{
	glUseProgram(program_id);
}