-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleShaderProgram.h
executable file
·264 lines (244 loc) · 8.14 KB
/
SimpleShaderProgram.h
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SimpleShaderProgram.h
#ifndef __SIMPLESHADERPROGRAM_H__
#define __SIMPLESHADERPROGRAM_H__
/* Include-order dependency!
*
* GLEW must be included before the standard GL.h header.
*/
#include "findGL.h" // figure out where this machine has GL installed and include it
#include <string>
#include <vector>
#include <assert.h>
#include <fstream>
#include <sstream>
/**
* SimpleShaderProgram - class to use GLSL programs.
* Call LoadVertexShader and UploadFragmentShader
* to add shaders to the program. Call Bind() to begin
* using the shader and UnBind() to stop using it.
*/
#define GLEW_VERSION_2_0 1
class SimpleShaderProgram {
public:
SimpleShaderProgram() {
if(GLEW_VERSION_2_0)
programid = glCreateProgram();
#ifndef __APPLE__
else
programid = glCreateProgramObjectARB();
#endif
}
~SimpleShaderProgram() {
if(GLEW_VERSION_2_0)
glDeleteProgram(programid);
#ifndef __APPLE__
else
glDeleteObjectARB(programid);
#endif
}
//
// You need to add at least one vertex shader and one fragment shader. You can add multiple shaders
// of each type as long as only one of them has a main() function.
//
void LoadVertexShader(const std::string& filename) {
std::ifstream in(filename.c_str());
if(!in) {
fprintf(stderr, "Failed to open shader file '%s'\n", filename.c_str());
assert(false);
return;
}
std::stringstream ss;
ss << in.rdbuf();
std::string str = ss.str();
const char* ptr = str.c_str();
// Buffer for error messages
static const int kBufferSize = 1024;
char buffer[1024];
if(GLEW_VERSION_2_0)
{
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 1, &ptr, NULL);
glCompileShader(shader);
GLint result = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result != GL_TRUE) {
GLsizei length = 0;
glGetShaderInfoLog(shader, kBufferSize-1,
&length, buffer);
fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer);
assert(false);
}
glAttachShader(programid, shader);
glLinkProgram(programid);
}
#ifndef __APPLE__
else
{
GLuint shader = glCreateShaderObjectARB(GL_VERTEX_SHADER);
glShaderSourceARB(shader, 1, &ptr, NULL);
glCompileShaderARB(shader);
GLint result = 0;
glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &result);
if(result != GL_TRUE) {
GLsizei length = 0;
glGetInfoLogARB(shader, kBufferSize-1,
&length, buffer);
fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer);
assert(false);
}
glAttachObjectARB(programid, shader);
glLinkProgramARB(programid);
}
#endif
}
void LoadFragmentShader(const std::string& filename) {
std::ifstream in(filename.c_str());
if(!in) {
fprintf(stderr, "Failed to open shader file '%s'\n", filename.c_str());
assert(false);
return;
}
std::stringstream ss;
ss << in.rdbuf();
std::string str = ss.str();
const char* ptr = str.c_str();
// Buffer for error messages
static const int kBufferSize = 1024;
char buffer[1024];
if(GLEW_VERSION_2_0)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, &ptr, NULL);
glCompileShader(shader);
GLint result = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result != GL_TRUE) {
GLsizei length = 0;
glGetShaderInfoLog(shader, kBufferSize-1,
&length, buffer);
fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer);
assert(false);
}
glAttachShader(programid, shader);
glLinkProgram(programid);
}
#ifndef __APPLE__
else
{
GLuint shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER);
glShaderSourceARB(shader, 1, &ptr, NULL);
glCompileShaderARB(shader);
GLint result = 0;
glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &result);
if(result != GL_TRUE) {
GLsizei length = 0;
glGetInfoLogARB(shader, kBufferSize-1,
&length, buffer);
fprintf(stderr, "%s: GLSL error\n%s\n", filename.c_str(), buffer);
assert(false);
}
glAttachObjectARB(programid, shader);
glLinkProgramARB(programid);
}
#endif
}
void Bind() {
if(GLEW_VERSION_2_0)
glUseProgram(programid);
#ifndef __APPLE__
else
glUseProgramObjectARB(programid);
#endif
for (unsigned int i = 0; i < textures.size(); i++) {
if (GLEW_VERSION_2_0)
glUniform1i(textures[i].location, textures[i].tex_id);
#ifndef __APPLE__
else
glUniform1iARB(textures[i].location, textures[i].tex_id);
#endif
}
}
void UnBind() {
if(GLEW_VERSION_2_0)
glUseProgram(0);
else
glUseProgramObjectARB(0);
}
//
// Set a uniform global parameter of the program by name.
//
void SetTexture(const std::string& name, int tex_index) {
GLint location = glGetUniformLocation(programid, name.c_str());
UnboundTexture tex;
tex.location = location;
tex.tex_id = tex_index;
textures.push_back(tex);
}
void SetUniform(const std::string& name, float value) {
GLint location = GetUniformLocation(name);
if (location == -1) return;
if(GLEW_VERSION_2_0) {
glUniform1f(location, value);
}
else {
glUniform1fARB(location, value);
}
}
void SetUniform(const std::string& name, float v0, float v1) {
GLint location = GetUniformLocation(name);
if (location == -1) return;
if(GLEW_VERSION_2_0) {
glUniform2f(location, v0, v1);
}
else {
glUniform2fARB(location, v0, v1);
}
}
void SetUniform(const std::string& name, float v0, float v1, float v2) {
GLint location = GetUniformLocation(name);
if (location == -1) return;
if(GLEW_VERSION_2_0) {
glUniform3f(location, v0, v1, v2);
}
else {
glUniform3fARB(location, v0, v1, v2);
}
}
void SetUniform(const std::string& name, float v0, float v1, float v2, float v3) {
GLint location = GetUniformLocation(name);
if (location == -1) return;
if(GLEW_VERSION_2_0) {
glUniform4f(location, v0, v1, v2, v3);
}
else {
glUniform4fARB(location, v0, v1, v2, v3);
}
}
void SetUniform1fv(const std::string& name, float* floats, int count) {
int location = GetUniformLocation(name);
glUniform1fv(location, count, floats);
}
private:
//
// Helper routine - get the location for a uniform shader parameter.
//
GLint GetUniformLocation(const std::string& name) {
if(GLEW_VERSION_2_0) {
return glGetUniformLocation(programid, name.c_str());
}
#ifndef __APPLE__
else {
return glGetUniformLocationARB(programid, name.c_str());
}
#endif
return -1;
}
// OpenGL program object id.
unsigned int programid;
struct UnboundTexture {
GLint location;
GLint tex_id;
};
std::vector<UnboundTexture> textures;
};
#endif //__SIMPLESHADERPROGRAM_H__