-
Notifications
You must be signed in to change notification settings - Fork 7
/
shader.cpp
215 lines (180 loc) · 5.38 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Perry Kivolowitz - University of Wisconsin - Madison
Computer Sciences Department
A sample hello world like program demonstrating modern
OpenGL techniques.
Created: 2/25/13
Updates: 9/29/15 Refreshed.
*/
#include "shader.h"
#include <vector>
#include <assert.h>
using namespace std;
using namespace glm;
#define BAD_GL_VALUE GLuint(-1)
Shader::Shader()
{
this->is_used = false;
this->vertex_shader_id = BAD_GL_VALUE;
this->fragment_shader_id = BAD_GL_VALUE;
this->program_id = BAD_GL_VALUE;
}
/* This Shader() class implements or assumes a basic set of uniforms will be
provided to all shaders derived from it. These are listed below.
Shader::CommonSetup() can be used by call derived classes to send the
common values to the shader. Values unique to the derived class can be
loaded with the CustomShader() function.
*/
void Shader::Use()
{
assert(this->program_id != BAD_GL_VALUE);
assert(this->is_used == false);
glUseProgram(this->program_id);
this->is_used = true;
}
void Shader::UnUse()
{
assert(this->program_id != BAD_GL_VALUE);
assert(this->is_used == true);
glUseProgram(0);
this->is_used = false;
}
/* The shader initialization code is lifted liberally from the GLSL 4.0 Cookbook.
*/
bool Shader::Initialize(string vertex_shader_file, string fragment_shader_file)
{
GLint check_value;
if (GLReturnedError("Shader::Initialize - on entrance"))
return false;
this->vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
this->LoadShader(vertex_shader_file, this->vertex_shader_id);
glCompileShader(this->vertex_shader_id);
glGetShaderiv(this->vertex_shader_id, GL_COMPILE_STATUS, &check_value);
if (check_value != GL_TRUE)
{
cerr << this->GetShaderLog(vertex_shader_id).str();
cerr << "GLSL compilation failed - vertex shader: " << vertex_shader_file << endl;
return false;
}
if (GLReturnedError("Shader::Initialize - after processing vertex shader"))
return false;
this->fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
this->LoadShader(fragment_shader_file, this->fragment_shader_id);
glCompileShader(this->fragment_shader_id);
glGetShaderiv(this->fragment_shader_id, GL_COMPILE_STATUS, &check_value);
if (check_value != GL_TRUE)
{
cerr << this->GetShaderLog(fragment_shader_id).str();
cerr << "GLSL compilation failed - fragment shader: " << fragment_shader_file << endl;
return false;
}
this->program_id = glCreateProgram();
glAttachShader(this->program_id, this->vertex_shader_id);
glAttachShader(this->program_id, this->fragment_shader_id);
glLinkProgram(program_id);
glDeleteShader(vertex_shader_id);
glDeleteShader(fragment_shader_id);
return !GLReturnedError("Shader::Initialize - on exit");
}
void Shader::TakeDown()
{
GLint temp = 0;
GLsizei size;
if (this->program_id == (GLuint)-1)
return;
glGetProgramiv(this->program_id, GL_ATTACHED_SHADERS, &temp);
if (temp > 0)
{
GLuint * shader_list = new GLuint[temp];
glGetAttachedShaders(this->program_id, temp, &size, shader_list);
for (GLsizei i = 0; i < size; i++)
{
glDetachShader(this->program_id, shader_list[i]);
// The shaders were marked for deletion
// immediately after they were created.
}
delete[] shader_list;
}
glDeleteProgram(this->program_id);
this->program_id = (GLuint)-1;
}
/*
This function is adapted from OpenGL 4.0 Shading Language Cookbook by David Wolff.
*/
bool Shader::LoadShader(string file_name, GLuint shader_id)
{
assert(file_name.length() > 0);
if (GLReturnedError("Shader::LoadShader - on entrance"))
return false;
FILE * file_handle = NULL;
fopen_s(&file_handle, file_name.c_str(), "rb");
if (file_handle == NULL)
{
cerr << "Cannot open shader: " << file_name << endl;
return false;
}
fseek(file_handle, 0, SEEK_END);
int length = ftell(file_handle);
fseek(file_handle, 0, SEEK_SET);
GLubyte * buffer = new GLubyte[length + 1];
fread(buffer, sizeof(GLubyte), length, file_handle);
fclose(file_handle);
buffer[length] = 0;
glShaderSource(shader_id, 1, (const char **)&buffer, NULL);
delete[] buffer;
return !GLReturnedError("Shader::LoadShader - on exit");
}
/*
This function is adapted from OpenGL 4.0 Shading Language Cookbook by David Wolff.
*/
stringstream Shader::GetShaderLog(GLuint shader_id)
{
stringstream s;
GLint log_length;
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length <= 0)
s << "No shader log information available." << endl;
else
{
GLchar * buffer = new GLchar[log_length];
glGetShaderInfoLog(shader_id, log_length, NULL, buffer);
s << "Shader log:" << endl;
s << buffer << endl;
delete[] buffer;
}
return s;
}
bool Shader::GLReturnedError(char * s)
{
bool return_error = false;
#ifdef _DEBUG
GLenum glerror;
while ((glerror = glGetError()) != GL_NO_ERROR)
{
return_error = true;
cerr << s << ": " << gluErrorString(glerror) << endl;
}
#endif // _DEBUG
return return_error;
}
bool ShaderInitializer::Initialize(vector<ShaderInitializer> * shaders)
{
for (vector<ShaderInitializer>::iterator i = shaders->begin(); i < shaders->end(); i++)
{
if (!(*i).shader->Initialize((*i).vertex_shader_file_name, (*i).fragment_shader_file_name))
{
cerr << (*i).error_string << endl;
cerr << "Hit enter to exit:";
cin.get();
return false;
}
(*i).shader->CustomSetup();
}
return true;
}
void ShaderInitializer::TakeDown(vector<ShaderInitializer> * shaders)
{
for (vector<ShaderInitializer>::iterator i = shaders->begin(); i < shaders->end(); i++)
{
(*i).shader->TakeDown();
}
}