-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyHelpers.h
314 lines (266 loc) · 10 KB
/
MyHelpers.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
#include "Includes.h"
bool checkShaderCompile(GLuint shader);
void errorCallBack(int error, const char* description);
//const GLuint WIDTH = 800, HEIGHT = 600;
inline void ex()
{
cout << endl << "--------------------------------------------------" << endl;
cout << "Press Enter to continue ..." << endl;
getchar();
exit(0);
}
inline void errorCallBack(int error, const char* description)
{
cout << "========================================================" << endl;
cout << "ERROR: '" << error << "', DESCRIPTION: " << endl;
cout << description << endl;
cout << "========================================================" << endl;
}
///
/// \brief generateVertexShader create vertex shader
/// \param source the source code for the shader
/// \return success any integer or 0 for failure
///
inline GLuint generateVertexShader(const GLchar* source)
{
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
cout << "Checking vertex shader '" << shader << "' compile:" << endl;
bool success = checkShaderCompile(shader);
cout << "-----------------------------------------------" << endl;
return success ? shader : 0;
}
///
/// \brief GenerateGeometryShader creates geometry shader
/// \param source the source code for the shader
/// \return success any integer or 0 for failure
///
inline GLuint GenerateGeometryShader(const GLchar* source)
{
GLuint shader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
cout << "Checking geometry shader '" << shader << "' compile:" << endl;
bool success = checkShaderCompile(shader);
cout << "-----------------------------------------------" << endl;
return success ? shader : 0;
}
///
/// \brief generateFragmentShader creates the fragment shader
/// \param source the source code for the shader
/// \return success any integer and 0 for failure
///
inline GLuint generateFragmentShader(const GLchar* source)
{
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader, 1, &source, 0);
glCompileShader(shader);
cout << "Checking fragment shader '" << shader << "' compile:" << endl;
bool success = checkShaderCompile(shader);
cout << "-----------------------------------------------" << endl;
return success ? shader : 0;
}
///
/// \brief GenerateShader reads a whole file and then generates the shader
/// \param type shader type
/// \param filePath file path
/// \return returns the shader type
///
inline GLuint GenerateShader(GLenum type, const GLchar* filePath)
{
ifstream sourceFile;
sourceFile.open(filePath);
if (sourceFile.fail())
return 0;
// From here: http://www.gamedev.net/topic/353162-reading-a-whole-file-into-a-string-with-ifstream/
string sourceString(
(std::istreambuf_iterator<char>(sourceFile)),
std::istreambuf_iterator<char>()
);
const GLchar * source = sourceString.c_str();
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
cout << "Checking shader compile:" << endl;
checkShaderCompile(shader);
cout << "-----------------------------------------------" << endl;
return shader;
}
inline bool checkShaderCompile(GLuint shader)
{
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
char buffer[512];
glGetShaderInfoLog(shader, 512, nullptr, buffer);
cout << buffer << endl;
}
else
cout << "COMPILE OK!" << endl;
return status;
}
inline bool CheckProgramLink(GLuint program)
{
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status != GL_TRUE)
{
char buffer[512];
glGetProgramInfoLog(program, 512, nullptr, buffer);
cout << buffer << endl;
}
else
cout << "LINK OK!" << endl;
return status;
}
///
/// \brief GenerateShaderProgram Creates the shader program
/// \param vertexShader
/// \param fragmentShader
/// \param geometryShader
/// \return
///
inline GLuint GenerateShaderProgram(GLuint vertexShader, GLuint fragmentShader,
GLuint geometryShader)
{
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glAttachShader(program, geometryShader);
glLinkProgram(program);
cout << "Checking shader program link:" << endl;
bool success = CheckProgramLink(program);
cout << "-----------------------------------------------" << endl;
return success ? program : 0;
}
inline GLuint GenerateShaderProgram(GLuint vertexShader, GLuint fragmentShader)
{
return GenerateShaderProgram(vertexShader, fragmentShader, 0);
}
///
/// \brief loadTexture loads and binds the texture sets the wrap and filtering
/// \param texture texture type
/// \param texturePath file path of the texture
///
inline void loadTexture(GLuint texture, char* texturePath)
{
int width, height;
GLubyte* image;
glBindTexture(GL_TEXTURE_2D, texture);
image = SOIL_load_image(texturePath, &width, &height, nullptr, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
}
///
/// \brief loadTexture loads and binds the texture sets the wrap and filtering
/// \param texturePath file path of the texture
/// \return
///
inline GLuint loadTexture(char* texturePath)
{
GLuint textureId;
glGenTextures(1, &textureId);
int width, height;
GLubyte* image;
image = SOIL_load_image(texturePath, &width, &height, nullptr, SOIL_LOAD_RGB);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return textureId;
}
///
/// \brief loadTextureWithAlpha load and binds the texture sets the wrap and filtering
/// \param texturePath file path of the texture
/// \return
///
inline GLuint loadTextureWithAlpha(char* texturePath)
{
GLuint textureId;
glGenTextures(1, &textureId);
int width, height;
GLubyte* image;
image = SOIL_load_image(texturePath, &width, &height, nullptr, SOIL_LOAD_RGBA);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return textureId;
}
inline GLuint generateAttachmentTexture(GLuint screenWidth, GLuint screenHeight,
GLboolean depth, GLboolean stencil)
{
GLenum attachmentType = 0;
if (!depth && !stencil)
attachmentType = GL_RGB;
else if (depth && !stencil)
attachmentType = GL_DEPTH_COMPONENT;
else if (!depth && stencil)
attachmentType = GL_STENCIL_INDEX;
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
if (!depth && !stencil)
glTexImage2D(GL_TEXTURE_2D, 0, attachmentType, screenWidth, screenHeight,
0, attachmentType, GL_UNSIGNED_BYTE, nullptr);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, screenWidth, screenHeight,
0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return textureId;
}
inline GLuint LoadCubeMap(vector<const GLchar*> faces)
{
GLuint textureId;
glGenTextures(1, &textureId);
// glActiveTexture(GL_TEXTURE0);
int width, height;
GLubyte* image;
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
for (GLuint i = 0; i < faces.size(); i++)
{
image = SOIL_load_image(faces[i], &width, &height, nullptr, SOIL_LOAD_RGB);
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return textureId;
}
inline std::ostream& operator<<(std::ostream& stream, glm::vec3 vector)
{
stream << "x = " << vector.x << ", y = " << vector.y << ", z = " << vector.z;
return stream;
}