-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleRects.cpp
More file actions
225 lines (166 loc) · 5.89 KB
/
ExampleRects.cpp
File metadata and controls
225 lines (166 loc) · 5.89 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
/*
* ExampleRects.cpp
*
* Created on: Apr 10, 2020
* Author: ans
*/
#include "ExampleRects.h"
ExampleRects::ExampleRects()
: pixelTestW(0),
pixelSize(2),
randomGenerator(Rand::RAND_ALGO_LEHMER32),
renderBorders(false),
testPixels(false) {}
ExampleRects::~ExampleRects() {}
// run the application
int ExampleRects::run(int argc, char * argv[]) {
UNUSED(argc);
UNUSED(argv);
const std::string name("rects");
constexpr int width = 800;
constexpr int height = 600;
this->setClearBuffer(true);
this->setPixelSize(this->pixelSize);
this->createMainWindow(width, height, name);
this->Engine::run();
return EXIT_SUCCESS;
}
// create resources
void ExampleRects::onCreate() {}
// update frame
void ExampleRects::onUpdate(double elapsedTime) {
UNUSED(elapsedTime);
// render rectangles
const int w = this->getWindowWidth();
const int h = this->getWindowHeight();
if(this->testPixels) // draw backwards if pixel testing is enabled
for(auto it = this->rects.rbegin(); it != this->rects.rend(); ++it)
this->render(w, h, *it);
else
for(const auto& rect : this->rects)
this->render(w, h, rect);
// show number of rectangles and whether pixel testing is enabled
std::string debugStr("n=");
debugStr += std::to_string(this->rects.size());
if(this->testPixels)
debugStr += ", testing pixels";
this->setDebugText(debugStr);
// handle ENTER key for adding a rectangle
if(this->isKeyPressed(GLFW_KEY_ENTER))
this->add();
if(this->isKeyRepeated(GLFW_KEY_ENTER))
this->add();
// handle ESCAPE key for removing all rectangles
if(this->isKeyPressed(GLFW_KEY_ESCAPE))
this->rects.clear();
if(this->isKeyRepeated(GLFW_KEY_ESCAPE))
this->rects.clear();
// handle SPACE key for toggling drawing the borders of the rectangles
if(this->isKeyPressed(GLFW_KEY_SPACE))
this->renderBorders = !(this->renderBorders);
// handle TAB key for toggling pixel testing
if(this->isKeyPressed(GLFW_KEY_TAB)) {
this->testPixels = !(this->testPixels);
if(this->testPixels) {
PixelTest pixelTest;
pixelTest.debugging = true;
pixelTest.init = std::bind(&ExampleRects::pixelTestInit, this, std::placeholders::_1, std::placeholders::_2);
pixelTest.frame = std::bind(&ExampleRects::pixelTestFrame, this);
pixelTest.test = std::bind(&ExampleRects::pixelTestTest, this, std::placeholders::_1, std::placeholders::_2);
this->setPixelTest(pixelTest);
}
else
this->disablePixelTest();
}
// handle UP/DOWN arrow keys for changing the 'pixel' size
const unsigned short oldPixelSize = this->pixelSize;
if(this->isKeyPressed(GLFW_KEY_UP) && this->pixelSize < 100)
++(this->pixelSize);
if(this->isKeyRepeated(GLFW_KEY_UP) && this->pixelSize < 100)
++(this->pixelSize);
if(this->isKeyPressed(GLFW_KEY_DOWN) && this->pixelSize > 1)
--(this->pixelSize);
if(this->isKeyRepeated(GLFW_KEY_DOWN) && this->pixelSize > 1)
--(this->pixelSize);
if(this->pixelSize != oldPixelSize)
this->setPixelSize(this->pixelSize);
// handle F10-F12 keys for changing the rendering mode
const auto currentRenderingMode = this->getRenderingMode();
auto newRenderingMode = currentRenderingMode;
if(this->isKeyPressed(GLFW_KEY_F10))
newRenderingMode = MainWindow::RENDERING_MODE_PBO;
if(this->isKeyPressed(GLFW_KEY_F11))
newRenderingMode = MainWindow::RENDERING_MODE_POINTS;
if(this->isKeyPressed(GLFW_KEY_F12))
newRenderingMode = MainWindow::RENDERING_MODE_TEXTURE;
if(newRenderingMode != currentRenderingMode)
this->setRenderingMode(newRenderingMode);
}
// add one rectangle
void ExampleRects::add() {
constexpr double minSize = 0.001f;
Rect newRect;
while(newRect.w() < minSize || newRect.h() < minSize) {
newRect.x1 = this->randomGenerator.generateReal();
newRect.y1 = this->randomGenerator.generateReal();
newRect.x2 = this->randomGenerator.generateReal();
newRect.y2 = this->randomGenerator.generateReal();
}
newRect.c.r = this->randomGenerator.generateByte();
newRect.c.g = this->randomGenerator.generateByte();
newRect.c.b = this->randomGenerator.generateByte();
if(newRect.x1 > newRect.x2)
std::swap(newRect.x1, newRect.x2);
if(newRect.y1 > newRect.y2)
std::swap(newRect.y1, newRect.y2);
Geometry::addAndSplit(this->rects, newRect, minSize);
}
// render one rectangle
void ExampleRects::render(int w, int h, const Rect& rect) {
const int absX1 = static_cast<int>(std::lround(rect.x1 * w));
const int absY1 = static_cast<int>(std::lround(rect.y1 * h));
const int absX2 = static_cast<int>(std::lround(rect.x2 * w));
const int absY2 = static_cast<int>(std::lround(rect.y2 * h));
if(this->renderBorders) {
constexpr unsigned char borderR = 0;
constexpr unsigned char borderG = 0;
constexpr unsigned char borderB = 0;
for(int x = absX1; x < absX2; ++x) {
if(absY1 < h)
this->draw(x, absY1, borderR, borderG, borderB);
if(absY2 > 0)
this->draw(x, absY2 - 1, borderR, borderG, borderB);
}
for(int y = absY1; y < absY2; ++y) {
if(absX1 < w)
this->draw(absX1, y, borderR, borderG, borderB);
if(absX2 > 0)
this->draw(absX2 - 1, y, borderR, borderG, borderB);
}
if(absX2 > 0 && absY2 > 0)
this->fill(absX1 + 1, absY1 + 1, absX2 - 1, absY2 - 1, rect.c.r, rect.c.g, rect.c.b);
else if(absX2 > 0)
this->fill(absX1 + 1, absY1 + 1, absX2 - 1, 0, rect.c.r, rect.c.g, rect.c.b);
else if(absY2 > 0)
this->fill(absX1 + 1, absY1 + 1, 0, absY2 - 1, rect.c.r, rect.c.g, rect.c.b);
}
else
this->fill(absX1, absY1, absX2, absY2, rect.c.r, rect.c.g, rect.c.b);
}
// pixel test initialization
void ExampleRects::pixelTestInit(unsigned int w, unsigned int h) {
this->pixelTest.assign(w * h, false);
this->pixelTestW = w;
}
// pixel test frame
void ExampleRects::pixelTestFrame() {
this->pixelTest.assign(this->pixelTest.size(), false);
}
// pixel test
bool ExampleRects::pixelTestTest(unsigned int x, unsigned int y) {
const auto i = y * this->pixelTestW + x;
if(this->pixelTest.at(i))
return false;
this->pixelTest.at(i) = true;
return true;
}