-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
741 lines (563 loc) · 16.9 KB
/
MainWindow.cpp
File metadata and controls
741 lines (563 loc) · 16.9 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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* MainWindow.cpp
*
* Created on: Apr 10, 2020
* Author: ans
*/
#include "MainWindow.h"
// constructor: set default values
MainWindow::MainWindow()
: glfwInitialized(false),
windowPointer(nullptr),
renderingMode(RENDERING_MODE_PBO),
pboId(0),
textureId(0),
width(0),
height(0),
bytes(4),
pixelWidth(0),
pixelHeight(0),
clearBuffer(false),
pixelSize(1),
halfPixelSize(0),
rendering(false),
lastTime(0.),
elapsedTime(0.),
fps(0.),
debugChanged(false) {
for(int n = 0; n < GLFW_KEY_LAST; ++n) {
keys[n].pressed = false;
keys[n].held = false;
keys[n].released = false;
keys[n].repeated = false;
}
glfwSetErrorCallback(MainWindow::callbackError);
}
// destructor
MainWindow::~MainWindow() {
// destroy pixel buffer if necessary
this->destroyRenderingTarget();
// destroy window if necessary
if(this->windowPointer) {
glfwDestroyWindow(this->windowPointer);
this->windowPointer = nullptr;
}
// terminate GLFW if necessary
if(this->glfwInitialized) {
glfwTerminate();
this->glfwInitialized = false;
}
}
// initialize window
void MainWindow::init(unsigned int w, unsigned int h, const std::string& title) {
// initialize GLFW
if(!(this->glfwInitialized) && !glfwInit())
throw std::runtime_error("glfwInit failed");
this->glfwInitialized = true;
// create window
this->windowPointer = glfwCreateWindow(w, h, title.c_str(), nullptr, nullptr);
if(!(this->windowPointer))
throw std::runtime_error("glfwCreateWindow failed");
this->title = title;
// set additional window options
glfwSetWindowUserPointer(this->windowPointer, this);
glfwSetFramebufferSizeCallback(this->windowPointer, MainWindow::callbackFramebuffer);
glfwSetKeyCallback(this->windowPointer, MainWindow::callbackKey);
// get size of framebuffer
glfwGetFramebufferSize(this->windowPointer, &(this->width), &(this->height));
// make OpenGL context current and disable vertical synchronization
glfwMakeContextCurrent(this->windowPointer);
glfwSwapInterval(0);
// set additional OpenGL options
glDisable(GL_DITHER);
glDisable(GL_MULTISAMPLE);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0., 0., 0., 1.);
// initialize rendering target and set projection
this->initRenderingTarget();
this->setProjection();
// save starting time
this->lastTime = glfwGetTime();
}
// tick in window loop to process window events, return whether window has been closed
bool MainWindow::update() {
// check whether window has been closed
if(glfwWindowShouldClose(this->windowPointer))
return false;
// check whether debugging string has been changed
if(this->debugChanged) {
if(this->debug.empty())
glfwSetWindowTitle(this->windowPointer, this->title.c_str());
else {
std::string fullTitle(this->title);
fullTitle += " [" + this->debug + "]";
glfwSetWindowTitle(this->windowPointer, fullTitle.c_str());
}
this->debugChanged = false;
}
// poll for window events
glfwPollEvents();
// begin rendering to pixel buffer
this->beginRendering();
// update frame
this->onUpdate(this->getElapsedTime());
// end rendering to pixel buffer
this->endRendering();
// clear keys
this->clearKeys();
// flush the buffer
glfwSwapBuffers(this->windowPointer);
// calculate the framerate
double currentTime = glfwGetTime();
this->elapsedTime = currentTime - this->lastTime;
this->lastTime = currentTime;
this->fps = 1. / this->elapsedTime;
return true;
}
// get the width of the framebuffer in pixels
int MainWindow::getWidth() const {
return this->pixelWidth;
}
// get the height of the framebuffer in pixels
int MainWindow::getHeight() const {
return this->pixelHeight;
}
// get time since start in seconds
double MainWindow::getTime() const {
return glfwGetTime();
}
// get elapsed time since last frame in seconds
double MainWindow::getElapsedTime() const {
return this->elapsedTime;
}
// get framerate in frames per second
double MainWindow::getFPS() const {
return this->fps;
}
// get the current rendering mode
MainWindow::RenderingMode MainWindow::getRenderingMode() const {
return this->renderingMode;
}
// check whether the specified key has been pressed this frame
bool MainWindow::isKeyPressed(unsigned int code) const {
if(code <= 0 || code > GLFW_KEY_LAST)
throw std::runtime_error("isKeyPressed: illegal key code");
return this->keys[code - 1].pressed;
}
// check whether the specified key has been held
bool MainWindow::isKeyHeld(unsigned int code) const {
if(code <= 0 || code > GLFW_KEY_LAST)
throw std::runtime_error("isKeyHeld: illegal key code");
return this->keys[code - 1].held;
}
// check whether the specified key has been released this frame
bool MainWindow::isKeyReleased(unsigned int code) const {
if(code <= 0 || code > GLFW_KEY_LAST)
throw std::runtime_error("isKeyReleased: illegal key code");
return this->keys[code - 1].released;
}
// check whether the specified key has been held long enough to be repeated
bool MainWindow::isKeyRepeated(unsigned int code) const {
if(code <= 0 || code > GLFW_KEY_LAST)
throw std::runtime_error("isKeyRepeated: illegal key code");
return this->keys[code - 1].repeated;
}
// set the rendering mode
void MainWindow::setRenderingMode(RenderingMode mode) {
// end rendering if necessary
if(this->rendering)
this->endRendering();
// destroy old rendering target
this->destroyRenderingTarget();
this->renderingMode = mode;
// initialize new rendering target
this->initRenderingTarget();
// start rendering if necessary
if(this->rendering)
this->beginRendering();
}
// set whether to clear the rendering target to black every frame
void MainWindow::setClearBuffer(bool clear) {
this->clearBuffer = clear;
}
// set the actual size of one pixel
void MainWindow::setPixelSize(unsigned short size) {
this->pixelSize = size;
this->halfPixelSize = size / 2;
this->setProjection();
if(this->renderingMode == RENDERING_MODE_POINTS && this->rendering) {
this->endRendering();
glPointSize(this->pixelSize);
this->beginRendering();
}
}
// set a test for pixels before drawing them
void MainWindow::setPixelTest(const PixelTest& test) {
this->pixelTest = test;
if(this->pixelTest)
this->pixelTest.init(this->pixelWidth, this->pixelHeight);
}
// write one pixel into the buffer / draw it onto the screen
void MainWindow::putPixel(
unsigned int x,
unsigned int y,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a,
bool test
) {
const auto offsetX = x * this->pixelSize;
const auto offsetY = y * this->pixelSize;
// perform pixel test if necessary
if(test && this->pixelTest && !(this->pixelTest.test(x, y))) {
if(this->pixelTest.debugging)
// draw red pixel for debugging instead
putPixel(x, y, 255, 0, 0, 255, false);
return;
}
if(this->renderingMode == RENDERING_MODE_POINTS) {
glColor4ub(r, g, b, a);
glVertex2i(x * this->pixelSize + this->halfPixelSize, y * this->pixelSize + this->halfPixelSize);
}
else {
int limitX = this->pixelSize;
int limitY = this->pixelSize;
if(static_cast<int>(offsetX + limitX) > this->width)
limitX = this->width - offsetX;
if(static_cast<int>(offsetY + limitY) > this->height)
limitY = this->height - offsetY ;
for(unsigned short relX = 0; relX < limitX; ++relX)
for(unsigned short relY = 0; relY < limitY; ++relY) {
const auto putX = offsetX + relX;
const auto putY = offsetY + relY;
this->pixels.set(
putX,
putY,
r,
g,
b,
a
);
}
}
}
// set callback function for updating the content
void MainWindow::setOnUpdate(UpdateFunction callBack) {
this->onUpdate = callBack;
}
// set callback function for resize
void MainWindow::setOnResize(ResizeFunction callBack) {
this->onResize = callBack;
}
// set the debug text to be shown in the window title bar (after "pixels")
void MainWindow::setDebugText(const std::string& text) {
this->debug = text;
this->debugChanged = true;
}
// set projection (use window coordinates)
void MainWindow::setProjection() {
// calculate pixels
if(this->width % this->pixelSize > 0)
this->pixelWidth = this->width / this->pixelSize + 1;
else
this->pixelWidth = this->width / this->pixelSize;
if(this->height % this->pixelSize > 0)
this->pixelHeight = this->height / this->pixelSize + 1;
else
this->pixelHeight = this->height / this->pixelSize;
// set viewport
glViewport(0, 0, this->width, this->height);
// set projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, this->width, 0, this->height, 1, -1);
// set model
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// reset keys (only pressed and released)
void MainWindow::clearKeys() {
for(int n = 0; n < GLFW_KEY_LAST; ++n) {
if(this->keys[n].pressed)
this->keys[n].pressed = false;
if(this->keys[n].released)
this->keys[n].released = false;
if(this->keys[n].repeated)
this->keys[n].repeated = false;
}
}
// initialize rendering target
void MainWindow::initRenderingTarget() {
// destroy old rendering target if necessary
this->destroyRenderingTarget();
// initialize or reset pixel test
if(this->pixelTest)
this->pixelTest.init(this->pixelWidth, this->pixelHeight);
switch(this->renderingMode) {
case RENDERING_MODE_PBO:
// generate pixel buffer object
glGenBuffers(1, &(this->pboId));
// check pixel buffer object
if(this->pboId <= 0) {
const auto errorCode = glGetError();
switch(errorCode) {
case GL_NO_ERROR:
throw std::runtime_error(
"Could not create pixel buffer"
);
default:
throw std::runtime_error(
"Could not create pixel buffer: "
+ MainWindow::glErrorString(errorCode)
);
}
}
// reserve memory for pixel buffer object
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->pboId);
glBufferData(GL_PIXEL_UNPACK_BUFFER, this->width * this->height * this->bytes, nullptr, GL_STREAM_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
// enable texturing
glEnable(GL_TEXTURE_2D);
// generate texture
glGenTextures(1, &(this->textureId));
// check texture
if(this->textureId <= 0) {
const auto errorCode = glGetError();
switch(errorCode) {
case GL_NO_ERROR:
throw std::runtime_error(
"Could not create texture"
);
default:
throw std::runtime_error(
"Could not create texture: "
+ MainWindow::glErrorString(errorCode)
);
}
}
// set texture properties and reserve memory for texture
glBindTexture(GL_TEXTURE_2D, this->textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, 0);
// clear pixel buffer object
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->pboId);
this->pixels.map(
this->width,
this->height,
this->bytes,
static_cast<unsigned char *>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY))
);
if(!(this->pixels))
throw std::runtime_error("Could not map memory of pixel buffer object");
this->pixels.fill(0, 0, 0, 255);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
this->pixels.unmap();
break;
case RENDERING_MODE_POINTS:
// set pixel size
glPointSize(this->pixelSize);
// set blending (not supported yet)
//glEnable(GL_BLEND);
// clear both frame buffers (front and back)
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(this->windowPointer);
glClear(GL_COLOR_BUFFER_BIT);
break;
case RENDERING_MODE_TEXTURE:
// enable texturing
glEnable(GL_TEXTURE_2D);
// generate and bind texture
glGenTextures(1, &(this->textureId));
glBindTexture(GL_TEXTURE_2D, this->textureId);
// set texture properties
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// allocate memory for rendering data
this->pixels.allocate(this->width, this->height, this->bytes);
break;
}
}
// start rendering a single frame
void MainWindow::beginRendering() {
// notify pixel test of coming frame
if(this->pixelTest)
this->pixelTest.frame();
this->rendering = true;
switch(this->renderingMode) {
case RENDERING_MODE_PBO:
// bind pixel buffer object
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->pboId);
// map memory of pixel buffer object
this->pixels.map(
this->width,
this->height,
this->bytes,
static_cast<unsigned char *>(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY))
);
if(!(this->pixels))
throw std::runtime_error("Could not map memory of pixel buffer object");
// clear the pixel buffer object if necessary
if(this->clearBuffer)
this->pixels.fill(0, 0, 0, 255);
break;
case RENDERING_MODE_POINTS:
// clear the OpenGL framebuffer if necessary
if(this->clearBuffer)
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
break;
case RENDERING_MODE_TEXTURE:
// clear the texture if necessary
if(this->clearBuffer)
this->pixels.fill(0, 0, 0, 255);
break;
}
}
// render textured quad
void MainWindow::renderQuad() {
glBegin(GL_QUADS);
glTexCoord2f(0., 0.);
glVertex2i(0, 0);
glTexCoord2f(0., 1.);
glVertex2i(0, this->height);
glTexCoord2f(1., 1.);
glVertex2i(this->width, this->height);
glTexCoord2f(1., 0.);
glVertex2i(this->width, 0);
glEnd();
}
// finish up rendering a single frame
void MainWindow::endRendering() {
switch(this->renderingMode) {
case RENDERING_MODE_PBO:
// unmap memory of pixel buffer object
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
this->pixels.unmap();
//glDrawPixels(this->width, this->height, GL_RGBA, GL_UNSIGNED_BYTE, 0);
// bind and update texture
glBindTexture (GL_TEXTURE_2D, this->textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->pixels.get());
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
// render textured quad
this->renderQuad();
// unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
break;
case RENDERING_MODE_POINTS:
glEnd();
break;
case RENDERING_MODE_TEXTURE:
// update texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->pixels.get());
// render textured quad
this->renderQuad();
break;
}
this->rendering = false;
}
// destroy rendering target
void MainWindow::destroyRenderingTarget() {
switch(this->renderingMode) {
case RENDERING_MODE_PBO:
if(this->textureId > 0) {
glDeleteTextures(1, &(this->textureId));
this->textureId = 0;
}
if(this->pboId > 0) {
glDeleteBuffers(1, &(this->pboId));
this->pboId = 0;
}
break;
case RENDERING_MODE_POINTS:
glDisable(GL_BLEND);
break;
case RENDERING_MODE_TEXTURE:
glBindTexture(GL_TEXTURE_2D, 0);
if(this->textureId > 0) {
glDeleteTextures(1, &(this->textureId));
this->textureId = 0;
}
this->pixels.deallocate();
break;
}
}
// in-class callback for framebuffer creation and changes
void MainWindow::onFramebuffer(int w, int h) {
if(this->width == w && this->height == h)
return;
this->width = w;
this->height = h;
// initialize rendering target and set projection
this->initRenderingTarget();
this->setProjection();
if(this->onResize)
this->onResize(w, h);
}
// in-class callback for key events
void MainWindow::onKey(int key, int action) {
if(key > 0 && key <= GLFW_KEY_LAST) {
switch(action) {
case GLFW_PRESS:
this->keys[key - 1].pressed = true;
this->keys[key - 1].held = true;
break;
case GLFW_RELEASE:
this->keys[key - 1].held = false;
this->keys[key - 1].released = true;
break;
case GLFW_REPEAT:
this->keys[key - 1].held = true;
this->keys[key - 1].repeated = true;
break;
}
}
}
// callback for GLFW errors
void MainWindow::callbackError(int error, const char * description) {
throw std::runtime_error(
"GLFW error #"
+ std::to_string(error)
+ ": "
+ description
);
}
// callback for framebuffer changes (use GLFW user pointer to jump into class)
void MainWindow::callbackFramebuffer(GLFWwindow * window, int width, int height) {
static_cast<MainWindow *>(glfwGetWindowUserPointer(window))->onFramebuffer(width, height);
}
// callback for key event (use GLFW user pointer to jump into class)
void MainWindow::callbackKey(GLFWwindow * window, int key, int scancode, int action, int mods) {
UNUSED(scancode);
UNUSED(mods);
static_cast<MainWindow *>(glfwGetWindowUserPointer(window))->onKey(key, action);
}
// get the last OpenGL error as a string
std::string MainWindow::glErrorString(GLenum errorCode) {
switch(errorCode) {
case GL_NO_ERROR:
return "GL_NO_ERROR";
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
case GL_STACK_OVERFLOW:
return "GL_STACK_OVERFLOW";
case GL_STACK_UNDERFLOW:
return "GL_STACK_UNDERFLOW";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
case GL_TABLE_TOO_LARGE:
return "GL_TABLE_TOO_LARGE";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
default:
return "UNKNOWN ERROR";
}
}