/
Renderer.cpp
436 lines (377 loc) · 13 KB
/
Renderer.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
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
#include "Renderer.h"
#include "lib/gl_structs.h"
#include "DbAlbumCollection.h"
#include "Engine.h"
#include "Image.h"
#include "TextureCache.h"
#include "config.h"
#include "utils.h"
#include "world_state.h"
#define SELECTION_CENTER INT_MAX // Selection is an unsigned int, so this is center
#define SELECTION_COVERS 1
#define SELECTION_MIRROR 2
Renderer::Renderer(Engine& engine)
: textDisplay(*this), bitmapFont(*this), engine(engine),
spinnerTexture(loadSpinner()) {
glfwSwapInterval(0);
vSyncEnabled = false;
}
void Renderer::ensureVSync(bool enableVSync) {
if (vSyncEnabled != enableVSync) {
vSyncEnabled = enableVSync;
glfwSwapInterval(enableVSync ? 1 : 0);
}
}
void Renderer::resizeGlScene(int width, int height) {
if (height == 0)
height = 1;
winWidth = width;
winHeight = height;
glViewport(0, 0, width, height); // Reset The Current Viewport
setProjectionMatrix();
textDisplay.clearCache();
}
void Renderer::getFrustrumSize(double& right, double& top, double& zNear, double& zFar) {
zNear = 0.1;
zFar = 500;
// Calculate The Aspect Ratio Of The Window
static const double fov = 45;
static const double squareLength = tan(deg2rad(fov) / 2) * zNear;
fovAspectBehaviour weight = engine.coverPos.getAspectBehaviour();
double aspect = double(winHeight) / winWidth;
right = squareLength / pow(aspect, double(weight.y));
top = squareLength * pow(aspect, double(weight.x));
}
void Renderer::setProjectionMatrix(bool pickMatrix, int x, int y) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (pickMatrix) {
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, static_cast<GLint*>(viewport));
gluPickMatrix(
GLdouble(x), GLdouble(viewport[3] - y), 1.0, 1.0, static_cast<GLint*>(viewport));
}
double right, top, zNear, zFar;
getFrustrumSize(right, top, zNear, zFar);
glFrustum(-right, +right, -top, +top, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
}
void Renderer::glPushOrthoMatrix() {
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, winWidth, 0, winHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
void Renderer::glPopOrthoMatrix() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
}
std::optional<AlbumInfo> Renderer::albumAtPoint(int x, int y) {
GLuint buf[256];
glSelectBuffer(256, static_cast<GLuint*>(buf));
glRenderMode(GL_SELECT);
setProjectionMatrix(true, x, y);
glInitNames();
drawScene(true);
GLint hits = glRenderMode(GL_RENDER);
setProjectionMatrix();
auto* p = static_cast<GLuint*>(buf);
GLuint minZ = INFINITE;
GLuint selectedName = 0;
for (int i = 0; i < hits; i++) {
GLuint names = *p;
p++;
GLuint z = *p;
p++;
p++;
if ((names > 1) && (z < minZ) && (*p == SELECTION_COVERS)) {
minZ = z;
selectedName = *(p + 1);
}
p += names;
}
if (minZ == INFINITE)
return std::nullopt;
int offset = (selectedName - SELECTION_CENTER);
auto& center = engine.worldState.getCenteredPos();
if (auto iter = engine.db.iterFromPos(center)) {
return engine.db.getAlbumInfo(engine.db.moveIterBy(iter.value(), offset));
} else {
return std::nullopt;
}
}
void Renderer::drawMirrorPass() {
glVectord mirrorNormal = engine.coverPos.getMirrorNormal();
glVectord mirrorCenter = engine.coverPos.getMirrorCenter();
double mirrorDist; // distance from origin
mirrorDist = mirrorCenter * mirrorNormal;
glVectord mirrorPos = mirrorDist * mirrorNormal;
glVectord scaleAxis(1, 0, 0);
glVectord rotAxis = scaleAxis.cross(mirrorNormal);
double rotAngle = rad2deg(scaleAxis.intersectAng(mirrorNormal));
rotAngle = -2 * rotAngle;
glFogfv(GL_FOG_COLOR, std::array<GLfloat, 4>{GetRValue(cfgPanelBg) / 255.0f,
GetGValue(cfgPanelBg) / 255.0f,
GetBValue(cfgPanelBg) / 255.0f, 1.0f}
.data());
glEnable(GL_FOG);
glPushMatrix();
glTranslated(2 * mirrorPos.x, 2 * mirrorPos.y, 2 * mirrorPos.z);
glScalef(-1.0f, 1.0f, 1.0f);
glRotated(rotAngle, rotAxis.x, rotAxis.y, rotAxis.z);
drawCovers();
glPopMatrix();
glDisable(GL_FOG);
}
void Renderer::drawMirrorOverlay() {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor4f(GetRValue(cfgPanelBg) / 255.0f, GetGValue(cfgPanelBg) / 255.0f,
GetBValue(cfgPanelBg) / 255.0f, 0.60f);
glShadeModel(GL_FLAT);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glVertex3i(-1, -1, -1);
glVertex3i(1, -1, -1);
glVertex3i(1, 1, -1);
glVertex3i(-1, 1, -1);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
}
pfc::array_t<double> Renderer::getMirrorClipPlane() {
glVectord mirrorNormal = engine.coverPos.getMirrorNormal();
glVectord mirrorCenter = engine.coverPos.getMirrorCenter();
glVectord eye2mCent = (mirrorCenter - engine.coverPos.getCameraPos());
// Angle at which the mirror normal stands to the eyePos
double mirrorNormalAngle = rad2deg(eye2mCent.intersectAng(mirrorNormal));
pfc::array_t<double> clipEq;
clipEq.set_size(4);
if (mirrorNormalAngle > 90) {
clipEq[0] = -mirrorNormal.x;
clipEq[1] = -mirrorNormal.y;
clipEq[2] = -mirrorNormal.z;
clipEq[3] = mirrorNormal * mirrorCenter;
} else {
clipEq[0] = mirrorNormal.x;
clipEq[1] = mirrorNormal.y;
clipEq[2] = mirrorNormal.z;
clipEq[3] = -(mirrorNormal * mirrorCenter);
}
return clipEq;
}
void Renderer::drawScene(bool selectionPass) {
glLoadIdentity();
auto cameraPos = engine.coverPos.getCameraPos();
auto lookAt = engine.coverPos.getLookAt();
auto up = engine.coverPos.getUpVector();
gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, lookAt.x, lookAt.y, lookAt.z, up.x,
up.y, up.z);
pfc::array_t<double> clipEq;
if (engine.coverPos.isMirrorPlaneEnabled()) {
clipEq = getMirrorClipPlane();
if (!selectionPass) {
glClipPlane(GL_CLIP_PLANE0, clipEq.get_ptr());
glEnable(GL_CLIP_PLANE0);
glPushName(SELECTION_MIRROR);
drawMirrorPass();
glPopName();
glDisable(GL_CLIP_PLANE0);
drawMirrorOverlay();
}
// invert the clip equation
for (int i = 0; i < 4; i++) {
clipEq[i] *= -1;
}
glClipPlane(GL_CLIP_PLANE0, clipEq.get_ptr());
glEnable(GL_CLIP_PLANE0);
}
glPushName(SELECTION_COVERS);
drawCovers(true);
glPopName();
if (engine.coverPos.isMirrorPlaneEnabled()) {
glDisable(GL_CLIP_PLANE0);
}
}
void Renderer::drawGui() {
if (cfgShowAlbumTitle || engine.db.initializing()) {
std::string albumTitle;
if (engine.db.initializing()) {
albumTitle = "Generating Cover Display ...";
} else if (engine.db.empty()) {
albumTitle = "No Covers to Display";
} else {
DBIter iter = engine.db.iterFromPos(engine.worldState.getTarget()).value();
albumTitle = engine.db.getAlbumInfo(iter).title;
}
textDisplay.displayText(
albumTitle, int(winWidth * cfgTitlePosH), int(winHeight * (1 - cfgTitlePosV)));
}
if (cfgShowFps) {
double avgDur, maxDur, avgCPU, maxCPU;
engine.fpsCounter.getFPS(avgDur, maxDur, avgCPU, maxCPU);
std::ostringstream dispStringA;
std::ostringstream dispStringB;
dispStringA.flags(std::ios_base::fixed | std::ios_base::right);
dispStringB.flags(std::ios_base::fixed | std::ios_base::right);
dispStringA.precision(1);
dispStringB.precision(1);
dispStringA << "FPS: " << std::setw(4) << 1 / avgDur;
dispStringB << "max ms/f: " << std::setw(5) << (1000 * maxDur);
dispStringA << " cpu: " << std::setw(5) << avgCPU * 1000;
dispStringB << " max: " << std::setw(5) << maxCPU * 1000;
bitmapFont.displayText(dispStringA.str().c_str(), 15, winHeight - 20);
bitmapFont.displayText(dispStringB.str().c_str(), 15, winHeight - 35);
}
if (engine.reloadWorker)
drawSpinner();
}
void Renderer::drawSpinner() {
glPushOrthoMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
COLORREF c = cfgTitleColor.get_value();
glColor3f(GetRValue(c) / 255.0f, GetGValue(c) / 255.0f, GetBValue(c) / 255.0f);
spinnerTexture.bind();
int x = winWidth - 30;
int y = winHeight - 30;
double rot = -time() * 10;
double r = -sqrtf(2) * 16;
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 1.0f); // top left
glVertex3d(x + r * cos(rot + 0 * M_PI / 2), y + r * sin(rot + 0 * M_PI / 2), 0);
glTexCoord2f(1.0f, 1.0f); // top right
glVertex3d(x + r * cos(rot + 1 * M_PI / 2), y + r * sin(rot + 1 * M_PI / 2), 0);
glTexCoord2f(1.0f, 0.0f); // bottom right
glVertex3d(x + r * cos(rot + 2 * M_PI / 2), y + r * sin(rot + 2 * M_PI / 2), 0);
glTexCoord2f(0.0f, 0.0f); // bottom left
glVertex3d(x + r * cos(rot + 3 * M_PI / 2), y + r * sin(rot + 3 * M_PI / 2), 0);
}
glEnd();
glDisable(GL_BLEND);
glPopOrthoMatrix();
}
void Renderer::drawBg() {
glClearColor(GetRValue(cfgPanelBg) / 255.0f, GetGValue(cfgPanelBg) / 255.0f,
GetBValue(cfgPanelBg) / 255.0f, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Renderer::drawFrame() {
TRACK_CALL_TEXT("Renderer::drawFrame");
wasMissingTextures = false;
drawBg();
drawScene(false);
drawGui();
}
void Renderer::drawCovers(bool showTarget) {
if (cfgHighlightWidth == 0)
showTarget = false;
if (engine.db.empty() && !engine.db.initializing())
return;
struct Cover {
const GLImage* tex;
float offset;
int index;
bool isTarget;
};
std::vector<Cover> covers;
if (engine.db.initializing()) {
auto tex = &engine.texCache.getLoadingTexture();
for (int i = engine.coverPos.getFirstCover(); i <= engine.coverPos.getLastCover();
++i) {
covers.push_back(Cover{tex, float(i), i, i == 0});
}
} else {
float centerOffset = engine.worldState.getCenteredOffset();
// We can assume that iterFromPos succeeds, because we already checked for empty db
DBIter targetCover = engine.db.iterFromPos(engine.worldState.getTarget()).value();
DBIter centerCover =
engine.db.iterFromPos(engine.worldState.getCenteredPos()).value();
DBIter firstCover =
engine.db.moveIterBy(centerCover, engine.coverPos.getFirstCover() + 1);
DBIter lastCover = engine.db.moveIterBy(centerCover, engine.coverPos.getLastCover());
lastCover++; // moveIterBy never returns the end() element
int offset = engine.db.difference(firstCover, centerCover);
for (DBIter p = firstCover; p != lastCover; ++p, ++offset) {
float co = -centerOffset + offset;
auto tex = engine.texCache.getAlbumTexture(p->key);
if (tex == nullptr) {
wasMissingTextures = true;
tex = &engine.texCache.getLoadingTexture();
}
covers.push_back(Cover{tex, co, offset, p == targetCover});
}
}
for (Cover cover : covers) {
cover.tex->bind();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// calculate darkening
float g = 1 - (std::min(1.0f, (abs(cover.offset) - 2) / 5)) * 0.5f;
if (abs(cover.offset) < 2)
g = 1;
glColor3f(g, g, g);
glQuad coverQuad = engine.coverPos.getCoverQuad(cover.offset, cover.tex->getAspect());
glPushName(SELECTION_CENTER + cover.index);
glBegin(GL_QUADS);
glFogCoordf(
static_cast<GLfloat>(engine.coverPos.distanceToMirror(coverQuad.topLeft)));
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(coverQuad.topLeft.as_3fv());
glFogCoordf(
static_cast<GLfloat>(engine.coverPos.distanceToMirror(coverQuad.topRight)));
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(coverQuad.topRight.as_3fv());
glFogCoordf(
static_cast<GLfloat>(engine.coverPos.distanceToMirror(coverQuad.bottomRight)));
glTexCoord2f(1.0f, 1.0f);
glVertex3fv(coverQuad.bottomRight.as_3fv());
glFogCoordf(
static_cast<GLfloat>(engine.coverPos.distanceToMirror(coverQuad.bottomLeft)));
glTexCoord2f(0.0f, 1.0f);
glVertex3fv(coverQuad.bottomLeft.as_3fv());
glEnd();
glPopName();
if (showTarget && cover.isTarget) {
bool clipPlane = false;
if (glIsEnabled(GL_CLIP_PLANE0)) {
glDisable(GL_CLIP_PLANE0);
clipPlane = true;
}
glColor3f(GetRValue(cfgTitleColor) / 255.0f, GetGValue(cfgTitleColor) / 255.0f,
GetBValue(cfgTitleColor) / 255.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_TEXTURE_2D);
glLineWidth(GLfloat(cfgHighlightWidth));
glPolygonOffset(-1.0f, -1.0f);
glEnable(GL_POLYGON_OFFSET_LINE);
glEnable(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, static_cast<void*>(&coverQuad));
glDrawArrays(GL_QUADS, 0, 4);
glDisable(GL_POLYGON_OFFSET_LINE);
glEnable(GL_TEXTURE_2D);
if (clipPlane)
glEnable(GL_CLIP_PLANE0);
}
}
}