forked from noodlecollie/calliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openglrenderer.cpp
464 lines (373 loc) · 13.1 KB
/
openglrenderer.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
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
#include "openglrenderer.h"
#include <QOpenGLFunctions_4_1_Core>
#include "resourcemanager.h"
#include "sceneobject.h"
#include "shaderprogram.h"
#include "callipermath.h"
#include "scenecamera.h"
#include <QOpenGLTexture>
#include "mapscene.h"
#include <QPainter>
#include "geometrydata.h"
#include "geometryfactory.h"
#include <QSurface>
#include "shaders.h"
void OpenGLRenderer::ObjectPicker::checkDrawnObject(SceneObject *obj)
{
// Get the depth component and see if it's nearer than our current.
float newDepth = 1.0f;
functions->glReadPixels(selectionPos.x(), selectionPos.y(), 1, 1,
GL_DEPTH_COMPONENT,
GL_FLOAT,
&newDepth);
if ( newDepth < nearestDepth )
{
nearestDepth = newDepth;
selectedObject = obj;
if ( getPickColour )
{
unsigned int rgba;
functions->glReadPixels(selectionPos.x(), selectionPos.y(), 1, 1,
GL_RGBA,
GL_UNSIGNED_BYTE,
&rgba);
// As Qt sees it, this value is returned as ABGR.
// We fix this here.
selectedColour = (rgba & 0xff00ff00) |
((rgba & 0x000000ff) << 16) |
((rgba & 0x00ff0000) >> 16);
}
}
}
static OpenGLRenderer* g_pRenderer = NULL;
OpenGLRenderer* renderer()
{
return g_pRenderer;
}
OpenGLRenderer::OpenGLRenderer()
{
g_pRenderer = this;
m_pStack = NULL;
m_bPreparedForRendering = false;
m_vecDirectionalLight = QVector3D(1,0.8f,0.5f).normalized();
m_colFogColour = QColor::fromRgb(0xff999999);
m_colGlobalColour = QColor::fromRgb(0xffffffff);
m_flFogBegin = 0;
m_flFogEnd = 0;
m_iShader = 0;
m_bPicking = false;
}
OpenGLRenderer::~OpenGLRenderer()
{
resourceManager()->makeCurrent();
QOpenGLFunctions_4_1_Core* f = resourceManager()->functions();
// Nothing yet
Q_UNUSED(f);
resourceManager()->doneCurrent();
}
void OpenGLRenderer::initialise()
{
Q_ASSERT(!g_pRenderer);
new OpenGLRenderer();
}
void OpenGLRenderer::shutdown()
{
Q_ASSERT(g_pRenderer);
delete g_pRenderer;
g_pRenderer = NULL;
}
QColor OpenGLRenderer::globalColor() const
{
return m_colGlobalColour;
}
void OpenGLRenderer::setGlobalColor(const QColor &col)
{
m_colGlobalColour = col;
}
void OpenGLRenderer::setUpOpenGLResources()
{
QOpenGLFunctions_4_1_Core* f = resourceManager()->functions();
// Nothing yet.
Q_UNUSED(f);
setShaderIndex(0);
}
int OpenGLRenderer::shaderIndex() const
{
return m_iShader;
}
void OpenGLRenderer::setShaderIndex(int index)
{
m_iShader = index;
}
void OpenGLRenderer::begin()
{
ShaderProgram* program = resourceManager()->shader(shaderIndex());
Q_ASSERT(program);
// Start with autoupdate false so that we can apply things in a batch.
m_pStack = new ShaderStack(program, false);
// Set up initial things.
m_pStack->coordinateTransformPostMultiply(Math::hammerToOpenGL());
m_pStack->fogColorSetTop(fogColor());
m_pStack->fogBeginSetTop(fogBeginDistance());
m_pStack->fogEndSetTop(fogEndDistance());
m_pStack->directionalLightSetTop(directionalLight());
m_pStack->globalColorSetTop(globalColor());
// Apply them all.
m_pStack->applyAll();
m_pStack->setAutoUpdate(true);
m_vecCurrentCameraWorldPosition = QVector3D();
const HierarchicalObject* o = m_pStack->cameraParams().hierarchicalObject();
if ( o )
{
m_vecCurrentCameraWorldPosition = (o->rootToLocal().inverted() * QVector4D(0,0,0,1)).toVector3D();
}
m_bPreparedForRendering = true;
}
void OpenGLRenderer::end()
{
Q_ASSERT(m_bPreparedForRendering);
Q_ASSERT(m_pStack->inInitialState());
delete m_pStack;
m_pStack = NULL;
m_bPreparedForRendering = false;
}
void OpenGLRenderer::renderDeferred()
{
renderTranslucent();
clearDeferred();
}
void OpenGLRenderer::renderTranslucent()
{
for ( QMap<float,DeferredObject>::const_iterator it = m_TranslucentObjects.constBegin();
it != m_TranslucentObjects.constEnd(); ++it )
{
const DeferredObject &dfo = *it;
m_pStack->modelToWorldPush();
m_pStack->modelToWorldSetToIdentity();
m_pStack->modelToWorldPreMultiply(dfo.matrix);
dfo.object->draw(m_pStack);
if ( m_bPicking )
m_ObjectPicker.checkDrawnObject(dfo.object);
m_pStack->modelToWorldPop();
}
}
void OpenGLRenderer::renderSceneRecursive(SceneObject *obj, ShaderStack *stack)
{
// If the object is hidden, don't draw it or any of its children.
if ( obj->hidden() )
return;
stack->modelToWorldPush();
// TODO: Really we should change this so that the renderer always applies the modelToWorld matrix.
// The object inside can then un-apply it if necessary.
if ( !obj->isEmpty() )
{
// Check if we need to defer this object.
bool deferred = false;
if ( (obj->renderFlags() & SceneObject::Translucent) == SceneObject::Translucent )
{
deferred = true;
// Order objects within the map by depth.
// obj->position() is used because localToParent() has not yet been applied.
// The depth is negated because of the way QMap orderes keys.
QVector3D worldPos = ((stack->worldToCameraTop() * stack->modelToWorldTop()) * QVector4D(obj->position(), 1)).toVector3D();
m_TranslucentObjects.insertMulti(-(worldPos - m_vecCurrentCameraWorldPosition).lengthSquared(), DeferredObject(obj, stack->modelToWorldTop()));
}
if ( !deferred )
{
obj->draw(stack);
if ( m_bPicking )
m_ObjectPicker.checkDrawnObject(obj);
}
else
{
stack->modelToWorldPostMultiply(obj->localToParent());
}
}
else
{
stack->modelToWorldPostMultiply(obj->localToParent());
}
QList<SceneObject*> children = obj->children();
foreach ( SceneObject* o, children )
{
renderSceneRecursive(o, stack);
}
stack->modelToWorldPop();
}
QVector3D OpenGLRenderer::directionalLight() const
{
return m_vecDirectionalLight;
}
void OpenGLRenderer::setDirectionalLight(const QVector3D &dir)
{
m_vecDirectionalLight = dir.normalized();
}
void OpenGLRenderer::setDirectionalLight(const EulerAngle &ang)
{
m_vecDirectionalLight = Math::angleToVectorSimple(ang);
}
void OpenGLRenderer::renderScene(BaseScene *scene, const CameraParams ¶ms)
{
Q_ASSERT(m_bPreparedForRendering);
Q_ASSERT(m_pStack->worldToCameraTop().isIdentity());
Q_ASSERT(m_pStack->cameraProjectionTop().isIdentity());
m_pStack->setCameraParams(params);
m_pStack->worldToCameraPush();
m_pStack->worldToCameraPostMultiply(params.worldToCameraMatrix());
m_pStack->cameraProjectionPush();
m_pStack->cameraProjectionPostMultiply(params.projectionMatrix());
m_pStack->globalColorPush();
m_pStack->globalColorSetTop(globalColor());
// Render the scene.
renderSceneRecursive(scene->root(), m_pStack);
// Render any deferred things we have left.
renderDeferred();
m_pStack->globalColorPop();
m_pStack->cameraProjectionPop();
m_pStack->worldToCameraPop();
}
SceneObject* OpenGLRenderer::selectFromDepthBuffer(BaseScene *scene, const CameraParams ¶ms,
const QPoint &oglPos, QRgb *pickColor)
{
Q_ASSERT(m_bPreparedForRendering);
QOpenGLContext* context = QOpenGLContext::currentContext();
Q_ASSERT(context);
QOpenGLFunctions_4_1_Core* f = context->versionFunctions<QOpenGLFunctions_4_1_Core>();
m_ObjectPicker = ObjectPicker(f, oglPos, pickColor != NULL);
m_bPicking = true;
m_pStack->setCameraParams(params);
m_pStack->worldToCameraPush();
m_pStack->worldToCameraPostMultiply(params.worldToCameraMatrix());
m_pStack->cameraProjectionPush();
m_pStack->cameraProjectionPostMultiply(params.projectionMatrix());
m_pStack->shaderPush(resourceManager()->shader(SelectionMaskShader::staticName()));
m_pStack->m_bLockShader = true;
m_pStack->m_bPicking = true;
f->glEnable(GL_SCISSOR_TEST);
f->glScissor(oglPos.x(), oglPos.y(), 1, 1);
renderSceneRecursive(scene->root(), m_pStack);
renderDeferred();
f->glDisable(GL_SCISSOR_TEST);
m_pStack->m_bLockShader = false;
m_pStack->m_bPicking = false;
m_pStack->shaderPop();
m_pStack->cameraProjectionPop();
m_pStack->worldToCameraPop();
m_bPicking = false;
if ( pickColor )
{
*pickColor = m_ObjectPicker.selectedColour;
}
return m_ObjectPicker.selectedObject;
}
GeometryData* OpenGLRenderer::createTextQuad(const QSize &texSize, const QString &text, const QColor &col,
const QFont &font, Qt::Alignment alignment)
{
Q_ASSERT(texSize.width() > 0 && Math::isPowerOfTwo(texSize.width()));
Q_ASSERT(texSize.height() > 0 && Math::isPowerOfTwo(texSize.height()));
QImage image(texSize, QImage::Format_ARGB32);
// Make the image transparent.
image.fill(0x00000000);
// Draw on the image.
{
QPainter painter(&image);
painter.setPen(col);
painter.setFont(font);
painter.drawText(image.rect(), alignment, text);
}
// Create a texture with the image.
QOpenGLTexture* tex = new QOpenGLTexture(image.mirrored(), QOpenGLTexture::DontGenerateMipMaps);
tex->setMagnificationFilter(QOpenGLTexture::Nearest);
tex->setMinificationFilter(QOpenGLTexture::Nearest);
tex->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::ClampToEdge);
tex->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::ClampToEdge);
// Create the actual quad.
// The vertices are specified in OpenGL co-ordinates.
GeometryData* geometry = GeometryFactory::triangleQuad(1);
geometry->setLocalTexture(QSharedPointer<QOpenGLTexture>(tex));
return geometry;
}
void OpenGLRenderer::drawQuad(GeometryData *quad, const QSize &screen, const QRect &subrect, Qt::Alignment alignment,
int offset, int count)
{
Q_ASSERT(m_bPreparedForRendering);
// We transform the quad as follows:
// - Translate by (transX,transY,0). transX and transY depend on the alignment passed in, but will be one of -1, 0 or 1.
// - Scale by (0.5,-0,5,1). This makes the bottom right of the quad at (1,1).
// - Scale by (width, height, 1). This makes the quad our desired size.
// - Translate by (x,y). This puts the top left of the quad at our desired co-ordinates.
// - Convert from window to device co-ordinates.
float transX = 0, transY = 0;
if ( alignment.testFlag(Qt::AlignLeft) )
{
transX++;
}
if ( alignment.testFlag(Qt::AlignRight) )
{
transX--;
}
if ( alignment.testFlag(Qt::AlignTop) )
{
transY--;
}
if ( alignment.testFlag(Qt::AlignBottom) )
{
transY++;
}
// QMatrix4x4 transformation = Math::windowToDevice(screen.width(), screen.height())
// * Math::matrixTranslate(QVector3D(subrect.x(), subrect.y(), 0))
// * Math::matrixScale(QVector3D(subrect.width(),subrect.height(),1))
// * Math::matrixScale(QVector3D(0.5f,-0.5f,1))
// * Math::matrixTranslate(QVector3D(transX,transY,0));
float x = subrect.x(), y = subrect.y(), w = subrect.width(), h = subrect.height();
QMatrix4x4 transformation(0.5f*w, 0, 0, (0.5f*w*transX)+x,
0, -0.5f*h, 0, (-0.5f*h*transY)+y,
0, 0, 1, 0,
0, 0, 0, 1);
transformation = Math::windowToDevice(screen.width(), screen.height()) * transformation;
quad->upload();
quad->bindVertices(true);
quad->bindIndices(true);
m_pStack->modelToWorldPostMultiply(transformation);
m_pStack->worldToCameraSetToIdentity();
m_pStack->coordinateTransformSetToIdentity();
m_pStack->cameraProjectionSetToIdentity();
quad->applyDataFormat(m_pStack->shaderTop());
QOpenGLTexture* tex = quad->hasLocalTexture() ? quad->localTexture().data()
: resourceManager()->texture(quad->texture(0));
tex->bind(0);
quad->draw(offset, count);
}
QColor OpenGLRenderer::fogColor() const
{
return m_colFogColour;
}
void OpenGLRenderer::setFogColor(const QColor &col)
{
m_colFogColour = col;
}
float OpenGLRenderer::fogBeginDistance() const
{
return m_flFogBegin;
}
void OpenGLRenderer::setFogBeginDistance(float dist)
{
m_flFogBegin = dist;
}
float OpenGLRenderer::fogEndDistance() const
{
return m_flFogEnd;
}
void OpenGLRenderer::setFogEndDistance(float dist)
{
m_flFogEnd = dist;
}
void OpenGLRenderer::clearDeferred()
{
m_TranslucentObjects.clear();
}
QVector2D OpenGLRenderer::deviceCoordinates(const QVector3D &worldPos, const SceneCamera *camera)
{
// World -> camera -> OpenGL camera -> projected -> divided by w and returned.
return (camera->lens()->projectionMatrix() * Math::hammerToOpenGL() * camera->rootToLocal() * QVector4D(worldPos, 1)).toVector2DAffine();
}