-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.C
179 lines (153 loc) · 4.5 KB
/
Shader.C
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
// Shader.C
// Author: Matt Stine
#include <GL/gl.h>
#include <string>
#include <iterator>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include "glext.h"
#include "Shader.h"
using namespace std;
string shader::readShaderFile (string fileName)
{
// Check for empty filename string.
if (!fileName.size()) {
throw(invalid_argument("Empty shader filename string."));
}
// Attempt to open file.
ifstream is(fileName.c_str());
is.unsetf(ios::skipws);
istream_iterator<char> ii(is);
istream_iterator<char> eos;
if (!is) {
throw(runtime_error("Could not open file " + fileName));
}
// Load file.
string content(ii, eos);
is.close();
if (!content.size()) {
throw(runtime_error("Empty file " + fileName));
}
return content;
}
GLuint shader::compileShader(GLenum shaderType,
string shaderText)
{
// Determine the type of shader being created.
string shaderTypeText;
switch (shaderType) {
case GL_VERTEX_SHADER:
shaderTypeText = "Vertex";
break;
case GL_TESS_CONTROL_SHADER:
shaderTypeText = "TessControl";
break;
case GL_TESS_EVALUATION_SHADER:
shaderTypeText = "TessEvaluation";
break;
case GL_GEOMETRY_SHADER:
shaderTypeText = "Geometry";
break;
case GL_FRAGMENT_SHADER:
shaderTypeText = "Fragment";
break;
default:
throw(invalid_argument("Invalid shader type"));
}
// Ensure we're provided with valid shader text.
if (!shaderText.size()) {
throw(invalid_argument(shaderTypeText + " shader - empty shader text"));
}
// Create the requested type of shader.
GLuint shaderHandle = glCreateShader(shaderType);
if (!glIsShader(shaderHandle)) {
throw(runtime_error(shaderTypeText + " shader - failed to create handle"));
}
// Compile the shader.
GLint compileSuccess;
GLchar compilerSpew[256];
const char *text = shaderText.c_str();
glShaderSource(shaderHandle, 1, &text, 0);
glCompileShader(shaderHandle);
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
glGetShaderInfoLog(shaderHandle, sizeof(compilerSpew), 0, compilerSpew);
if (!compileSuccess) {
glDeleteShader(shaderHandle);
throw(runtime_error(shaderTypeText + " shader - compilation error:\n" +
compilerSpew));
}
return shaderHandle;
}
// NOTE: Currently only supports vertex and fragment shaders.
bool shader::loadShader(const char *vS,
const char *tcS,
const char *teS,
const char *gS,
const char *fS,
GLuint &programHandle)
{
GLint linkSuccess = 1;
GLchar linkerSpew[256];
// Load source for each shader
string vsSource;
string fsSource;
string tcsSource;
string tesSource;
string gsSource;
GLuint vsHandle = 0;
GLuint fsHandle = 0;
GLuint tesHandle = 0;
GLuint tcsHandle = 0;
GLuint gsHandle = 0;
try {
// Create a program handle.
programHandle = glCreateProgram();
if (!glIsProgram(programHandle)) {
throw(runtime_error("Failed to create shader program handle"));
}
// Read, compile, and attach each shader object to the program.
vsSource = readShaderFile(vS);
vsHandle = compileShader(GL_VERTEX_SHADER, vsSource);
glAttachShader(programHandle, vsHandle);
fsSource = readShaderFile(fS);
fsHandle = compileShader(GL_FRAGMENT_SHADER, fsSource);
glAttachShader(programHandle, fsHandle);
if (tcS) {
tcsSource = readShaderFile(tcS);
tcsHandle = compileShader(GL_TESS_CONTROL_SHADER, tcsSource);
glAttachShader(programHandle, tcsHandle);
}
if (teS) {
tesSource = readShaderFile(teS);
tesHandle = compileShader(GL_TESS_EVALUATION_SHADER, tesSource);
glAttachShader(programHandle, tesHandle);
}
if (gS) {
gsSource = readShaderFile(gS);
gsHandle = compileShader(GL_GEOMETRY_SHADER, gsSource);
glAttachShader(programHandle, gsHandle);
}
// Link the shader program.
glLinkProgram(programHandle);
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
glGetProgramInfoLog(programHandle, sizeof(linkerSpew), 0, linkerSpew);
if (!linkSuccess) {
throw(runtime_error("Shader program linking error:\n" +
string(linkerSpew)));
}
}
catch (exception &e) {
// Handle errors.
glDeleteShader(vsHandle);
glDeleteShader(fsHandle);
glDeleteShader(tesHandle);
glDeleteShader(tcsHandle);
glDeleteShader(gsHandle);
glDeleteProgram(programHandle);
programHandle = 0;
cerr << "ERROR creating shader program: " << e.what() << endl;
return false;
}
return true;
}