-
Notifications
You must be signed in to change notification settings - Fork 2
/
shader.cpp
58 lines (42 loc) · 1.12 KB
/
shader.cpp
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
#include "shader.h"
namespace core {
namespace graphics {
Shader::Shader(const char* vertexPath, const char* fragmentPath)
: vertexPath(vertexPath), fragmentPath(fragmentPath)
{
shaderID = load();
}
Shader::~Shader()
{
glDeleteProgram(shaderID);
}
GLuint Shader::load()
{
GLuint program = glCreateProgram();
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertexSrcString = read_file(vertexPath);
std::string fragmentSrcString = read_file(fragmentPath);
const char* vertexSource = vertexSrcString.c_str();
const char* fragmentSource = fragmentSrcString.c_str();
glShaderSource(vertex, 1, &vertexSource, NULL);
glCompileShader(vertex);
glShaderSource(fragment, 1, &fragmentSource, NULL);
glCompileShader(fragment);
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vertex);
glDeleteShader(fragment);
return program;
}
void Shader::enable()
{
glUseProgram(shaderID);
}
void Shader::disable()
{
glUseProgram(0);
}
}}