-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShader.cpp
executable file
·141 lines (123 loc) · 2.91 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
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
132
133
134
135
136
137
138
139
140
141
#include "Shader.h"
#include <fstream>
#include <iostream>
#include <exception>
Shader::Shader(std::string path, GLenum shaderType) :
Path(path), ShaderType(shaderType)
{
Name = glCreateShader(shaderType);
Load(path);
Compile();
}
Shader::Shader(std::string path) :
Path(path)
{
std::string extension = path.substr(path.length() - 4, 4);
GLenum shaderType;
if (extension == "vert")
shaderType = GL_VERTEX_SHADER;
else if (extension == "tesc")
shaderType = GL_TESS_CONTROL_SHADER;
else if (extension == "tese")
shaderType = GL_TESS_EVALUATION_SHADER;
else if (extension == "geom")
shaderType = GL_GEOMETRY_SHADER;
else if (extension == "frag")
shaderType = GL_FRAGMENT_SHADER;
Name = glCreateShader(shaderType);
Load(path);
Compile();
}
Shader::~Shader()
{
glDeleteShader(Name);
}
Shader::Shader(const Shader& other) :
Name(0)
{
*this = other;
}
Shader& Shader::operator=(const Shader& other)
{
GLint length;
glGetShaderiv(other.Name, GL_SHADER_SOURCE_LENGTH, &length);
std::vector<char> buffer(length);
char *src = buffer.data();
glGetShaderSource(other.Name, length, nullptr, src);
glDeleteShader(Name);
Name = glCreateShader(other.ShaderType);
ShaderType = other.ShaderType;
Path = other.Path;
glShaderSource(Name, 1, &src, nullptr);
Compile();
return *this;
}
Shader::Shader(Shader&& other) :
Name(0)
{
*this = std::move(other);
}
Shader& Shader::operator=(Shader&& other)
{
if (this != &other)
{
glDeleteShader(Name);
Name = other.Name;
ShaderType = other.ShaderType;
Path = other.Path;
other.Name = 0;
other.ShaderType = 0;
other.Path.clear();
}
return *this;
}
void Shader::Load(std::string path)
{
std::vector<char> buffer;
ReadSource(path.c_str(), buffer);
const char *src = buffer.data();
glShaderSource(Name, 1, &src, nullptr);
}
void Shader::Compile()
{
// Compile the shader
glCompileShader(Name);
// Check the result of the compilation
GLint isCompiled;
glGetShaderiv(Name, GL_COMPILE_STATUS, &isCompiled);
if (!isCompiled)
{
std::cerr << "Shader compilation (" << Path <<
") failed with this message:" << std::endl;
GLint maxLength = 0;
glGetShaderiv(Name, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(Name, maxLength, &maxLength, &infoLog[0]);
std::cerr << &infoLog[0] << std::endl;
throw std::exception();
}
}
void Shader::ReadSource(const char *fname, std::vector<char> &buffer)
{
std::ifstream in;
in.open(fname, std::ios::binary);
if (in.is_open())
{
// Get the number of bytes stored in this file
in.seekg(0, std::ios::end);
size_t length = (size_t)in.tellg();
// Go to start of the file
in.seekg(0, std::ios::beg);
// Read the content of the file in a buffer
buffer.resize(length + 1);
in.read(&buffer[0], length);
in.close();
// Add a valid C - string end
buffer[length] = '\0';
}
else
{
std::cerr << "Unable to open " << fname << std::endl;
throw std::exception();
}
}