-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.cpp
236 lines (170 loc) · 6.81 KB
/
helpers.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "helpers.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <png.h>
#include <exception>
#include <random>
#include <regex>
#include <filesystem>
void glErrorToConsole(const std::string & huidigeActie)
{
GLenum Fout = glGetError();
if(Fout == GL_NO_ERROR)
return;
if(huidigeActie != "")
std::cout << huidigeActie << ": ";
switch(Fout)
{
case GL_INVALID_FRAMEBUFFER_OPERATION: std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION!"; break;
case GL_INVALID_ENUM: std::cout << "GL_INVALID_ENUM!"; break;
case GL_OUT_OF_MEMORY: std::cout << "GL_OUT_OF_MEMORY!"; break;
case GL_STACK_UNDERFLOW: std::cout << "GL_STACK_UNDERFLOW!"; break;
case GL_STACK_OVERFLOW: std::cout << "GL_STACK_OVERFLOW!"; break;
case GL_INVALID_VALUE: std::cout << "GL_INVALID_VALUE!"; break;
case GL_INVALID_OPERATION: std::cout << "GL_INVALID_OPERATION!"; break;
default: std::cout << "Onbekende fout!"; break;
}
std::cout << std::endl;
throw std::runtime_error("...");
}
bool checkvoorshadercompileerfout(GLuint shader, const std::string & naam)
{
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the nullptr character
std::string errorLog;
errorLog.resize(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, errorLog.data());
std::cout << "Shadercompilatie van " << naam << " is gefaald..\nCheck dit: " << errorLog.data();
glDeleteShader(shader); //Don't leak the shader.
return false;
}
return true;
}
std::string GetShaderTypeString(GLenum ShaderType)
{
switch(ShaderType)
{
case GL_COMPUTE_SHADER: return "GL_COMPUTE_SHADER";
case GL_VERTEX_SHADER: return "GL_VERTEX_SHADER";
case GL_TESS_CONTROL_SHADER: return "GL_TESS_CONTROL_SHADER";
case GL_TESS_EVALUATION_SHADER: return "GL_TESS_EVALUATION_SHADER";
case GL_GEOMETRY_SHADER: return "GL_GEOMETRY_SHADER";
case GL_FRAGMENT_SHADER: return "GL_FRAGMENT_SHADER";
default: return "Unknown" ;
}
}
std::string tekstInlezen(const std::string & bestandsNaam)
{
using namespace std::filesystem;
path bestand(bestandsNaam);
std::ifstream leesShader(bestandsNaam);
if(!leesShader.is_open())
throw std::runtime_error("Het openen van de shader " + bestandsNaam + " is helaas niet gelukt...");
std::regex headers("\\s*#include\\s+\"(\\w+\\.\\w+)\"\\s*");
std::stringstream touw;
std::string regel;
std::smatch m;
while(std::getline(leesShader, regel))
touw << (std::regex_match(regel, m, headers) ? tekstInlezen(bestand.replace_filename(path(m[1])).string()) : regel) << "\n";
if(false)
{
const char * streepjes = "-----------------";
std::cout << "\n" << streepjes << "tekstInlezen('" << bestandsNaam << "')" << streepjes << "\n" << touw.str() << streepjes << "\nEinde '" << bestandsNaam << "'" << streepjes << std::endl;
}
return touw.str();
}
GLuint _maakShaderObject(const std::string & shaderBestandsnaam, GLenum shadertype)
{
GLuint shaderobject = glCreateShader(shadertype);
std::string shaderSource = tekstInlezen(shaderBestandsnaam);
//The following seems a bit weird no?
char const * pointerToCStr = shaderSource.c_str();
char const ** shadersourcefakearray = const_cast<char const **>(&pointerToCStr);
glShaderSource(shaderobject, 1, shadersourcefakearray, nullptr);
std::cout << "Compiling " << shaderBestandsnaam << "..." << std::endl;
glCompileShader(shaderobject);
if(!checkvoorshadercompileerfout(shaderobject, shaderBestandsnaam.c_str()))
exit(1);
return shaderobject;
}
GLuint _maakGeometrieShader(const std::string & vertShaderBestandsnaam, const std::string & fragShaderBestandsnaam, const std::string & geomShaderBestandsnaam)
{
GLuint vertshaderobject = _maakShaderObject(vertShaderBestandsnaam, GL_VERTEX_SHADER );
GLuint fragshaderobject = _maakShaderObject(fragShaderBestandsnaam, GL_FRAGMENT_SHADER );
GLuint geomshaderobject = _maakShaderObject(geomShaderBestandsnaam, GL_GEOMETRY_SHADER );
GLuint prog = glCreateProgram();
glAttachShader(prog, fragshaderobject);
glAttachShader(prog, vertshaderobject);
glAttachShader(prog, geomshaderobject);
glLinkProgram(prog);
return prog;
}
GLuint _maakVlakVerdelingShader( const std::string & vertShaderBestandsnaam, const std::string & fragShaderBestandsnaam,
const std::string & vlakEvalBestandsnaam, const std::string & vlakCtrlBestandsnaam)
{
GLuint vertshaderobject = _maakShaderObject(vertShaderBestandsnaam, GL_VERTEX_SHADER );
GLuint fragshaderobject = _maakShaderObject(fragShaderBestandsnaam, GL_FRAGMENT_SHADER );
GLuint tessEvalObject = _maakShaderObject(vlakEvalBestandsnaam, GL_TESS_EVALUATION_SHADER);
GLuint tessCtrlObject = vlakCtrlBestandsnaam == "" ? 0 : _maakShaderObject(vlakCtrlBestandsnaam, GL_TESS_CONTROL_SHADER);
GLuint prog = glCreateProgram();
glAttachShader(prog, fragshaderobject);
glAttachShader(prog, vertshaderobject);
glAttachShader(prog, tessEvalObject);
if(vlakCtrlBestandsnaam != "")
glAttachShader(prog, tessCtrlObject);
glLinkProgram(prog);
return prog;
}
GLuint _maakShader(const std::string & vertShaderBestandsnaam, const std::string & fragShaderBestandsnaam)
{
GLuint vertshaderobject = _maakShaderObject(vertShaderBestandsnaam, GL_VERTEX_SHADER );
GLuint fragshaderobject = _maakShaderObject(fragShaderBestandsnaam, GL_FRAGMENT_SHADER );
GLuint prog = glCreateProgram();
glAttachShader(prog, fragshaderobject);
glAttachShader(prog, vertshaderobject);
glLinkProgram(prog);
return prog;
}
GLuint _maakBerekenShader(const std::string & shaderBestandsnaam)
{
GLuint compshaderobject = _maakShaderObject(shaderBestandsnaam, GL_COMPUTE_SHADER);
GLuint prog = glCreateProgram();
glAttachShader(prog, compshaderobject);
glLinkProgram(prog);
return prog;
}
glm::vec3 willekeurigeVec3()
{
static std::random_device willekeur;
static std::mt19937 bemonsteraar(willekeur());
static std::uniform_real_distribution<> greep(-1.0, 1.0);
return glm::vec3(greep(bemonsteraar), greep(bemonsteraar), greep(bemonsteraar));
}
png_byte * laadPNG(const std::string & bestandsnaam, size_t & width, size_t & height, size_t & kanalen)
{
png_image image;
memset(&image, 0, (sizeof image));
image.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_file(&image, bestandsnaam.c_str()))
{
png_bytep buffer = nullptr;
image.format = PNG_FORMAT_RGBA;
buffer = new png_byte[PNG_IMAGE_SIZE(image)];
if (buffer != nullptr && png_image_finish_read(&image, nullptr, buffer, 0, nullptr) != 0)
{
width = image.width;
height = image.height;
kanalen = 4;
std::cout << "Plaatje '" << bestandsnaam << "' geladen en formaat: " << width << " X " << height << std::endl;
return buffer;
}
delete buffer;
}
return nullptr;
}