-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderClass.cpp
137 lines (120 loc) · 4.04 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
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
#include "ShaderClass.h"
std::string get_file_contents(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();
// std::cout << contents << std::endl;
return (contents);
}
else
{
std::cout << "Could not read file: " << filename << std::endl;
}
throw (errno);
}
Shader::Shader(const char* vertexFile, const char* fragmentFile)
{
std::string vertexCode = get_file_contents(vertexFile);
std::string fragmentCode = get_file_contents(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);
compileErrors(vertexShader, "VERTEX", vertexFile);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
compileErrors(fragmentShader, "FRAGMENT", fragmentFile);
this->ID = glCreateProgram();
glAttachShader(this->ID, vertexShader);
glAttachShader(this->ID, fragmentShader);
glLinkProgram(this->ID);
compileErrors(this->ID, "PROGRAM", "LINK");
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void Shader::Activate()
{
glUseProgram(this->ID);
}
void Shader::Delete()
{
glDeleteProgram(this->ID);
}
void Shader::compileErrors( unsigned int shader, const char* type, const char* name ) {
GLint hasCompiled;
char infoLog[1024];
if ( std::strcmp(type, "PROGRAM") != 0 )
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &hasCompiled);
if (hasCompiled == GL_FALSE)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << __FILE__ << ": line " << __LINE__ << ":SHADER_COMPILATION_ERROR for: " << type << " file: " << name << "\n\n"
<< infoLog << "\n\n";
return;
}
}
else
{
glGetProgramiv( shader, GL_LINK_STATUS, &hasCompiled );
if (hasCompiled == GL_FALSE)
{
GLsizei length;
glGetProgramInfoLog(shader, 1024, &length, infoLog);
std::cout <<__FILE__ << ": line " << __LINE__ << ": SHADER_LINKING_ERROR for: " << type << "\n\n" << length << " " << infoLog << "\n\n";
return;
}
}
if (hasCompiled == GL_TRUE)
{
std::cout << name << " has compiled successfully. \n\n";
}
}
void Shader::SetFloatUniform1f( const char* uniform, float value ) {
GLint getUniform = glGetUniformLocation(this->ID, uniform);
if( getUniform != -1 )
glUniform1f( getUniform, value );
else
{
// Inactive shader uniform variable also returns -1
// Ref: https://stackoverflow.com/questions/47871575/glgetuniformlocation-returns-1-for-samplers-other-than-first
std::cout << "Shader Error: " << __FILE__ << " line " << __LINE__ << ": GLSL uniform variable " << uniform << " not active!" << std::endl;
exit(1);
//std::cin.get();
}
}
void Shader::SetFloatUniform3f( const char* uniform, float v0, float v1, float v2 ) {
GLint getUniform = glGetUniformLocation( this->ID, uniform );
if( getUniform != -1 )
glUniform3f( getUniform, v0, v1, v2 );
else
{
// Inactive shader uniform variable also returns -1
// Ref: https://stackoverflow.com/questions/47871575/glgetuniformlocation-returns-1-for-samplers-other-than-first
std::cout << "Shader Error: " << __FILE__ << " line " << __LINE__ << ": GLSL uniform variable " << uniform << " not active!" << std::endl;
exit(1);
//std::cin.get();
}
}
void Shader::SetFloatVecUniform3fv( const char* uniform, glm::vec3 v) {
GLint getUniform = glGetUniformLocation( this->ID, uniform );
if( getUniform != -1 )
glUniform3fv( getUniform, 1, &v[0] );
else
{
// Inactive shader uniform variable also returns -1
// Ref: https://stackoverflow.com/questions/47871575/glgetuniformlocation-returns-1-for-samplers-other-than-first
std::cout << "Shader Error: " << __FILE__ << " line " << __LINE__ << ": GLSL uniform variable " << uniform << " not active!" << std::endl;
exit(1);
//std::cin.get();
}
}