summaryrefslogtreecommitdiffstats
path: root/src/threed/viewing/qglview.cpp
blob: f22314f15c4979c8b0ec0b89de62b5ab5b7de461 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qglview.h"
#include "qglframebufferobject.h"
#include "qglsubsurface.h"
#include "qglmaskedsurface_p.h"
#include "qglwidgetsurface.h"
#include "qgldrawbuffersurface_p.h"
#include "qray3d.h"
#include <QtGui/qevent.h>
#include <QtCore/qmap.h>
#include <QtGui/qapplication.h>
#include <QtCore/qtimer.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qdebug.h>

QT_BEGIN_NAMESPACE

/*!
    \class QGLView
    \brief The QGLView class extends QGLWidget with support for 3D viewing.
    \since 4.8
    \ingroup qt3d
    \ingroup qt3d::viewing

    \section1 Navigating

    Navigation in 3D space is possible under keyboard and mouse control.

    Holding down the left mouse button and dragging it will rotate the
    camera() position around the object being viewed.  Holding down the
    left mouse button and the Shift key pans the view in a plane without
    rotating the viewed object.

    Using the mouse wheel, the view can be zoomed in or out.  If the
    system does not have a mouse wheel, then holding down the left mouse
    button and the Control key and moving the mouse up and down will
    also zoom in and out.

    On the keyboard, the left, right, up, and down keys can also be used
    to shift the camera() position around the object being viewed.  Shift
    and Control modify keys the same way they modify the left mouse
    button above.  Hitting the Home key will cause the camera position
    to be reset to its original position.

    \section1 Stereo viewing support

    Note - Stereo viewing is experimental and unsupported.

    If the hardware supports stereo buffers, then each time the scene needs
    to be painted, QGLView renders it twice: first from the perspective of
    the left eye, and then from the perspective of the right eye.
    The separation between the eye positions is specified by
    QGLCamera::eyeSeparation().  If the eye separation is zero,
    then stereo viewing is disabled and only a single image will
    be rendered per frame.

    Three kinds of stereo viewing are possible: hardware stereo,
    anaglyph stereo, and double image stereo.

    Hardware stereo relies upon specialized hardware that can render
    the left and right eye images into separate buffers and then show
    them independently to each eye through the use of polarized glasses
    or similar technology.  Hardware stereo is used if the \c{-stereo-hw}
    command-line option is supplied or if the user explicitly requests
    stereo buffers when the QGLView is constructed:

    \code
    QGLFormat format(QGLFormat::defaultFormat());
    format.setOption(QGL::StereoBuffers);
    QGLView view(format);
    \endcode

    Anaglyph stereo is used when the hardware doesn't have specialized
    stereo buffer support.  The left eye image is masked by a red
    filter and the right eye image is masked by a cyan filter.  This makes
    the resulting images suitable for viewing with standard red-cyan
    anaglyph glasses.

    When using red-cyan anaglyphs, it is recommended that the
    scene use non-primary object colors.  Pure primary colors such
    as red, green, and blue will only appear to one of the viewer's
    eyes and will inhibit the 3D effect.  Non-primary colors or
    grayscale should be used to get the best effects.

    Red-cyan anaglyphs can be disorienting to some viewers.  If hardware
    stereo is not available and stereo viewing is not critical to
    the application, then stereo can be disabled by setting
    QGLCamera::eyeSeparation() to zero.

    Double image stereo involves drawing the left and right eye
    images in a double-wide or double-high window, with the hardware
    combining the images.  Four different configurations are available:
    LeftRight, RightLeft, TopBottom,
    and BottomTop, according to the layout of the eye images.
    Double image stereo is selected by calling setStereoType().  It is
    the responsibility of the application to resize the window to
    twice its normal size to accommodate the images.

    Ctrl-Left and Ctrl-Right can be used to make the eye separation
    smaller or larger under keyboard control.

    A number of command-line options are available to select the
    stereo mode of the QGLView so that the application does not
    need to select the mode itself:

    \table
    \row \o \c{-stereo-hw} \o \l Hardware.
    \row \o \c{-stereo-lr} \o LeftRight.
    \row \o \c{-stereo-rl} \o RightLeft.
    \row \o \c{-stereo-tb} \o TopBottom.
    \row \o \c{-stereo-bt} \o BottomTop.
    \row \o \c{-stereo-stretched-lr} \o StretchedLeftRight.
    \row \o \c{-stereo-stretched-rl} \o StretchedRightLeft.
    \row \o \c{-stereo-stretched-tb} \o StretchedTopBottom.
    \row \o \c{-stereo-stretched-bt} \o StretchedBottomTop.
    \endtable

    The option can also be supplied in the \c{Quick3D_OPTIONS} environment
    variable:

    \code
    $ Quick3D_OPTIONS="-stereo-lr" ./cubehouse
    \endcode

    If the application sets the stereo type with setStereoType(),
    that will be used.  Next is the command-line setting, and finally
    the contents of the environment variable.
*/

/*!
    \enum QGLView::Option
    This enum defines an option for QGLView.

    \value ObjectPicking Object picking is enabled.  Disabled by default.
    \value ShowPicking Objects are rendered with their pick colors instead
           of their normal colors and materials.  This can help debug
           problems with object picking.  Disabled by default.
    \value CameraNavigation Camera navigation using the keyboard and mouse
           is enabled.  Enabled by default.
    \omitvalue PaintingLog
    \value FOVZoom Enables zooming by changing field of view instead of
           physically moving the camera.
*/

/*!
    \enum QGLView::StereoType
    This enum defines the type of stereo viewing technology being used by QGLView.

    \value Hardware Specialized stereo hardware is being used.
    \value RedCyanAnaglyph Stereo is being simulated for viewing by
        red-cyan anaglyph classes.
    \value LeftRight The view is double-wide with the left eye
        image on the left of the window.
    \value RightLeft The view is double-wide with the left eye
        image on the right of the window.
    \value TopBottom The view is double-high with the left eye
        image on the top of the window.
    \value BottomTop The view is double-high with the left eye
        image on the bottom of the window.
    \value StretchedLeftRight Same as LeftRight, but with the
        left and right eye images stretched to double their width.
    \value StretchedRightLeft Same as RightLeft, but with the
        left and right eye images stretched to double their width.
    \value StretchedTopBottom Same as TopBottom, but with the
        left and right eye images stretched to double their height.
    \value StretchedBottomTop Same as BottomTop, but with the
        left and right eye images stretched to double their height.
*/

class QGLViewPrivate
{
public:
    QGLViewPrivate(QGLView *parent)
        : view(parent), mainSurface(parent)
    {
        options = QGLView::CameraNavigation;
        fbo = 0;
        leftSurface = 0;
        rightSurface = 0;

        if (parent->format().stereo())
            stereoType = QGLView::Hardware;
        else
            stereoType = QGLView::RedCyanAnaglyph;

        pickBufferForceUpdate = true;
        pickBufferMaybeInvalid = true;
        updateQueued = false;

        pressedObject = 0;
        pressedButton = Qt::NoButton;
        enteredObject = 0;

        defaultCamera = new QGLCamera(parent);
        camera = defaultCamera;

        panning = false;
        startPan = QPoint(-1, -1);
        lastPan = QPoint(-1, -1);
        panModifiers = Qt::NoModifier;
        QObject::connect(defaultCamera, SIGNAL(projectionChanged()),
                         parent, SLOT(cameraChanged()));
        QObject::connect(defaultCamera, SIGNAL(viewChanged()),
                         parent, SLOT(cameraChanged()));

        logTime.start();
        lastFrameTime.start();
        QByteArray env = qgetenv("Quick3D_LOG_EVENTS");
        if (env == "1")
            options |= QGLView::PaintingLog;
    }
    ~QGLViewPrivate()
    {
        delete fbo;
        delete leftSurface;
        delete rightSurface;
    }

    QGLView *view;
    QGLView::Options options;
    QGLView::StereoType stereoType;
    QGLFramebufferObject *fbo;
    QGLWidgetSurface mainSurface;
    QGLAbstractSurface *leftSurface;
    QGLAbstractSurface *rightSurface;
    bool pickBufferForceUpdate;
    bool pickBufferMaybeInvalid;
    bool updateQueued;
    QMap<int, QObject *> objects;
    QObject *pressedObject;
    Qt::MouseButton pressedButton;
    QObject *enteredObject;
    QGLCamera *defaultCamera;
    QGLCamera *camera;
    bool panning;
    QPoint startPan;
    QPoint lastPan;
    QVector3D startEye;
    QVector3D startCenter;
    QVector3D startUpVector;
    Qt::KeyboardModifiers panModifiers;
    QTime logTime;
    QTime enterTime;
    QTime lastFrameTime;

    inline void logEnter(const char *message);
    inline void logLeave(const char *message);

    void processStereoOptions(QGLView *view);
    void processStereoOptions(QGLView *view, const QString &arg);

    QGLAbstractSurface *leftEyeSurface(const QSize &size);
    QGLAbstractSurface *rightEyeSurface(const QSize &size);
    QGLAbstractSurface *bothEyesSurface();
};

inline void QGLViewPrivate::logEnter(const char *message)
{
    if ((options & QGLView::PaintingLog) == 0)
        return;
    int ms = logTime.elapsed();
    enterTime.start();
    int sinceLast = lastFrameTime.restart();
    qDebug("LOG[%d:%02d:%02d.%03d]: ENTER: %s (%d ms since last enter)",
           ms / 3600000, (ms / 60000) % 60,
           (ms / 1000) % 60, ms % 1000, message, sinceLast);
}

inline void QGLViewPrivate::logLeave(const char *message)
{
    if ((options & QGLView::PaintingLog) == 0)
        return;
    int ms = logTime.elapsed();
    int duration = enterTime.elapsed();
    qDebug("LOG[%d:%02d:%02d.%03d]: LEAVE: %s (%d ms elapsed)",
           ms / 3600000, (ms / 60000) % 60,
           (ms / 1000) % 60, ms % 1000, message, duration);
}

static QString qt_gl_stereo_arg()
{
    QStringList args = QApplication::arguments();
    foreach (QString arg, args) {
        if (arg.startsWith(QLatin1String("-stereo-")))
            return arg;
    }
    QByteArray options(qgetenv("Quick3D_OPTIONS"));
    args = QString::fromLocal8Bit
        (options.constData(), options.size()).split(QLatin1Char(' '));
    foreach (QString arg, args) {
        if (arg.startsWith(QLatin1String("-stereo-")))
            return arg;
    }
    return QString();
}

void QGLViewPrivate::processStereoOptions(QGLView *view)
{
    if (stereoType == QGLView::Hardware)
        return;
    QString arg = qt_gl_stereo_arg();
    if (!arg.isEmpty())
        processStereoOptions(view, arg);
}

void QGLViewPrivate::processStereoOptions(QGLView *view, const QString &arg)
{
    // If the command-line contains an option that starts with "-stereo-",
    // then convert it into options that define the size and type of
    // stereo window to use for a top-level QGLView.  Possible options:
    //
    //      hw - use hardware stereo
    //      lr, rl, tb, bt - specify the eye order (default is left-right)
    //      stretched - used stretched versions of double wide/high modes.
    //
    QStringList opts = arg.mid(8).split(QLatin1Char('-'));
    QGLView::StereoType stereoType;
    bool stretched = opts.contains(QLatin1String("stretched"));
    if (opts.contains(QLatin1String("rl"))) {
        stereoType = stretched ? QGLView::StretchedRightLeft : QGLView::RightLeft;
    } else if (opts.contains(QLatin1String("tb"))) {
        stereoType = stretched ? QGLView::StretchedTopBottom : QGLView::TopBottom;
    } else if (opts.contains(QLatin1String("bt"))) {
        stereoType = stretched ? QGLView::StretchedBottomTop : QGLView::BottomTop;
    } else {
        stereoType = stretched ? QGLView::StretchedLeftRight : QGLView::LeftRight;
    }
    view->setStereoType(stereoType);
}

class QGLViewSubsurface : public QGLSubsurface
{
public:
    QGLViewSubsurface(QGLAbstractSurface *surface, const QRect &region,
                      qreal adjust)
        : QGLSubsurface(surface, region), m_adjust(adjust) {}

    qreal aspectRatio() const;

private:
    qreal m_adjust;
};

qreal QGLViewSubsurface::aspectRatio() const
{
    return QGLSubsurface::aspectRatio() * m_adjust;
}

// Returns the surface to use to render the left eye image.
QGLAbstractSurface *QGLViewPrivate::leftEyeSurface(const QSize &size)
{
    QRect viewport;
    qreal adjust = 1.0f;
    switch (stereoType) {
    case QGLView::Hardware:
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        if (!leftSurface) {
            leftSurface = new QGLDrawBufferSurface
                (&mainSurface,
                 view->doubleBuffer() ? GL_BACK_LEFT : GL_FRONT_LEFT);
        }
        return leftSurface;
#endif
    case QGLView::RedCyanAnaglyph:
        if (!leftSurface) {
            leftSurface = new QGLMaskedSurface
                (&mainSurface,
                 QGLMaskedSurface::RedMask | QGLMaskedSurface::AlphaMask);
        }
        return leftSurface;
    case QGLView::LeftRight:
        viewport = QRect(0, 0, size.width() / 2, size.height());
        break;
    case QGLView::RightLeft:
        viewport = QRect(size.width() / 2, 0, size.width() / 2, size.height());
        break;
    case QGLView::TopBottom:
        viewport = QRect(0, 0, size.width(), size.height() / 2);
        break;
    case QGLView::BottomTop:
        viewport = QRect(0, size.height() / 2, size.width(), size.height() / 2);
        break;
    case QGLView::StretchedLeftRight:
        viewport = QRect(0, 0, size.width() / 2, size.height());
        adjust = 2.0f;
        break;
    case QGLView::StretchedRightLeft:
        viewport = QRect(size.width() / 2, 0, size.width() / 2, size.height());
        adjust = 2.0f;
        break;
    case QGLView::StretchedTopBottom:
        viewport = QRect(0, 0, size.width(), size.height() / 2);
        adjust = 0.5f;
        break;
    case QGLView::StretchedBottomTop:
        viewport = QRect(0, size.height() / 2, size.width(), size.height() / 2);
        adjust = 0.5f;
        break;
    }
    if (!leftSurface) {
        if (adjust == 1.0f)
            leftSurface = new QGLSubsurface(&mainSurface, viewport);
        else
            leftSurface = new QGLViewSubsurface(&mainSurface, viewport, adjust);
    } else {
        static_cast<QGLSubsurface *>(leftSurface)->setRegion(viewport);
    }
    return leftSurface;
}

// Returns the surface to use to render the right eye image.
QGLAbstractSurface *QGLViewPrivate::rightEyeSurface(const QSize &size)
{
    QRect viewport;
    qreal adjust = 1.0f;
    switch (stereoType) {
    case QGLView::Hardware:
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        if (!rightSurface) {
            rightSurface = new QGLDrawBufferSurface
                (&mainSurface,
                 view->doubleBuffer() ? GL_BACK_RIGHT : GL_FRONT_RIGHT);
        }
        return rightSurface;
#endif
    case QGLView::RedCyanAnaglyph:
        if (!rightSurface) {
            rightSurface = new QGLMaskedSurface
                (&mainSurface,
                 QGLMaskedSurface::GreenMask | QGLMaskedSurface::BlueMask);
        }
        return rightSurface;
    case QGLView::LeftRight:
        viewport = QRect(size.width() / 2, 0, size.width() / 2, size.height());
        break;
    case QGLView::RightLeft:
        viewport = QRect(0, 0, size.width() / 2, size.height());
        break;
    case QGLView::TopBottom:
        viewport = QRect(0, size.height() / 2, size.width(), size.height() / 2);
        break;
    case QGLView::BottomTop:
        viewport = QRect(0, 0, size.width(), size.height() / 2);
        break;
    case QGLView::StretchedLeftRight:
        viewport = QRect(size.width() / 2, 0, size.width() / 2, size.height());
        adjust = 2.0f;
        break;
    case QGLView::StretchedRightLeft:
        viewport = QRect(0, 0, size.width() / 2, size.height());
        adjust = 2.0f;
        break;
    case QGLView::StretchedTopBottom:
        viewport = QRect(0, size.height() / 2, size.width(), size.height() / 2);
        adjust = 0.5f;
        break;
    case QGLView::StretchedBottomTop:
        viewport = QRect(0, 0, size.width(), size.height() / 2);
        adjust = 0.5f;
        break;
    }
    if (!rightSurface) {
        if (adjust == 1.0f)
            rightSurface = new QGLSubsurface(&mainSurface, viewport);
        else
            rightSurface = new QGLViewSubsurface(&mainSurface, viewport, adjust);
    } else {
        static_cast<QGLSubsurface *>(rightSurface)->setRegion(viewport);
    }
    return rightSurface;
}

// Returns a surface that can be used to render a non-stereoscopic
// image into both eyes at the same time.  Returns null if the eyes
// must be rendered one at a time.
QGLAbstractSurface *QGLViewPrivate::bothEyesSurface()
{
    switch (stereoType) {
    case QGLView::Hardware:
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        return 0;
#endif
    case QGLView::RedCyanAnaglyph:
        return &mainSurface;
    default:
        return 0;
    }
}

static QGLFormat makeStereoGLFormat(const QGLFormat& format)
{
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
    QGLFormat fmt(format);
    if (qt_gl_stereo_arg() == QLatin1String("-stereo-hw"))
        fmt.setOption(QGL::StereoBuffers);
    return fmt;
#else
    QGLFormat fmt(format);
    fmt.setOption(QGL::NoStereoBuffers);
    return fmt;
#endif
}

/*!
    Constructs a new view widget and attaches it to \a parent.

    This constructor will request a stereo rendering context if
    the hardware supports it.
*/
QGLView::QGLView(QWidget *parent)
    : QGLWidget(makeStereoGLFormat(QGLFormat::defaultFormat()), parent)
{
    d = new QGLViewPrivate(this);
    setMouseTracking(true);
    if (!parent)
        d->processStereoOptions(this);
}

/*!
    Constructs a new view widget and attaches it to \a parent.
    The \a format argument specifies the desired QGLFormat
    rendering options.

    If \a format does not include the stereo option, then a stereo
    viewing context will not be requested.
*/
QGLView::QGLView(const QGLFormat& format, QWidget *parent)
    : QGLWidget(format, parent)
{
    d = new QGLViewPrivate(this);
    setMouseTracking(true);
    if (!parent)
        d->processStereoOptions(this);
}

/*!
    Destroys this view widget.
*/
QGLView::~QGLView()
{
    delete d;
}

/*!
    Returns the options for this view.  The default value is
    CameraNavigation.

    \sa setOptions(), setOption()
*/
QGLView::Options QGLView::options() const
{
    return d->options;
}

/*!
    Sets the options for this view to \a value.

    \sa options(), setOption()
*/
void QGLView::setOptions(QGLView::Options value)
{
    d->options = value;
}

/*!
    Enables or disables \a option according to \a value.

    \sa options(), setOptions()
*/
void QGLView::setOption(QGLView::Option option, bool value)
{
    if (value)
        d->options |= option;
    else
        d->options &= ~option;
}

/*!
    Returns the type of stereo viewing technology that is in use.

    \sa setStereoType()
*/
QGLView::StereoType QGLView::stereoType() const
{
    return d->stereoType;
}

/*!
    Sets the \a type of stereo viewing technology that is in use.
    The request takes effect at the next repaint.

    The request is ignored stereoType() or \a type is Hardware,
    because hardware stereo can only be enabled if the hardware
    supports it, and then it can never be disabled.

    \sa stereoType()
*/
void QGLView::setStereoType(QGLView::StereoType type)
{
    if (d->stereoType == Hardware || type == Hardware)
        return;
    if (d->stereoType == type)
        return;
    d->stereoType = type;

    // Destroy the current surface objects so that they will
    // be re-generated the next time we paint the widget.
    delete d->leftSurface;
    delete d->rightSurface;
    d->leftSurface = 0;
    d->rightSurface = 0;
}

/*!
    Registers an \a object with this view to be notified when
    \a objectId is selected with the mouse.  The \a object must
    persist for the lifetime of the QGLView, or until
    deregisterObject() is called for \a objectId.

    \sa deregisterObject(), objectForPoint()
*/
void QGLView::registerObject(int objectId, QObject *object)
{
    d->objects[objectId] = object;
}

/*!
    Deregisters the object associated with \a objectId.

    \sa registerObject()
*/
void QGLView::deregisterObject(int objectId)
{
    d->objects.remove(objectId);
}

/*!
    Returns the camera parameters.  The camera defines the projection
    to apply to convert eye co-ordinates into window co-ordinates,
    and the position and orientation of the viewer's eye.

    \sa setCamera()
*/
QGLCamera *QGLView::camera() const
{
    return d->camera;
}

/*!
    Sets the camera parameters to \a value.  The camera defines the
    projection to apply to convert eye co-ordinates into window
    co-ordinates, and the position and orientation of the viewer's eye.

    If \a value is null, then the default camera object will be used.

    This function will call update() to force the view to
    update with the new camera parameters upon the next event loop.

    \sa camera()
*/
void QGLView::setCamera(QGLCamera *value)
{
    if (!value)
        value = d->defaultCamera;

    if (d->camera == value)
        return;

    disconnect(d->camera, SIGNAL(projectionChanged()),
               this, SLOT(cameraChanged()));
    disconnect(d->camera, SIGNAL(viewChanged()),
               this, SLOT(cameraChanged()));

    d->camera = value;

    connect(d->camera, SIGNAL(projectionChanged()),
            this, SLOT(cameraChanged()));
    connect(d->camera, SIGNAL(viewChanged()),
            this, SLOT(cameraChanged()));

    cameraChanged();
}

/*!
    Maps \a point from viewport co-ordinates to eye co-ordinates.

    The returned vector will have its x and y components set to the
    position of the point on the near plane, and the z component
    set to the inverse of the camera's near plane.

    This function is used for converting a mouse event's position
    into eye co-ordinates within the current camera view.

    \sa QGLCamera::mapPoint()
*/
QVector3D QGLView::mapPoint(const QPoint &point) const
{
    QSize viewportSize(size());
    qreal aspectRatio;

    // Get the size of the underlying paint device.
    int width = viewportSize.width();
    int height = viewportSize.height();

    // Use the device's DPI setting to determine the pixel aspect ratio.
    int dpiX = logicalDpiX();
    int dpiY = logicalDpiY();
    if (dpiX <= 0 || dpiY <= 0)
        dpiX = dpiY = 1;

    // Derive the aspect ratio based on window and pixel size.
    if (width <= 0 || height <= 0)
        aspectRatio = 1.0f;
    else
        aspectRatio = ((qreal)(width * dpiY)) / ((qreal)(height * dpiX));

    // Map the point into eye co-ordinates.
    return d->camera->mapPoint(point, aspectRatio, viewportSize);
}

void QGLView::cameraChanged()
{
    // The pick buffer will need to be refreshed at the new camera position.
    d->pickBufferForceUpdate = true;

    // Queue an update for the next event loop.
    update();
}

/*!
    \internal
*/
void QGLView::initializeGL()
{
    d->logEnter("QGLView::initializeGL");
    QGLPainter painter;
    painter.begin();

    // Set the default depth buffer options.
    glEnable(GL_DEPTH_TEST);
    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.
    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);

    glDisable(GL_CULL_FACE);
    initializeGL(&painter);
    d->logLeave("QGLView::initializeGL");
}

/*!
    \internal
*/
void QGLView::resizeGL(int w, int h)
{
    // Set up the standard viewport for the new window size.
    glViewport(0, 0, w, h);

    // We will need to regenerate the pick buffer.
    d->pickBufferForceUpdate = true;
}

/*!
    \internal
*/
void QGLView::paintGL()
{
    d->logEnter("QGLView::paintGL");
    // We may need to regenerate the pick buffer on the next mouse event.
    d->pickBufferMaybeInvalid = true;

    // Paint the scene contents.
    QGLPainter painter;
    QGLAbstractSurface *surface;
    painter.begin();
    if (d->options & QGLView::ShowPicking &&
            d->stereoType == QGLView::RedCyanAnaglyph) {
        // If showing picking, then render normally.  This really
        // only works if we aren't using hardware or double stereo.
        painter.setPicking(true);
        painter.clearPickObjects();
        painter.setEye(QGL::NoEye);
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.setPicking(false);
    } else if (d->camera->eyeSeparation() == 0.0f &&
               (surface = d->bothEyesSurface()) != 0) {
        // No camera separation, so render the same image into both buffers.
        painter.pushSurface(surface);
        painter.setEye(QGL::NoEye);
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.popSurface();
    } else {
        // Paint the scene twice, from the perspective of each camera.
        QSize size(this->size());
        painter.setEye(QGL::LeftEye);
        if (d->stereoType != QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear both eyes at the same time.
        painter.pushSurface(d->leftEyeSurface(size));
        if (d->stereoType == QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear the left eye only.
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        if (d->stereoType == QGLView::RedCyanAnaglyph)
            glClear(GL_DEPTH_BUFFER_BIT);
        painter.setEye(QGL::RightEye);
        painter.setSurface(d->rightEyeSurface(size));
        if (d->stereoType == QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear the right eye only.
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.popSurface();
    }
    d->logLeave("QGLView::paintGL");
}

/*!
    Initializes the current GL context represented by \a painter.

    \sa paintGL()
*/
void QGLView::initializeGL(QGLPainter *painter)
{
    Q_UNUSED(painter);
}

/*!
    Performs early painting operations just after \a painter
    is initialized but before the camera is set up.  The default
    implementation clears the color buffer and depth buffer.

    This function is typically overridden to draw scene backdrops
    on the color buffer before the rest of the scene is drawn
    by paintGL().

    \sa paintGL()
*/
void QGLView::earlyPaintGL(QGLPainter *painter)
{
    Q_UNUSED(painter);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

/*!
    \fn void QGLView::paintGL(QGLPainter *painter)

    Paints the scene onto \a painter.  The color and depth buffers
    will have already been cleared, and the camera() position set.

    If QGLPainter::isPicking() is set for \a painter, then the
    function should paint the scene onto \a painter in
    "object picking mode".  The scene will be rendered into a
    background buffer using flat colors so that mouse events
    can determine which object lies under the mouse pointer.

    The default implementation of picking will typically just
    render the scene normally.  However, some applications
    may wish to render a simpler scene that omits unselectable
    objects and uses simpler meshes for the selectable objects.

    \sa earlyPaintGL()
*/

/*!
    Processes the mouse press event \a e.
*/
void QGLView::mousePressEvent(QMouseEvent *e)
{
    QObject *object;
    if (!d->panning && (d->options & QGLView::ObjectPicking) != 0)
        object = objectForPoint(e->pos());
    else
        object = 0;
    if (d->pressedObject) {
        // Send the press event to the pressed object.  Use a position
        // of (0, 0) if the mouse is still within the pressed object,
        // or (-1, -1) if the mouse is no longer within the pressed object.
        QMouseEvent event
            (QEvent::MouseButtonPress,
             (d->pressedObject == object) ? QPoint(0, 0) : QPoint(-1, -1),
             e->globalPos(), e->button(), e->buttons(), e->modifiers());
        QCoreApplication::sendEvent(d->pressedObject, &event);
    } else if (object) {
        // Record the object that was pressed and forward the event.
        d->pressedObject = object;
        d->enteredObject = 0;
        d->pressedButton = e->button();

        // Send a mouse press event for (0, 0).
        QMouseEvent event(QEvent::MouseButtonPress, QPoint(0, 0),
                          e->globalPos(), e->button(), e->buttons(),
                          e->modifiers());
        QCoreApplication::sendEvent(object, &event);
    } else if ((d->options & QGLView::CameraNavigation) != 0 &&
                    e->button() == Qt::LeftButton) {
        d->panning = true;
        d->lastPan = d->startPan = e->pos();
        d->startEye = d->camera->eye();
        d->startCenter = d->camera->center();
        d->startUpVector = d->camera->upVector();
        d->panModifiers = e->modifiers();
#ifndef QT_NO_CURSOR
        setCursor(Qt::ClosedHandCursor);
#endif
    }
    QGLWidget::mousePressEvent(e);
}

/*!
    Processes the mouse release event \a e.
*/
void QGLView::mouseReleaseEvent(QMouseEvent *e)
{
    if (d->panning && e->button() == Qt::LeftButton) {
        d->panning = false;
#ifndef QT_NO_CURSOR
        unsetCursor();
#endif
    }
    if (d->pressedObject) {
        // Notify the previously pressed object about the release.
        QObject *object = objectForPoint(e->pos());
        QObject *pressed = d->pressedObject;
        if (e->button() == d->pressedButton) {
            d->pressedObject = 0;
            d->pressedButton = Qt::NoButton;
            d->enteredObject = object;

            // Send the release event to the pressed object.  Use a position
            // of (0, 0) if the mouse is still within the pressed object,
            // or (-1, -1) if the mouse is no longer within the pressed object.
            QMouseEvent event
                (QEvent::MouseButtonRelease,
                 (pressed == object) ? QPoint(0, 0) : QPoint(-1, -1),
                 e->globalPos(), e->button(), e->buttons(), e->modifiers());
            QCoreApplication::sendEvent(pressed, &event);

            // Send leave and enter events if necessary.
            if (object != pressed) {
                sendLeaveEvent(pressed);
                if (object)
                    sendEnterEvent(object);
            }
        } else {
            // Some other button than the original was released.
            // Forward the event to the pressed object.
            QMouseEvent event
                (QEvent::MouseButtonRelease,
                 (pressed == object) ? QPoint(0, 0) : QPoint(-1, -1),
                 e->globalPos(), e->button(), e->buttons(), e->modifiers());
            QCoreApplication::sendEvent(pressed, &event);
        }
    }
    QGLWidget::mouseReleaseEvent(e);
}

/*!
    Processes the mouse double click event \a e.
*/
void QGLView::mouseDoubleClickEvent(QMouseEvent *e)
{
    if ((d->options & QGLView::ObjectPicking) != 0) {
        QObject *object = objectForPoint(e->pos());
        if (object) {
            // Simulate a double click event for (0, 0).
            QMouseEvent event
                (QEvent::MouseButtonDblClick, QPoint(0, 0),
                 e->globalPos(), e->button(), e->buttons(), e->modifiers());
            QCoreApplication::sendEvent(object, &event);
        }
    }
    QGLWidget::mouseDoubleClickEvent(e);
}

/*!
    Processes the mouse move event \a e.
*/
void QGLView::mouseMoveEvent(QMouseEvent *e)
{
    if (d->panning) {
        QPoint delta = e->pos() - d->startPan;
        if (e->modifiers() == d->panModifiers) {
            d->camera->setEye(d->startEye);
            d->camera->setCenter(d->startCenter);
            d->camera->setUpVector(d->startUpVector);
        } else {
            d->startPan = d->lastPan;
            delta = e->pos() - d->startPan;
            d->startEye = d->camera->eye();
            d->startCenter = d->camera->center();
            d->startUpVector = d->camera->upVector();
            d->panModifiers = e->modifiers();
        }
        d->lastPan = e->pos();
        if ((e->modifiers() & Qt::ControlModifier) != 0)
            wheel(delta.y() * -60);
        else if ((e->modifiers() & Qt::ShiftModifier) != 0)
            pan(delta.x(), delta.y());
        else
            rotate(delta.x(), delta.y());
    } else if ((d->options & QGLView::ObjectPicking) != 0) {
        QObject *object = objectForPoint(e->pos());
        if (d->pressedObject) {
            // Send the move event to the pressed object.  Use a position
            // of (0, 0) if the mouse is still within the pressed object,
            // or (-1, -1) if the mouse is no longer within the pressed object.
            QMouseEvent event
                (QEvent::MouseMove,
                 (d->pressedObject == object) ? QPoint(0, 0) : QPoint(-1, -1),
                 e->globalPos(), e->button(), e->buttons(), e->modifiers());
            QCoreApplication::sendEvent(d->pressedObject, &event);
        } else if (object) {
            if (object != d->enteredObject) {
                if (d->enteredObject)
                    sendLeaveEvent(d->enteredObject);
                d->enteredObject = object;
                sendEnterEvent(d->enteredObject);
            }
            QMouseEvent event
                (QEvent::MouseMove, QPoint(0, 0),
                 e->globalPos(), e->button(), e->buttons(), e->modifiers());
            QCoreApplication::sendEvent(object, &event);
        } else if (d->enteredObject) {
            sendLeaveEvent(d->enteredObject);
            d->enteredObject = 0;
        }
    }
    QGLWidget::mouseMoveEvent(e);
}

/*!
    Processes the leave event \a e.
*/
void QGLView::leaveEvent(QEvent *e)
{
    if (!d->pressedObject && d->enteredObject) {
        sendLeaveEvent(d->enteredObject);
        d->enteredObject = 0;
    }
    QGLWidget::leaveEvent(e);
}

#ifndef QT_NO_WHEELEVENT

/*!
    Processes the wheel event \a e.
*/
void QGLView::wheelEvent(QWheelEvent *e)
{
    if ((d->options & QGLView::CameraNavigation) != 0)
        wheel(e->delta());
    QGLWidget::wheelEvent(e);
}

#endif

/*!
    Processes the key press event \a e.
*/
void QGLView::keyPressEvent(QKeyEvent *e)
{
    QGLCamera *camera;
    qreal sep;

    if ((d->options & QGLView::CameraNavigation) == 0) {
        QGLWidget::keyPressEvent(e);
        return;
    }
    switch (e->key()) {

        case Qt::Key_Escape:
        case Qt::Key_Q:
        {
            if (parentWidget() == 0)
                close();
        }

        case Qt::Key_Left:
        {
            if ((e->modifiers() & Qt::ShiftModifier) != 0) {
                pan(-10, 0);
            } else if ((e->modifiers() & Qt::ControlModifier) != 0) {
                camera = this->camera();
                sep = camera->eyeSeparation();
                sep -= (sep / 10.0f);
                if (sep < 0.0f)
                    sep = 0.0f;
                camera->setEyeSeparation(sep);
                e->accept();
                return;
            } else {
                rotate(-10, 0);
            }
        }
        break;

        case Qt::Key_Right:
        {
            if ((e->modifiers() & Qt::ShiftModifier) != 0) {
                pan(10, 0);
            } else if ((e->modifiers() & Qt::ControlModifier) != 0) {
                camera = this->camera();
                sep = camera->eyeSeparation();
                sep += (sep / 10.0f);
                camera->setEyeSeparation(sep);
                e->accept();
                return;
            } else {
                rotate(10, 0);
            }
        }
        break;

        case Qt::Key_Up:
        {
            if ((e->modifiers() & Qt::ControlModifier) != 0)
                wheel(120);
            else if ((e->modifiers() & Qt::ShiftModifier) != 0)
                pan(0, -10);
            else
                rotate(0, -10);
        }
        break;

        case Qt::Key_Down:
        {
            if ((e->modifiers() & Qt::ControlModifier) != 0)
                wheel(-120);
            else if ((e->modifiers() & Qt::ShiftModifier) != 0)
                pan(0, 10);
            else
                rotate(0, 10);
        }
        break;
    }
    QGLWidget::keyPressEvent(e);
}

class QGLViewPickSurface : public QGLAbstractSurface
{
public:
    QGLViewPickSurface(QGLView *view, QGLFramebufferObject *fbo,
                       const QSize &areaSize);

    QPaintDevice *device() const;
    bool activate(QGLAbstractSurface *prevSurface);
    void deactivate(QGLAbstractSurface *nextSurface);
    QRect viewportGL() const;

private:
    QGLView *m_view;
    QGLFramebufferObject *m_fbo;
    QRect m_viewportGL;
};

QGLViewPickSurface::QGLViewPickSurface
        (QGLView *view, QGLFramebufferObject *fbo, const QSize &areaSize)
    : QGLAbstractSurface(504)
    , m_view(view)
    , m_fbo(fbo)
    , m_viewportGL(QPoint(0, 0), areaSize)
{
}

QPaintDevice *QGLViewPickSurface::device() const
{
    return m_view;
}

bool QGLViewPickSurface::activate(QGLAbstractSurface *prevSurface)
{
    Q_UNUSED(prevSurface);
    if (m_fbo)
        m_fbo->bind();
    return true;
}

void QGLViewPickSurface::deactivate(QGLAbstractSurface *nextSurface)
{
    Q_UNUSED(nextSurface);
    if (m_fbo)
        m_fbo->release();
}

QRect QGLViewPickSurface::viewportGL() const
{
    return m_viewportGL;
}

/*!
    Returns the registered object that is under the mouse position
    specified by \a point.  This function may need to regenerate
    the contents of the pick buffer by repainting the scene
    with paintGL().

    \sa registerObject()
*/
QObject *QGLView::objectForPoint(const QPoint &point)
{
    QPoint pt(point);

    // What is the size of the drawing area after correcting for stereo?
    // Also adjust the mouse position to always be in the left half.
    QSize areaSize = size();
    switch (d->stereoType) {
    case QGLView::LeftRight:
    case QGLView::RightLeft:
        areaSize = QSize(areaSize.width() / 2, areaSize.height());
        if (pt.x() >= areaSize.width())
            pt.setX(pt.x() - areaSize.width());
        break;
    case QGLView::TopBottom:
    case QGLView::BottomTop:
        areaSize = QSize(areaSize.width(), areaSize.height() / 2);
        if (pt.y() >= areaSize.height())
            pt.setY(pt.y() - areaSize.height());
        break;
    case QGLView::StretchedLeftRight:
    case QGLView::StretchedRightLeft: {
        int halfwid = areaSize.width() / 2;
        if (pt.x() >= halfwid)
            pt.setX((pt.x() - halfwid) * 2);
        else
            pt.setX(pt.x() * 2);
        break; }
    case QGLView::StretchedTopBottom:
    case QGLView::StretchedBottomTop: {
        int halfht = areaSize.height() / 2;
        if (pt.y() >= halfht)
            pt.setY((pt.y() - halfht) * 2);
        else
            pt.setY(pt.y() * 2);
        break; }
    default: break;
    }

    // Check the area boundaries in case a mouse move has
    // moved the pointer outside the window.
    if (pt.x() < 0 || pt.x() >= areaSize.width() ||
            pt.y() < 0 || pt.y() >= areaSize.height())
        return 0;

    // Do we need to refresh the pick buffer contents?
    QGLPainter painter(this);
    if (d->pickBufferForceUpdate) {
        // Initialize the painter, which will make the window context current.
        painter.setPicking(true);
        painter.clearPickObjects();

        // Create a framebuffer object as big as the window to act
        // as the pick buffer if we are single buffered.  If we are
        // double-buffered, then use the window back buffer.
        bool useBackBuffer = doubleBuffer();
        if (!useBackBuffer) {
            QSize fbosize = QGL::nextPowerOfTwo(areaSize);
            if (!d->fbo) {
                d->fbo = new QGLFramebufferObject(fbosize, QGLFramebufferObject::CombinedDepthStencil);
            } else if (d->fbo->size() != fbosize) {
                delete d->fbo;
                d->fbo = new QGLFramebufferObject(fbosize, QGLFramebufferObject::CombinedDepthStencil);
            }
        }

        // Render the pick version of the scene.
        QGLViewPickSurface surface(this, d->fbo, areaSize);
        painter.pushSurface(&surface);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        painter.setEye(QGL::NoEye);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.setPicking(false);
        painter.popSurface();

        // The pick buffer contents are now valid, unless we are using
        // the back buffer - we cannot rely upon it being valid next time.
        d->pickBufferForceUpdate = useBackBuffer;
        d->pickBufferMaybeInvalid = false;
    }

    // Pick the object under the mouse.
    if (d->fbo)
        d->fbo->bind();
    int objectId = painter.pickObject(pt.x(), areaSize.height() - 1 - pt.y());
    QObject *object = d->objects.value(objectId, 0);
    if (d->fbo)
        d->fbo->release();

    // Release the framebuffer object and return.
    painter.end();
    doneCurrent();
    return object;
}

void QGLView::sendEnterEvent(QObject *object)
{
    QEvent event(QEvent::Enter);
    QCoreApplication::sendEvent(object, &event);
}

void QGLView::sendLeaveEvent(QObject *object)
{
    QEvent event(QEvent::Leave);
    QCoreApplication::sendEvent(object, &event);
}

// Zoom in and out according to the change in wheel delta.
void QGLView::wheel(int delta)
{
    if (d->options & QGLView::FOVZoom) {     
        //Use field-of view as zoom (much like a traditional camera)
        qreal scale = qAbs(viewDelta(delta, delta).x());
        if (delta < 0)
            scale = -scale;
        if (scale >= 0.0f)
            scale += 1.0f;
        else
            scale = 1.0f / (1.0f - scale);
        qreal fov = d->camera->fieldOfView();
        if (fov != 0.0f)
            d->camera->setFieldOfView(d->camera->fieldOfView() / scale);
        else
            d->camera->setViewSize(d->camera->viewSize() / scale);
    } else {
        // enable this to get wheel navigation that actually zooms by moving the
        // camera back, as opposed to making the angle of view wider.        
        QVector3D viewVector= camera()->eye() - camera()->center();
        qreal zoomMag = viewVector.length();
        qreal zoomIncrement = -float(delta) / 100.0f;
        if (!qFuzzyIsNull(zoomIncrement))
        {
            zoomMag += zoomIncrement;
            if (zoomMag < 1.0f)
                zoomMag = 1.0f;

            QRay3D viewLine(camera()->center(), viewVector.normalized());
            camera()->setEye(viewLine.point(zoomMag));
        }
    }

}

// Pan left/right/up/down without rotating about the object.
void QGLView::pan(int deltax, int deltay)
{
    QPointF delta = viewDelta(deltax, deltay);
    QVector3D t = d->camera->translation(delta.x(), -delta.y(), 0.0f);

    // Technically panning the eye left should make the object appear to
    // move off to the right, but this looks weird on-screen where the user
    // actually thinks they are picking up the object and dragging it rather
    // than moving the eye.  We therefore apply the inverse of the translation
    // to make it "look right".
    d->camera->setEye(d->camera->eye() - t);
    d->camera->setCenter(d->camera->center() - t);
}

// Rotate about the object being viewed.
void QGLView::rotate(int deltax, int deltay)
{
    int rotation = d->camera->screenRotation();
    if (rotation == 90 || rotation == 270) {
        qSwap(deltax, deltay);
    }
    if (rotation == 90 || rotation == 180) {
        deltax = -deltax;
    }
    if (rotation == 180 || rotation == 270) {
        deltay = -deltay;
    }
    qreal anglex = deltax * 90.0f / width();
    qreal angley = deltay * 90.0f / height();
    QQuaternion q = d->camera->pan(-anglex);
    q *= d->camera->tilt(-angley);
    d->camera->rotateCenter(q);
}

/*!
    Converts \a deltax and \a deltay into percentages of the
    view width and height.  Returns a QPointF containing the
    percentage values, typically between -1 and 1.

    This function is typically used by subclasses to convert a
    change in mouse position into a relative distance travelled
    across the field of view.

    The returned value is corrected for the camera() screen
    rotation and view size.
*/
QPointF QGLView::viewDelta(int deltax, int deltay) const
{
    int w = width();
    int h = height();
    bool scaleToWidth;
    qreal scaleFactor, scaleX, scaleY;
    QSizeF viewSize = d->camera->viewSize();
    if (w >= h) {
        if (viewSize.width() >= viewSize.height())
            scaleToWidth = true;
        else
            scaleToWidth = false;
    } else {
        if (viewSize.width() >= viewSize.height())
            scaleToWidth = false;
        else
            scaleToWidth = true;
    }
    int rotation = d->camera->screenRotation();
    if (rotation == 90 || rotation == 270) {
        scaleToWidth = !scaleToWidth;
        qSwap(deltax, deltay);
    }
    if (rotation == 90 || rotation == 180) {
        deltax = -deltax;
    }
    if (rotation == 180 || rotation == 270) {
        deltay = -deltay;
    }
    if (scaleToWidth) {
        scaleFactor = 2.0f / viewSize.width();
        scaleX = scaleFactor * ((qreal)h) / ((qreal)w);
        scaleY = scaleFactor;
    } else {
        scaleFactor = 2.0f / viewSize.height();
        scaleX = scaleFactor;
        scaleY = scaleFactor * ((qreal)w) / ((qreal)h);
    }
    return QPointF(deltax * scaleX / w, deltay * scaleY / h);
}

/*!
    \fn QPointF QGLView::viewDelta(const QPoint &delta) const
    \overload

    Converts the x and y components of \a delta into percentages
    of the view width and height.  Returns a QPointF containing
    the percentage values, typically between -1 and 1.
*/


QT_END_NAMESPACE