summaryrefslogtreecommitdiffstats
path: root/src/imports/threed/stereovieweffect.cpp
blob: 3670b6a7919d3e282d7449f4570aef986db8c2a7 (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
/****************************************************************************
**
** Copyright (C) 2010 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$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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, 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "stereovieweffect.h"
#include <QtGui/qpainter.h>
#include <QtGui/qpaintengine.h>
#include <QtOpenGL/qgl.h>

StereoViewEffect::StereoViewEffect(StereoView *parent)
    : QGraphicsEffect(parent)
    , m_view(parent)
{
}

StereoViewEffect::~StereoViewEffect()
{
}

void StereoViewEffect::draw(QPainter *painter)
{
    // Determine if we have an OpenGL rendering surface or not.
    QPaintEngine *engine = painter->paintEngine();
    bool isOpenGL = (engine->type() == QPaintEngine::OpenGL ||
                     engine->type() == QPaintEngine::OpenGL2);

    // Determine how to draw the two eye images.
    StereoView::Layout layout = m_view->layout();
    if (layout == StereoView::RedCyan) {
        // Draw the scene twice with red and cyan filters.
        if (isOpenGL) {
            m_view->setEye(QGL::LeftEye);
            glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
            drawSource(painter);
            m_view->setEye(QGL::RightEye);
            glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
            drawSource(painter);
            glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
            m_view->setEye(QGL::NoEye);
        } else {
            // No OpenGL, so draw normally.
            drawSource(painter);
        }
    } else if (layout == StereoView::Default ||
                    layout == StereoView::Hardware) {
        // Draw the scene into the left and right back buffers.
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        if (isOpenGL && QGLContext::currentContext()->format().stereo()) {
            m_view->setEye(QGL::LeftEye);
            glDrawBuffer(GL_BACK_LEFT);
            drawSource(painter);
            m_view->setEye(QGL::RightEye);
            glDrawBuffer(GL_BACK_RIGHT);
            drawSource(painter);
            m_view->setEye(QGL::NoEye);
        } else
#endif
        if (isOpenGL && layout == StereoView::Default) {
            // No hardware stereo, so use red-cyan instead.
            m_view->setEye(QGL::LeftEye);
            glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
            drawSource(painter);
            m_view->setEye(QGL::RightEye);
            glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
            drawSource(painter);
            glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
            m_view->setEye(QGL::NoEye);
        } else {
            // No OpenGL or red-cyan not allowed, so draw normally.
            drawSource(painter);
        }
    } else if (layout != StereoView::Disabled) {
        // Fetch the original window and viewport from the painter.
        // We then use these values to set up an alternative logical
        // co-ordinate system that shifts drawing into the left or
        // right screen halves.
        QRect window = painter->window();
        QRect viewport = painter->viewport();
        QRect leftViewport(viewport);
        QRect rightViewport(viewport);
        switch (layout) {
        case StereoView::LeftRight: default:
            window.setWidth(window.width() / 2);
            // Fall through.
        case StereoView::StretchedLeftRight:
            leftViewport.setWidth(viewport.width() / 2);
            rightViewport.setX(leftViewport.right() + 1);
            rightViewport.setWidth(viewport.width() - leftViewport.width());
            break;
        case StereoView::RightLeft:
            window.setWidth(window.width() / 2);
            // Fall through.
        case StereoView::StretchedRightLeft:
            rightViewport.setWidth(viewport.width() / 2);
            leftViewport.setX(rightViewport.right() + 1);
            leftViewport.setWidth(viewport.width() - rightViewport.width());
            break;
        case StereoView::TopBottom:
            window.setHeight(window.height() / 2);
            // Fall through.
        case StereoView::StretchedTopBottom:
            leftViewport.setHeight(viewport.height() / 2);
            rightViewport.setY(leftViewport.bottom() + 1);
            rightViewport.setHeight(viewport.height() - leftViewport.height());
            break;
        case StereoView::BottomTop:
            window.setHeight(window.height() / 2);
            // Fall through.
        case StereoView::StretchedBottomTop:
            rightViewport.setHeight(viewport.height() / 2);
            leftViewport.setY(rightViewport.bottom() + 1);
            leftViewport.setHeight(viewport.height() - rightViewport.height());
            break;
        }

        // Draw the left eye image into one half of the screen.
        m_view->setEye(QGL::LeftEye);
        painter->save();
        painter->setWindow(window);
        painter->setViewport(leftViewport);
        painter->setClipRect(window, Qt::IntersectClip);
        drawSource(painter);
        painter->restore();

        // Draw the right eye image into the other half of the screen.
        m_view->setEye(QGL::RightEye);
        painter->save();
        painter->setWindow(window);
        painter->setViewport(rightViewport);
        painter->setClipRect(window, Qt::IntersectClip);
        drawSource(painter);
        painter->restore();
        m_view->setEye(QGL::NoEye);
    } else {
        // Stereo rendering has been explicitly disabled.
        drawSource(painter);
    }
}