-
Notifications
You must be signed in to change notification settings - Fork 1
/
stereoviewport.cpp
2166 lines (1956 loc) · 65.7 KB
/
stereoviewport.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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "stereoviewport.h"
#include "qquickeffect.h"
#include "qgllightmodel.h"
#include "qgllightparameters.h"
#include "qglcamera.h"
#include "qglview.h"
#include "qglsubsurface.h"
#include "qray3d.h"
#include "qglframebufferobjectsurface.h"
#include "qglmaskedsurface_p.h"
#include "qgldrawbuffersurface_p.h"
//#include "skybox.h"
#include <QOpenGLContext>
#include <QOpenGLFramebufferObject>
#include <QPainter>
#include <QEvent>
#include <QTimer>
#include <QCoreApplication>
#include <QQmlInfo>
#include <QtQuick/QQuickWindow>
#include <QOpenGLBuffer>
#include <QtCore/qthread.h>
#include <QtCore/qmutex.h>
#include <QtCore/qmath.h>
/*!
\qmltype Viewport
\instantiates Viewport
\brief The Viewport item defines the logical viewport for a 3D scene. It includes all necessary
references and parameters for the contents of the scene, as well as drawing and painting functions
\since 4.8
\ingroup qt3d::qml3d
The Viewport item is usually the outermost in a 3D scene, specifying
the size of the view, the camera position, lights, and the main 3D object:
\code
import QtQuick 2.0
import Qt3D 2.0
Viewport {
width: 640; height: 480
camera: Camera {}
light: Light {}
Item3D {
mesh: Mesh { source: "meshes/teapot.bez" }
effect: Effect {}
}
}
\endcode
\sa Camera
*/
QT_BEGIN_NAMESPACE
// copied from the top of qsgcanvas.cpp and qqmlglobal_p.h
static bool qmlNoThreadedRenderer()
{
static enum { Yes, No, Unknown } status = Unknown;
#ifndef QT_NO_THREAD
if (status == Unknown)
{
QByteArray v = qgetenv("QML_NO_THREADED_RENDERER");
bool value = !v.isEmpty() && v != "0" && v != "false";
status = (value) ? Yes : No;
}
#endif
return status == Yes;
}
class PickEvent
{
public:
PickEvent() : m_object(0), m_event(0), m_callback(-1), m_id(nextId++) {}
~PickEvent()
{
delete m_event;
// we don't own the object so don't delete that
}
QObject *object() { return m_object; }
void setObject(QObject *o) { m_object = o; }
QMouseEvent *event() { return m_event; }
void setEvent(QMouseEvent *e) { m_event = e; }
int callback() const { return m_callback; }
void setCallback(int callback) { m_callback = callback; }
quint64 id() const { return m_id; }
private:
QObject *m_object;
QMouseEvent *m_event;
int m_callback;
quint64 m_id;
// INVARIANT CONDITION: We only even construct new PickEvent objects from the
// gui thread (via calls to initiatePick in the mouse handlers) so thus the
// accesses to this NON-THREAD-SAFE static are always serialized.
static quint64 nextId;
};
quint64 PickEvent::nextId = 0;
/*
\internal
Like QMutexLocker class, except only do anything if qmlThreadedRenderer
is true.
Also hide nasty QT_NO_THREAD macros in here. If built with
QT_NO_THREAD calls should compile away to nothing.
This class is not threadsafe. Only ever use it by creating one on the local
stack and not passing references to it out of local scope.
*/
class QMutexMaybeLocker
{
public:
#ifndef QT_NO_THREAD
typedef QMutex Lock;
#else
typedef int Lock;
#endif
// Construct the locker. Also do an RAAI acquire the mutex - but only if the
// qmlNoThreadedRenderer function returns true.
QMutexMaybeLocker(Lock *mutex)
: m_mutex(mutex)
, m_isLocked(false)
{
#ifndef QT_NO_THREAD
if (!qmlNoThreadedRenderer())
{
m_mutex->lock();
m_isLocked = true;
}
#else
Q_UNUSED(mutex);
#endif
}
// This is here for when an exception or other unusual condition causes
// control to jump out of the current block. Also explicitly use the unlock
// function for the reasons stated.
~QMutexMaybeLocker()
{
#ifndef QT_NO_THREAD
unlock();
#endif
}
// Have a seperate function like QMutexLocker for this. Use it explicity for two
// reasons (rather than just nicely exiting the scope and letting the destructor
// do it) - reason one: documentation -> its clearer what is going on; reason two:
// syntax checkers and static analysis tools will complain about unused variables
// if you don't reference the constructed value.
void unlock()
{
#ifndef QT_NO_THREAD
if (!qmlNoThreadedRenderer() && m_isLocked)
{
m_mutex->unlock();
m_isLocked = false;
}
#endif
}
private:
Lock *m_mutex;
bool m_isLocked;
};
class StereoViewportPrivate
{
public:
StereoViewportPrivate();
~StereoViewportPrivate();
QColor fillColor;
bool picking;
bool showPicking;
bool showSceneGraph;
int dumpCount;
bool navigation;
bool fovzoom;
bool blending;
bool itemsInitialized;
QGLCamera *camera;
QGLLightParameters *light;
QGLLightModel *lightModel;
QWidget *viewWidget;
int pickId;
QOpenGLFramebufferObject *pickFbo;
QMap<int, QObject *> objects;
QObject *pressedObject;
Qt::MouseButton pressedButton;
QObject *enteredObject;
bool panning;
QPointF startPan;
QPointF lastPan;
QObject *lastObject;
QVector3D startEye;
QVector3D startCenter;
QVector3D startUpVector;
Qt::KeyboardModifiers panModifiers;
QMap<int, QObject*> earlyDrawList;
StereoViewport::RenderMode renderMode;
bool directRenderInitialized;
bool pickingRenderInitialized;
QList<PickEvent *> pickEventQueue;
// This lock is for the pick event queue itself. All accesses
// to that data structure must be guarded by this lock.
QMutexMaybeLocker::Lock pickEventQueueLock;
// This lock is for all of the class - generally concurrent accesses to any of the
// class instance data from other threads should be at a bare minimum.
QMutexMaybeLocker::Lock viewportLock;
QQuickWindow* canvas;
StereoViewport::StereoType stereoType;
QGLAbstractSurface *leftSurface;
QGLAbstractSurface *rightSurface;
bool surfaceDirty;
void setDefaults(QGLPainter *painter);
void setRenderSettings(QGLPainter *painter);
void getOverflow(QMouseEvent *e);
QGLAbstractSurface *leftEyeSurface(const QRect &size, QGLAbstractSurface *mainSurface);
QGLAbstractSurface *rightEyeSurface(const QRect &size, QGLAbstractSurface *mainSurface);
};
class StereoViewportSubsurface : public QGLSubsurface
{
public:
StereoViewportSubsurface(QGLAbstractSurface *surface, const QRect ®ion,
float adjust)
: QGLSubsurface(surface, region), m_adjust(adjust) {}
float aspectRatio() const;
private:
float m_adjust;
};
float StereoViewportSubsurface::aspectRatio() const
{
return QGLSubsurface::aspectRatio() * m_adjust;
}
StereoViewportPrivate::StereoViewportPrivate()
: picking(false)
, showPicking(false)
, showSceneGraph(false)
, dumpCount(10) // maybe this needs to be higher?
, navigation(true)
, fovzoom(true)
, blending(false)
, itemsInitialized(false)
, camera(0)
, light(0)
, lightModel(0)
, viewWidget(0)
, pickId(1)
, pickFbo(0)
, pressedObject(0)
, pressedButton(Qt::NoButton)
, enteredObject(0)
, panning(false)
, startPan(-1, -1)
, lastPan(-1, -1)
, lastObject(0)
, panModifiers(Qt::NoModifier)
, renderMode(StereoViewport::UnknownRender)
, directRenderInitialized(false)
, pickingRenderInitialized(false)
#ifdef QT_NO_THREAD
, pickEventQueueLock(0)
#endif
, canvas(0)
, stereoType(StereoViewport::LeftRight)
, leftSurface(0)
, rightSurface(0)
, surfaceDirty(true)
{
}
StereoViewportPrivate::~StereoViewportPrivate()
{
delete pickFbo;
qDeleteAll(pickEventQueue);
}
void StereoViewportPrivate::setDefaults(QGLPainter *painter)
{
painter->disableEffect();
// Try to restore the default options
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
// Set the default depth buffer options.
#if defined(QT_OPENGL_ES)
glClearDepthf(0);
#else
glClearDepth(0);
#endif
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
#if defined(QT_OPENGL_ES)
glDepthRangef(0.0f, 1.0f);
#else
glDepthRange(0.0f, 1.0f);
#endif
// Set the default blend options.
glDisable(GL_BLEND);
if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendColor))
painter->glBlendColor(0, 0, 0, 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendEquation))
painter->glBlendEquation(GL_FUNC_ADD);
else if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendEquationSeparate))
painter->glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
}
void StereoViewportPrivate::setRenderSettings(QGLPainter *painter)
{
// Declarative SG sets clearDepth, and other depth buffer options to
// unexpected values. Set them up to standard Qt3Dvalues.
// This seems to be only needed in beforeRendering()
#if defined(QT_OPENGL_ES)
glClearDepthf(1);
#else
glClearDepth(1);
#endif
glDepthMask(GL_TRUE);
#if defined(QT_OPENGL_ES)
glDepthRangef(0.0f, 1.0f);
#else
glDepthRange(0.0f, 1.0f);
#endif
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
QColor clearColor(Qt::black);
if (fillColor.isValid())
clearColor = fillColor;
painter->setClearColor(clearColor);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
// Returns the surface to use to render the left eye image.
QGLAbstractSurface *StereoViewportPrivate::leftEyeSurface(const QRect &originalViewport, QGLAbstractSurface* mainSurface)
{
QRect viewport;
float adjust = 1.0f;
switch (stereoType) {
case StereoViewport::Hardware:
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
if (!leftSurface)
{
if (renderMode == StereoViewport::BufferedRender)
leftSurface = new QGLDrawBufferSurface(mainSurface, GL_BACK_LEFT);
else
leftSurface = new QGLDrawBufferSurface(mainSurface, GL_FRONT_LEFT);
}
return leftSurface;
#endif
case StereoViewport::RedCyanAnaglyph:
if (!leftSurface) {
leftSurface = new QGLMaskedSurface
(mainSurface,
QGLMaskedSurface::RedMask | QGLMaskedSurface::AlphaMask);
}
return leftSurface;
case StereoViewport::LeftRight:
viewport = QRect(originalViewport.x(), originalViewport.y(), originalViewport.width() / 2, originalViewport.height());
break;
case StereoViewport::RightLeft:
viewport = QRect(originalViewport.x() + originalViewport.width() / 2, originalViewport.y(), originalViewport.width() / 2, originalViewport.height());
break;
case StereoViewport::TopBottom:
viewport = QRect(originalViewport.x(), originalViewport.y(), originalViewport.width(), originalViewport.height() / 2);
break;
case StereoViewport::BottomTop:
viewport = QRect(originalViewport.x(), originalViewport.y() + originalViewport.height() / 2, originalViewport.width(), originalViewport.height() / 2);
break;
case StereoViewport::StretchedLeftRight:
viewport = QRect(originalViewport.x(), originalViewport.y(), originalViewport.width() / 2, originalViewport.height());
adjust = 2.0f;
break;
case StereoViewport::StretchedRightLeft:
viewport = QRect(originalViewport.x() + originalViewport.width() / 2, originalViewport.y(), originalViewport.width() / 2, originalViewport.height());
adjust = 2.0f;
break;
case StereoViewport::StretchedTopBottom:
viewport = QRect(originalViewport.x(), originalViewport.y(), originalViewport.width(), originalViewport.height() / 2);
adjust = 0.5f;
break;
case StereoViewport::StretchedBottomTop:
viewport = QRect(originalViewport.x(), originalViewport.y() + originalViewport.height() / 2, originalViewport.width(), originalViewport.height() / 2);
adjust = 0.5f;
break;
}
if (!leftSurface || surfaceDirty) {
if(leftSurface) {
delete leftSurface;
}
if (adjust == 1.0f)
leftSurface = new QGLSubsurface(mainSurface, viewport);
else
leftSurface = new StereoViewportSubsurface(mainSurface, viewport, adjust);
} else {
static_cast<QGLSubsurface *>(leftSurface)->setRegion(viewport);
}
return leftSurface;
}
// Returns the surface to use to render the right eye image.
QGLAbstractSurface *StereoViewportPrivate::rightEyeSurface(const QRect &originalViewport, QGLAbstractSurface* mainSurface)
{
QRect viewport;
float adjust = 1.0f;
switch (stereoType) {
case StereoViewport::Hardware:
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
if (!rightSurface) {
rightSurface = new QGLDrawBufferSurface
(mainSurface,
renderMode == StereoViewport::BufferedRender ? GL_BACK_RIGHT : GL_FRONT_RIGHT);
}
return rightSurface;
#endif
case StereoViewport::RedCyanAnaglyph:
if (!rightSurface) {
rightSurface = new QGLMaskedSurface
(mainSurface,
QGLMaskedSurface::GreenMask | QGLMaskedSurface::BlueMask);
}
return rightSurface;
case StereoViewport::LeftRight:
viewport = QRect(originalViewport.width() / 2, 0, originalViewport.width() / 2, originalViewport.height());
break;
case StereoViewport::RightLeft:
viewport = QRect(0, 0, originalViewport.width() / 2, originalViewport.height());
break;
case StereoViewport::TopBottom:
viewport = QRect(0, originalViewport.height() / 2, originalViewport.width(), originalViewport.height() / 2);
break;
case StereoViewport::BottomTop:
viewport = QRect(0, 0, originalViewport.width(), originalViewport.height() / 2);
break;
case StereoViewport::StretchedLeftRight:
viewport = QRect(originalViewport.width() / 2, 0, originalViewport.width() / 2, originalViewport.height());
adjust = 2.0f;
break;
case StereoViewport::StretchedRightLeft:
viewport = QRect(0, 0, originalViewport.width() / 2, originalViewport.height());
adjust = 2.0f;
break;
case StereoViewport::StretchedTopBottom:
viewport = QRect(0, originalViewport.height() / 2, originalViewport.width(), originalViewport.height() / 2);
adjust = 0.5f;
break;
case StereoViewport::StretchedBottomTop:
viewport = QRect(0, 0, originalViewport.width(), originalViewport.height() / 2);
adjust = 0.5f;
break;
}
if (!rightSurface || surfaceDirty) {
if(rightSurface) {
delete rightSurface;
}
if (adjust == 1.0f)
rightSurface = new QGLSubsurface(mainSurface, viewport);
else
rightSurface = new StereoViewportSubsurface(mainSurface, viewport, adjust);
} else {
static_cast<QGLSubsurface *>(rightSurface)->setRegion(viewport);
}
return rightSurface;
}
const int StereoViewport::FBO_SIZE = 8;
/*!
\internal
Construct the class and assign it a \a parent QQuickItem.
*/
StereoViewport::StereoViewport(QQuickItem *parent)
: QQuickPaintedItem(parent)
, d(new StereoViewportPrivate())
{
// Has to be set or we crash on render. Intention of this is that
// the update() function gets called when a re-render is needed.
setFlags(QQuickItem::ItemHasContents);
connect(this, SIGNAL(widthChanged()), this, SIGNAL(viewportChanged()));
connect(this, SIGNAL(heightChanged()), this, SIGNAL(viewportChanged()));
connect(this, SIGNAL(viewportChanged()), this, SLOT(update3d()));
setCamera(new QGLCamera(this));
setLight(new QGLLightParameters(this));
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptHoverEvents(true);
}
/*!
\internal
Class destruction and cleanup.
*/
StereoViewport::~StereoViewport()
{
delete d;
}
/*!
\qmlproperty enumeration StereoViewport::renderMode
This property defines the mode of rendering of this viewport as to
whether it is direct to the GL context, or via a buffer (in this case
a framebuffer object).
If the scene occupies most of the display, but the Viewport is
not the top level item, this property can be set to DirectRender in
order to improve performance.
At construction the render mode is set to UnknownRender, but during
initialization it is updated to an appropriate default. It is an error
to set the renderMode() to UnknownRender, and in debug mode an
assert will be thrown in this case. In release mode behavior is
undefined.
By default if the viewport is a child object of another SGItem, then
the renderMode() is set to use a buffer. This is suitable for most
simple 3D items which are small in size relative to the overall QML content
displayed. The buffer is prepared by drawing the 3D items into it and
then in a second step the buffer is composited into the QML 2D scene.
Otherwise if the viewport is a top-level item, the renderMode() is set
by default to use direct rendering, in this case using a "GL Under"
approach to capture the GL context prior to other QML content being
rendered. The 3D items are then rendered directly into the context
with any 2D QML content being rendered over the top. This is suitable
where the 3D components of the scene occupies most or all of the screen.
\list
\li UnknownRender The mode is not specified.
\li DirectRender Render to the GL context directly. This is the default for top-level viewports.
\li BufferedRender Render to an offscreen buffer. This is the default for a viewport that is a child.
\endlist
*/
StereoViewport::RenderMode StereoViewport::renderMode() const
{
return d->renderMode;
}
void StereoViewport::setRenderMode(StereoViewport::RenderMode mode)
{
Q_ASSERT(mode != UnknownRender);
if (d->renderMode != mode)
{
d->renderMode = mode;
if (d->renderMode == BufferedRender)
{
setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);
if (d->canvas)
{
disconnect(d->canvas, SIGNAL(beforeRendering()),
this, SLOT(beforeRendering()));
d->canvas->setClearBeforeRendering(true);
d->directRenderInitialized = false;
}
}
else
{
// If there is no engine at this point the setup will
// be done in the sceneGraphInitialized handler
if (d->canvas)
{
connect(d->canvas, SIGNAL(beforeRendering()),
this, SLOT(beforeRendering()), Qt::DirectConnection);
d->canvas->setClearBeforeRendering(false);
d->directRenderInitialized = true;
}
}
emit viewportChanged();
}
}
StereoViewport::StereoType StereoViewport::stereoType() const
{
return d->stereoType;
}
/*!
\qmlproperty bool StereoViewport::fillColor()
The color to use for the background of the viewport, if any. When no color is set
then the underlying QML background will show through (the viewport will be trans-
parent).
If a color is set here, then the viewport will be filled with that color before
any content is rendered into it.
The default value for this property is no color (an invalid color) resulting in
a transparent viewport.
\sa showPicking
*/
QColor StereoViewport::fillColor() const
{
return d->fillColor;
}
void StereoViewport::setFillColor(const QColor &color)
{
if (d->fillColor != color)
{
d->fillColor = color;
emit viewportChanged();
}
}
/*!
\qmlproperty bool StereoViewport::picking
User interaction in QML/3d is handled through the concept of object picking. Each
item has a unique picking id which is queried for a given screen click position when the
mouse is clicked.
If this property is set to true, picking will be supported for this
viewport, while if the property is false, no picking will be applied.
The default value for this property is false.
\sa showPicking
*/
bool StereoViewport::picking() const
{
return d->picking;
}
void StereoViewport::setPicking(bool value)
{
if (value != d->picking)
{
d->picking = value;
if (d->picking && d->canvas)
{
connect(d->canvas, SIGNAL(beforeRendering()),
this, SLOT(objectForPoint()), Qt::DirectConnection);
d->pickingRenderInitialized = true;
}
else
{
if (d->canvas)
disconnect(d->canvas, SIGNAL(beforeRendering()),
this, SLOT(objectForPoint()));
d->pickingRenderInitialized = false;
}
emit viewportChanged();
}
}
/*!
\qmlproperty bool StereoViewport::showPicking
The underlying mechanism for picking is based on painting an off-screen buffer with a flat
coloured image containing all of the objects with a unique color value.
Setting this property to true will display this flat-colour picking
representation in the viewport, which can be useful for debugging
problems with object selection.
The default value for this property is false.
\sa picking
*/
bool StereoViewport::showPicking() const
{
return d->showPicking;
}
void StereoViewport::setShowPicking(bool value)
{
d->showPicking = value;
emit viewportChanged();
}
/*!
\qmlproperty bool StereoViewport::showSceneGraph
This property controls whether or not the 3D scenegraph structure is dumped
to the console when the viewport is first rendered. Studying the output can be very
useful for optimising the scene, and for detecting issues with rendering, such
as misplaced textures, materials, geometry and so on.
By default the value is set to false.
*/
bool StereoViewport::showSceneGraph() const
{
return d->showSceneGraph;
}
void StereoViewport::setShowSceneGraph(bool show)
{
if (show != d->showSceneGraph)
{
d->showSceneGraph = show;
emit showSceneGraphChanged();
}
}
/*!
\qmlproperty bool StereoViewport::navigation
This property is used to set or unset camera navigation in for the viewport.
Camera navigation allows the user to move the camera position around using the mouse.
By default, camera navigation is set to true.
*/
bool StereoViewport::navigation() const
{
return d->navigation;
}
void StereoViewport::setNavigation(bool value)
{
d->navigation = value;
emit viewportChanged();
}
/*!
\qmlproperty bool StereoViewport::fovzoom
This property is used to set or unset zooming based on field-of-view (fov). Normally
zooming is achieved by moving the camera physically closer to the target object. This
options achieves zooming by narrowing or broadening the fov.
By default, fov zooming is set to false.
*/
bool StereoViewport::fovzoom() const
{
return d->fovzoom;
}
void StereoViewport::setFovzoom(bool value)
{
d->fovzoom = value;
emit viewportChanged();
}
/*!
\qmlproperty bool StereoViewport::blending
The blending property is used to enable or disable GL_BLEND
on the viewport, for alpha blending of drawn objects.
By default, blending is set to false.
\sa Effect::blending
*/
bool StereoViewport::blending() const
{
return d->blending;
}
void StereoViewport::setBlending(bool value)
{
d->blending = value;
emit viewportChanged();
}
/*!
\qmlproperty bool StereoViewport::antialiasing
The antialiasing property is used to enable or disable antialiasing
on the viewport. This property only has an effect if renderMode() == BufferedRender.
For renderMode() == DirectRender antialiasing can be enabled by setting the
QSurfaceFormat.
By default, antialiasing is set to false.
*/
bool StereoViewport::antialiasing() const
{
return QQuickPaintedItem::antialiasing();
}
void StereoViewport::setAntialiasing(bool value)
{
if (value != QQuickPaintedItem::antialiasing()) {
QQuickPaintedItem::setAntialiasing(value);
Q_EMIT antialiasingChanged();
}
}
/*!
\qmlproperty Camera StereoViewport::camera
This property sets the camera parameters which will be used for
the appropriate viewing transforms in OpenGL. The default is
a perspective camera with its eye located at (0, 0, 10) looking
at the center (0, 0, 0), with the y axis as up.
*/
QGLCamera *StereoViewport::camera() const
{
return d->camera;
}
void StereoViewport::setCamera(QGLCamera *value)
{
if (d->camera != value) {
if (d->camera) {
disconnect(d->camera, SIGNAL(projectionChanged()),
this, SLOT(cameraChanged()));
disconnect(d->camera, SIGNAL(viewChanged()),
this, SLOT(cameraChanged()));
}
d->camera = value;
if (d->camera) {
connect(d->camera, SIGNAL(projectionChanged()),
this, SLOT(cameraChanged()));
connect(d->camera, SIGNAL(viewChanged()),
this, SLOT(cameraChanged()));
}
cameraChanged();
}
}
/*!
\qmlproperty Light StereoViewport::light
This property defines the main scene light to use for 3D items
that are drawn in this viewport.
\sa lightModel
*/
QGLLightParameters *StereoViewport::light() const
{
return d->light;
}
void StereoViewport::setLight(QGLLightParameters *value)
{
if (d->light != value) {
if (d->light) {
disconnect(d->light, SIGNAL(lightChanged()),
this, SLOT(update3d()));
}
d->light = value;
if (d->light) {
connect(d->light, SIGNAL(lightChanged()),
this, SLOT(update3d()));
}
emit viewportChanged();
}
}
/*!
\qmlproperty LightModel StereoViewport::lightModel
The user is able to set a lighting model for the 3d environment through the use of the
lightModel property. By default the light model is undefined.
\sa light
*/
QGLLightModel *StereoViewport::lightModel() const
{
return d->lightModel;
}
void StereoViewport::setLightModel(QGLLightModel *value)
{
if (d->lightModel != value) {
if (d->lightModel) {
disconnect(d->lightModel, SIGNAL(lightModelChanged()),
this, SLOT(update3d()));
}
d->lightModel = value;
if (d->lightModel) {
connect(d->lightModel, SIGNAL(lightModelChanged()),
this, SLOT(update3d()));
}
emit viewportChanged();
}
}
class ViewportSubsurface : public QGLSubsurface
{
public:
ViewportSubsurface(QGLAbstractSurface *surface, const QRect ®ion, float adjust)
: QGLSubsurface(surface, region)
, m_adjust(adjust)
{
}
~ViewportSubsurface()
{
}
float aspectRatio() const;
private:
float m_adjust;
};
float ViewportSubsurface::aspectRatio() const
{
return QGLSubsurface::aspectRatio() * m_adjust;
}
/*!
\reimp
\internal
Only used in the case of renderMode() == BufferedRender.
Called by QQuickPaintedItem to refresh the content.
\sa beforeRendering(), setRenderMode()
*/
void StereoViewport::paint(QPainter *painter)
{
Q_ASSERT(renderMode() == BufferedRender);
if (!isVisible())
return;
QGLPainter glPainter;
if (!glPainter.begin(painter))
{
qWarning("GL graphics system is not active; cannot use 3D items");
return;
}
if (d->fillColor.isValid())
{
glPainter.setClearColor(d->fillColor);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
else
{
glClear(GL_DEPTH_BUFFER_BIT);
}
glEnable(GL_DEPTH_TEST);
render(&glPainter);
d->setDefaults(&glPainter);
}
/*!
\internal
Only used in the case of renderMode() == DirectRender.
Called by beforeRendering signal from QQuickContext.