summaryrefslogtreecommitdiffstats
path: root/src/datavis3d/engine/surface3drenderer.cpp
blob: 852ccd026bd0d8a4ebb9977e9ded5b39f0f26363 (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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtDataVis3D module.
**
** $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 "surface3dcontroller_p.h"
#include "surface3drenderer_p.h"
#include "camerahelper_p.h"
#include "objecthelper_p.h"
#include "theme_p.h"
#include "utils_p.h"
#include "drawer_p.h"

#include <QMatrix4x4>
#include <QMouseEvent>

#include <QDebug>

QT_DATAVIS3D_BEGIN_NAMESPACE

Surface3dRenderer::Surface3dRenderer(QRect rect, Surface3dController *controller)
    : QObject(controller),
      m_controller(controller),
      m_mousePressed(MouseNone),
      m_mousePos(QPoint(0, 0)),
      m_boundingRect(rect.x(), rect.y(), rect.width(), rect.height()),
      m_theme(new Theme()),
      m_labelTransparency(TransparencyFromTheme),
      m_font(QFont(QStringLiteral("Arial"))),
      m_hasNegativeValues(false),
      m_camera(new CameraHelper()),
      m_mainViewPort(rect.x(), rect.y(), rect.width(), rect.height()),
      m_sliceViewPort(rect.x(), rect.y(), rect.width(), rect.height()),
      m_isInitialized(false),
      m_backgroundObj(0),
      m_drawer(new Drawer(*m_theme, m_font, m_labelTransparency))
{
}

Surface3dRenderer::~Surface3dRenderer()
{
    if (m_backgroundObj)
        delete m_backgroundObj;
}

void Surface3dRenderer::initializeOpenGL()
{
    // Initialization is called multiple times when Qt Quick components are used
    if (m_isInitialized)
        return;

    initializeOpenGLFunctions();

    m_drawer->initializeOpenGL();

    // Init selection shader
    //initSelectionShader();

    // Load grid line mesh
    //loadGridLineMesh();

    // Load label mesh
    //loadLabelMesh();

    // Set OpenGL features
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

#if !defined(QT_OPENGL_ES_2)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
#endif

    // Set initial camera position
    // X must be 0 for rotation to work - we can use "setCameraRotation" for setting it later
    m_camera->setDefaultCameraOrientation(QVector3D(0.0f, 0.0f, 6.0f + zComp),
                                          QVector3D(0.0f, 0.0f, zComp),
                                          QVector3D(0.0f, 1.0f, 0.0f));

    // Set view port
    glViewport(m_sliceViewPort.x(), m_sliceViewPort.y(),
               m_sliceViewPort.width(), m_sliceViewPort.height());

    // Set initialized -flag
    m_isInitialized = true;

    // Resize in case we've missed resize events
    // Resize calls initSelectionBuffer and initDepthBuffer, so they don't need to be called here
    resizeNotify();

    // Load background mesh (we need to be initialized first)
    loadBackgroundMesh();
}

void Surface3dRenderer::render(const GLuint defaultFboHandle)
{
    if (!m_isInitialized)
        return;

    if (defaultFboHandle) {
        glDepthMask(true);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
    }

    QVector3D clearColor = Utils::vectorFromColor(m_theme->m_windowColor);
    glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    drawScene(defaultFboHandle);
}

void Surface3dRenderer::drawScene(const GLuint defaultFboHandle)
{
    Q_UNUSED(defaultFboHandle)

    // Specify viewport
    glViewport(m_mainViewPort.x(), m_mainViewPort.y(),
               m_mainViewPort.width(), m_mainViewPort.height());

    // Set up projection matrix
    QMatrix4x4 projectionMatrix;
    projectionMatrix.perspective(45.0f, (GLfloat)m_mainViewPort.width()
                                 / (GLfloat)m_mainViewPort.height(), 0.1f, 100.0f);

    // Calculate view matrix
    QMatrix4x4 viewMatrix = m_camera->calculateViewMatrix(m_mousePos,
                                                          1.0, //Hard coded
                                                          m_mainViewPort.width(),
                                                          m_mainViewPort.height(),
                                                          m_hasNegativeValues);
}

void Surface3dRenderer::loadBackgroundMesh()
{
    if (!m_isInitialized)
        return;

    if (m_backgroundObj)
        delete m_backgroundObj;
    if (m_hasNegativeValues)
        m_backgroundObj = new ObjectHelper(QStringLiteral(":/defaultMeshes/negativeBackground"));
    else
        m_backgroundObj = new ObjectHelper(QStringLiteral(":/defaultMeshes/background"));
    m_backgroundObj->load();
}


const QSize Surface3dRenderer::size()
{
    return m_boundingRect.size();
}

const QRect Surface3dRenderer::boundingRect()
{
    return m_boundingRect;
}

void Surface3dRenderer::setBoundingRect(const QRect boundingRect)
{
    m_boundingRect = boundingRect;
    resizeNotify();
}

void Surface3dRenderer::setWidth(const int width)
{
    m_boundingRect.setWidth(width);
    resizeNotify();
}

int Surface3dRenderer::width()
{
    return m_boundingRect.width();
}

void Surface3dRenderer::setHeight(const int height)
{
    m_boundingRect.setHeight(height);
    resizeNotify();
}

int Surface3dRenderer::height()
{
    return m_boundingRect.height();
}

void Surface3dRenderer::setX(const int x)
{
    m_boundingRect.setX(x);
}

int Surface3dRenderer::x()
{
    return m_boundingRect.x();
}

void Surface3dRenderer::setY(const int y)
{
    m_boundingRect.setY(y);
}

int Surface3dRenderer::y()
{
    return m_boundingRect.y();
}

#if defined(Q_OS_ANDROID)
void Surface3dRenderer::mouseDoubleClickEvent(QMouseEvent *event)
{

}

void Surface3dRenderer::touchEvent(QTouchEvent *event)
{

}
#endif
void Surface3dRenderer::mousePressEvent(QMouseEvent *event, const QPoint &mousePos)
{
    Q_UNUSED(event)
    Q_UNUSED(mousePos)
}

void Surface3dRenderer::mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos)
{
    Q_UNUSED(event)
    Q_UNUSED(mousePos)
}

void Surface3dRenderer::mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos)
{
    Q_UNUSED(event)
    Q_UNUSED(mousePos)
}

void Surface3dRenderer::wheelEvent(QWheelEvent *event)
{
    Q_UNUSED(event)
}

void Surface3dRenderer::resizeNotify()
{
    if (!m_isInitialized)
        return;

    m_sliceViewPort = QRect(0, 0, width(), height());
}

QT_DATAVIS3D_END_NAMESPACE