-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShader.cpp
207 lines (169 loc) · 4.26 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
#include "Shader.hpp"
#ifdef WIN32
#include <Windows.h>
#endif
#include <GL/glew.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#elif WIN32
#include <GL/GL.h>
#else
#include <GL/gl.h>
#endif
#include <fstream>
#include <iostream>
#include <cstring>
using std::string;
using std::ifstream;
using std::cerr;
//using std::cout;
using std::endl;
Shader::Shader(void) : program(-1)
{
}
Shader::Shader(const std::string& vertexShaderPath, const std::string& fragmentShaderPath) :
program(-1)
{
loadShaders(vertexShaderPath, fragmentShaderPath);
}
int Shader::getAttribLocation(const string& name)
{
if (program == -1)
{
return -1;
}
glUseProgram(program);
int attribute = glGetAttribLocation(program, name.c_str());
glUseProgram(0);
return attribute;
}
int Shader::getUniformLocation(const string& name)
{
if (program == -1)
{
return -1;
}
glUseProgram(program);
int uniform = glGetUniformLocation(program, name.c_str());
glUseProgram(0);
return uniform;
}
void Shader::loadShaders(const string& vsPath, const string& fsPath)
{
if (program != -1)
{
glDeleteProgram(program);
program = -1;
}
ifstream vs(vsPath.c_str());
if (!vs.good())
{
//#ifdef __clang__
cerr << "Failed to open vertex shader" << endl;
throw std::exception();
//#else
//throw std::exception("Failed to open vertex shader");
//#endif
}
ifstream fs(fsPath.c_str());
if (!fs.good())
{
vs.close();
//#ifdef __clang__
cerr << "Failed to open fragment shader" << endl;
throw std::exception();
//#else
//throw std::exception("Failed to open fragment shader");
//#endif
}
vs.seekg(0, std::ios::end);
std::streamoff size = vs.tellg();
vs.seekg(0, std::ios::beg);
char* vertexSource = new char[size];
memset(vertexSource, 0, size);
vs.read(vertexSource, size);
if (vs.bad())
{
//#ifdef __clang__
cerr << "Error reading vertex shader source" << endl;
throw std::exception();
//#else
//throw std::exception("Error reading vertex shader source");
//#endif
}
fs.seekg(0, std::ios::end);
size = fs.tellg();
fs.seekg(0, std::ios::beg);
char* fragmentSource = new char[size];
memset(fragmentSource, 0, size);
fs.read(fragmentSource, size);
if (fs.bad()) {
//#ifdef __clang__
cerr << "Error reading fragment shader source" << endl;
throw std::exception();
//#else
//throw std::exception("Error reading fragment shader source");
//#endif
}
int vShaderId = createShader(GL_VERTEX_SHADER, vertexSource);
int fShaderId = createShader(GL_FRAGMENT_SHADER, fragmentSource);
program = createProgram(vShaderId, fShaderId);
}
int Shader::createShader(int shaderType, const char* source)
{
// TODO check for errors
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint compileStatus;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == GL_FALSE)
{
GLint infoLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength);
char* log = new char[infoLength+1];
glGetShaderInfoLog(shader, infoLength, NULL, log);
string error = string(log);
delete [] log;
//#ifdef __clang__
cerr << error << endl;
throw std::exception();
//#else
//throw std::exception(error.c_str());
//#endif
}
return shader;
}
int Shader::createProgram(int vShader, int fShader)
{
// TODO check for errors
int program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
GLint linkStatus;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE)
{
GLint infoLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLength);
char* log = new char[infoLength+1];
glGetProgramInfoLog(program, infoLength, NULL, log);
string error = string(log);
delete [] log;
//#ifdef __clang__
cerr << error << endl;
throw std::exception();
//#else
//throw std::exception(error.c_str());
//#endif
}
return program;
}
Shader::~Shader(void)
{
if (program >= 0)
{
glDeleteProgram(program);
}
}