-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderClass.cpp
74 lines (62 loc) · 1.66 KB
/
ShaderClass.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "ShaderClass.h"
std::string getFileContents(const char* filename)
{
std::ifstream in(filename, std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return contents;
}
throw errno;
}
Shader::Shader(const char* vertexFile, const char* fragmentFile)
{
std::string vertexCode = getFileContents(vertexFile);
std::string fragmentCode = getFileContents(fragmentFile);
const char* vertexSource = vertexCode.c_str();
const char* fragmentSource = fragmentCode.c_str();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
GLint shaderStatus;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &shaderStatus);
if (shaderStatus == GL_FALSE)
{
std::cout << "Failed to compile vertex shader" << std::endl;
}
else
{
std::cout << "Compiled vertex shader!" << std::endl;
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &shaderStatus);
if (shaderStatus == GL_FALSE)
{
std::cout << "Failed to compile fragment shader" << std::endl;
}
else
{
std::cout << "Compiled fragment shader!" << std::endl;
}
id = glCreateProgram();
glAttachShader(id, vertexShader);
glAttachShader(id, fragmentShader);
glLinkProgram(id);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void Shader::Activate()
{
glUseProgram(id);
}
void Shader::Delete()
{
glDeleteProgram(id);
}