-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineUtil.cpp
More file actions
648 lines (551 loc) · 18.1 KB
/
Copy pathEngineUtil.cpp
File metadata and controls
648 lines (551 loc) · 18.1 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//-------------------------------------------------------------------------//
// MyUtil.h
// Some utility stuff - file reading and such
//
// David Cline
// 6/14/2014
//-------------------------------------------------------------------------//
// Local includes
#include "EngineUtil.h"
//-------------------------------------------------------------------------//
// MISCELLANEOUS
//-------------------------------------------------------------------------//
void ERROR(const string &msg, bool doExit)
{
cerr << "\nERROR! " << msg << endl;
if (doExit) exit(0);
}
double TIME(void)
{
return (double)clock() / (double)CLOCKS_PER_SEC;
}
void SLEEP(int millis)
{
this_thread::sleep_for(chrono::milliseconds(1));
}
//-------------------------------------------------------------------------//
// OPENGL STUFF
//-------------------------------------------------------------------------//
GLFWwindow* createOpenGLWindow(int width, int height, const char *title, int samplesPerPixel)
{
// Initialise GLFW
if (!glfwInit()) ERROR("Failed to initialize GLFW.", true);
glfwWindowHint(GLFW_SAMPLES, samplesPerPixel);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, MAJOR_VERSION);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, MINOR_VERSION);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Open a window and create its OpenGL context
GLFWwindow *window = glfwCreateWindow(width, height, title, NULL, NULL);
if (window == NULL) {
glfwTerminate();
ERROR("Failed to open GLFW window.", true);
}
glfwMakeContextCurrent(window);
// Ensure we can capture the escape key being pressed
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
glfwTerminate();
ERROR("Failed to initialize GLEW.", true);
}
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_FRONT);
// Print the OpenGL version we are working with
char *GL_version = (char *)glGetString(GL_VERSION);
printf("OpenGL Version: %s\n", GL_version);
return window;
}
//-------------------------------------------------------------------------//
GLuint loadShader(const string &fileName, GLuint shaderType)
{
// load the shader as a file
string mainCode;
if (!loadFileAsString(fileName, mainCode)) {
ERROR("Could not load file '" + fileName + "'", false);
return NULL_HANDLE;
}
string shaderCode;
string alreadyIncluded = fileName;
replaceIncludes(mainCode, shaderCode, "#include", alreadyIncluded, true);
// print the shader code
#ifdef _DEBUG
cout << "\n----------------------------------------------- SHADER CODE:\n";
cout << shaderCode << endl;
cout << "--------------------------------------------------------------\n";
#endif
// transfer shader code to card and compile
GLuint shaderHandle = glCreateShader(shaderType); // create handle for the shader
const char* source = shaderCode.c_str(); // get C style string for shader code
glShaderSource(shaderHandle, 1, &source, NULL); // pass the shader code to the card
glCompileShader(shaderHandle); // attempt to compile the shader
// check to see if compilation worked
// If the compilation did not work, print an error message and return NULL handle
int status;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
ERROR("compiling shader '" + fileName + "'", false);
GLint msgLength = 0;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &msgLength);
std::vector<char> msg(msgLength);
glGetShaderInfoLog(shaderHandle, msgLength, &msgLength, &msg[0]);
printf("%s\n", &msg[0]);
glDeleteShader(shaderHandle);
return NULL_HANDLE;
}
return shaderHandle;
}
//-------------------------------------------------------------------------//
GLuint createShaderProgram(GLuint vertexShader, GLuint fragmentShader)
{
// Create and link the shader program
GLuint shaderProgram = glCreateProgram(); // create handle
if (!shaderProgram) {
ERROR("could not create the shader program", false);
return NULL_HANDLE;
}
glAttachShader(shaderProgram, vertexShader); // attach vertex shader
glAttachShader(shaderProgram, fragmentShader); // attach fragment shader
glLinkProgram(shaderProgram);
// check to see if the linking was successful
int linked;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked); // get link status
if (!linked) {
ERROR("could not link the shader program", false);
GLint msgLength = 0;
glGetShaderiv(shaderProgram, GL_INFO_LOG_LENGTH, &msgLength);
std::vector<char> msg(msgLength);
glGetShaderInfoLog(shaderProgram, msgLength, &msgLength, &msg[0]);
printf("%s\n", &msg[0]);
glDeleteProgram(shaderProgram);
return NULL_HANDLE;
}
return shaderProgram;
}
//-------------------------------------------------------------------------//
// GLM UTILITY STUFF
//-------------------------------------------------------------------------//
void printMat(const glm::mat4x4 &m)
{
printf("\n");
for (int r = 0; r < 4; r++) {
printf("[ ");
for (int c = 0; c < 4; c++) {
printf("%7.3f ", m[c][r]); // glm uses column major order
}
printf("]\n");
}
}
//-------------------------------------------------------------------------//
// FILE READING
//-------------------------------------------------------------------------//
vector<string> PATH;
void addToPath(const string &p)
{
PATH.push_back(p);
}
void removeFromPath(const string &p)
{
for (int i = (int)p.length() - 1; i >= 0; i--) {
if (PATH[i] == p) PATH.erase(PATH.begin() + i);
}
}
bool getFullFileName(const string &fileName, string &fullName)
{
for (int i = -1; i < (int)PATH.size(); i++) {
if (i < 0) fullName = fileName;
else fullName = PATH[i] + fileName;
FILE *f = fopen(fullName.c_str(), "rb");
if (f != NULL) {
fclose(f);
return true;
}
}
fullName = "";
return false;
}
FILE *openFileForReading(const string &fileName)
{
string fullName;
bool fileExists = getFullFileName(fileName, fullName);
if (fileExists) {
cout << "Opening file '" << fileName << "'" << endl;
return fopen(fullName.c_str(), "rb");
}
string msg = "Could not open file " + fileName;
ERROR(msg.c_str(), false);
return NULL;
}
//-------------------------------------------------------------------------//
bool getToken(FILE *f, string &token, const string &oneCharTokens)
{
token = "";
while (!feof(f)) {
int c = getc(f);
int tokenLength = (int)token.length();
int cIsSpace = isspace(c);
bool cIsOneCharToken = ((int)oneCharTokens.find((char)c) >= 0);
if (cIsSpace && tokenLength == 0) { // spaces before token, ignore
continue;
}
else if (cIsSpace) { // space after token, done
break;
}
else if (c == EOF) { // end of file, done
break;
}
else if ((tokenLength == 0) && cIsOneCharToken) { // oneCharToken, done
token += (char)c;
break;
}
else if (cIsOneCharToken) { // oneCharToken after another token, push back
ungetc(c, f);
break;
}
else if ((tokenLength == 0) && (c == '\"' || c == '\'')) { // quoted string, append til end quote found
char endQuote = c;
while (!feof(f)) {
int d = getc(f);
if (d == endQuote) return true;
token += (char)d;
}
break;
}
else if (c == '\"' || c == '\'') { // quote after token started, push back
ungetc(c, f);
break;
}
else {
token += (char)c;
}
}
//cout << token << endl;
return (token.length() > 0);
}
//-------------------------------------------------------------------------//
int getFloats(FILE *f, float *a, int num)
{
string token;
int count = 0;
while (getToken(f, token, "[],")) {
if (token == "]") {
break;
}
else if (isdigit(token[0]) || token[0] == '-') {
sscanf(token.c_str(), "%f", &a[count]);
count++;
if (count == num) break;
}
}
return count;
}
int getInts(FILE *f, int *a, int num)
{
string token;
int count = 0;
while (getToken(f, token, "[],")) {
if (token == "]") {
break;
}
else if (isdigit(token[0]) || token[0] == '-') {
sscanf(token.c_str(), "%d", &a[count]);
count++;
if (count == num) break;
}
}
return count;
}
//-------------------------------------------------------------------------//
bool loadFileAsString(const string &fileName, string &fileContents)
{
printf("loading file '%s'\n", fileName.c_str());
string fullName;
bool fileExists = getFullFileName(fileName, fullName);
if (!fileExists) return false;
ifstream fileStream(fullName.c_str());
if (fileStream.good()) {
stringstream stringStream;
stringStream << fileStream.rdbuf();
fileContents = stringStream.str();
fileStream.close();
return true;
}
fileStream.close();
return false;
}
//-------------------------------------------------------------------------//
void replaceIncludes(string &src, string &dest, const string &directive,
string &alreadyIncluded, bool onlyOnce)
{
int start = 0;
while (true) {
int includeIndex = (int)src.find("#include", start);
if (includeIndex < 0) {
dest += src.substr(start);
break;
}
if (includeIndex > 0 && !isspace(src[includeIndex - 1])) continue;
dest += src.substr(start, includeIndex - 1);
//
int quoteStart = (int)src.find("\"", start + 8);
int quoteEnd = (int)src.find("\"", quoteStart + 1);
start = quoteEnd + 1;
if (quoteStart >= quoteEnd) {
ERROR("could not replace includes");
break;
}
string includeFileName = src.substr(quoteStart + 1, (quoteEnd - quoteStart - 1));
if ((int)alreadyIncluded.find(includeFileName) < 0) {
if (onlyOnce) {
alreadyIncluded.append("|");
alreadyIncluded.append(includeFileName);
}
string subSource;
loadFileAsString(includeFileName, subSource);
replaceIncludes(subSource, dest, directive, alreadyIncluded, onlyOnce);
}
}
}
//-------------------------------------------------------------------------//
// RGBAImage
//-------------------------------------------------------------------------//
RGBAImage::~RGBAImage()
{
if (textureId != NULL_HANDLE) glDeleteTextures(1, &textureId);
if (samplerId != NULL_HANDLE) glDeleteSamplers(1, &samplerId);
}
bool RGBAImage::loadPNG(const string &fileName, bool doFlipY)
{
string fullName;
getFullFileName(fileName, fullName);
unsigned error = lodepng::decode(pixels, width, height, fullName.c_str());
if (error) {
ERROR(lodepng_error_text(error), false);
return false;
}
if (doFlipY) flipY(); // PNGs go top-to-bottom, OpenGL is bottom-to-top
name = fileName;
return true;
}
bool RGBAImage::writeToPNG(const string &fileName)
{
unsigned error = lodepng::encode(fileName.c_str(), pixels, width, height);
if (error) {
ERROR(lodepng_error_text(error), false);
return false;
}
return true;
}
void RGBAImage::flipY(void)
{
unsigned int *a, *b;
unsigned int temp;
for (int y = 0; y < (int)height / 2; y++)
{
a = &pixel(0, y);
b = &pixel(0, height - 1 - y);
for (int x = 0; x < (int)width; x++) {
temp = a[x];
a[x] = b[x];
b[x] = temp;
}
}
}
void RGBAImage::sendToOpenGL(GLuint magFilter, GLuint minFilter, bool createMipMap)
{
if (width <= 0 || height <= 0) return;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
if (createMipMap) glGenerateMipmap(GL_TEXTURE_2D);
glGenSamplers(1, &samplerId);
glBindSampler(textureId, samplerId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
}
//-------------------------------------------------------------------------//
// TRIANGLE MESH
//-------------------------------------------------------------------------//
bool TriMesh::readFromPly(const string &fileName, bool flipZ)
{
FILE *f = openFileForReading(fileName);
if (f == NULL) return false;
string token, t;
int numVertices = 0;
int numFaces = 0;
int numTriangles = 0;
vector<int> faceIndices;
// get num vertices
while (getToken(f, token, "")) {
//cout << token << endl;
if (token == "vertex") break;
}
getToken(f, token, "");
numVertices = atoi(token.c_str());
// get vertex attributes
while (getToken(f, token, "")) {
if (token == "property") {
getToken(f, t, ""); // token should be "float"
getToken(f, t, ""); // attribute name
attributes.push_back(t);
}
else if (token == "face") {
getToken(f, t, "");
numFaces = atoi(t.c_str());
break;
}
}
// read to end of header
while (getToken(f, token, "")) {
if (token == "end_header") break;
}
// get vertices
float val;
for (int i = 0; i < (int)(numVertices*attributes.size()); i++) {
//getToken(f, token, "");
//vertexData.push_back((float)atof(token.c_str()));
fscanf(f, "%f", &val);
vertexData.push_back(val);
}
// divide color values by 255, and flip normal directions if needed
// This deals with issues related to exporting from Blender to ply
// usin the y-axis as UP and the z-axis as FRONT.
for (int i = 0; i < (int)attributes.size(); i++) {
if (attributes[i] == "red" || attributes[i] == "green" || attributes[i] == "blue") {
for (int j = 0; j < numVertices; j++) {
vertexData[i + j*attributes.size()] /= 255.0f;
}
}
else if (flipZ && (attributes[i] == "z" || attributes[i] == "nz")) {
for (int j = 0; j < numVertices; j++) {
vertexData[i + j*attributes.size()] *= -1.0f;
}
}
}
// get faces
int idx;
for (int i = 0; i < numFaces; i++) {
faceIndices.clear();
getToken(f, token, "");
int numVerts = atoi(token.c_str());
for (int j = 0; j < numVerts; j++) { // get all vertices in face
//getToken(f, t, "");
//faceIndices.push_back(atoi(t.c_str()));
fscanf(f, "%d", &idx);
faceIndices.push_back(idx);
}
for (int j = 2; j < numVerts; j++) { // make triangle fan
indices.push_back(faceIndices[0]);
indices.push_back(faceIndices[j-1]);
indices.push_back(faceIndices[j]);
numTriangles++;
}
}
numIndices = (int)indices.size();
//printf("vertices:%d, triangles:%d, attributes:%d\n",
// vertexData.size()/attributes.size(),
// indices.size()/3,
// attributes.size());
fclose(f);
return true;
}
//-------------------------------------------------------------------------//
#define V_POSITION 0
#define V_NORMAL 1
#define V_ST 2
#define V_COLOR 3
int NUM_COMPONENTS[] = { 3, 3, 2, 3 };
bool TriMesh::sendToOpenGL(void)
{
// Create vertex array object. The vertex array object
// holds the structure of how the vertices are stored. VAOs
// also are bound for rendering.
//
glGenVertexArrays(1, &vao); // generate 1 array
glBindVertexArray(vao);
// Make and bind the vertex buffer object. The vbo
// holds the raw data that will be indexed by the vao.
//
GLuint vbo; // vertex buffer object
glGenBuffers(1, &vbo); // generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexData.size()*sizeof(float), &vertexData[0], GL_STATIC_DRAW);
// At this point, we have to tell the vertex array what kind
// of data it holds, and where it is located in the vertex buffer.
// The code here uses the first field of four possible data types
// (position, normal, textureCoordinate, color)
//
int stride = (int)attributes.size() * sizeof(float); // size of a vertex in bytes
for (int i = 0; i < (int)attributes.size(); i++) {
int bindIndex = -1;
//int numComponents = 0;
if (attributes[i] == "x") bindIndex = V_POSITION;
else if (attributes[i] == "nx") bindIndex = V_NORMAL;
else if (attributes[i] == "s") bindIndex = V_ST;
else if (attributes[i] == "red") bindIndex = V_COLOR;
if (bindIndex >= 0) {
//printf("bindIndex = %d\n", bindIndex);
glEnableVertexAttribArray(bindIndex);
glVertexAttribPointer(bindIndex, NUM_COMPONENTS[bindIndex],
GL_FLOAT, GL_FALSE, stride, (void*)(i * sizeof(float)));
}
}
// unbind the VBO and VAO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Generate the index buffer
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int),
&indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // unbind
return true;
}
//-------------------------------------------------------------------------//
void TriMesh::draw(void)
{
glBindVertexArray(vao); // bind the vertices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); // bind the indices
// draw the triangles. modes: GL_TRIANGLES, GL_LINES, GL_POINTS
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, (void*)0);
}
//-------------------------------------------------------------------------//
TriMeshInstance::TriMeshInstance(void)
{
triMesh = NULL;
shaderProgram = NULL_HANDLE;
diffuseColor = glm::vec4(1, 1, 1, 1);
T.scale = glm::vec3(1, 1, 1);
T.translation = glm::vec3(0, 0, 0);
}
//-------------------------------------------------------------------------//
void TriMeshInstance::draw(Camera &camera)
{
glUseProgram(shaderProgram);
// Inefficient. Looks up uniforms by string every time.
// Setting the uniforms should probably part of a Material
// class.
GLint loc = glGetUniformLocation(shaderProgram, "uDiffuseColor");
if (loc != -1) glUniform4fv(loc, 1, &diffuseColor[0]);
loc = glGetUniformLocation(shaderProgram, "uDiffuseTex");
if (loc != -1) glBindSampler(loc, diffuseTexture.samplerId);
else ERROR("Could not bind texture", false);
//printVec(color);
T.refreshTransform();
//printMat(transform);
loc = glGetUniformLocation(shaderProgram, "uObjectWorldM");
if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.transform));
//else ERROR("Could not load uniform uObjectWorldM", false);
loc = glGetUniformLocation(shaderProgram, "uObjectWorldInverseM");
if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(T.invTransform));
//else ERROR("Could not load uniform uObjectWorldInverseM", false);
glm::mat4x4 objectWorldViewPerspect = camera.worldViewProject * T.transform;
loc = glGetUniformLocation(shaderProgram, "uObjectPerpsectM");
if (loc != -1) glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(objectWorldViewPerspect));
//else ERROR("Could not load uniform uObjectPerpsectM", false);
triMesh->draw();
}
//-------------------------------------------------------------------------//