summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/utils/camerahelper.cpp
blob: ae134fe0b7a02d491249b5edcf0aa0d2f53e3063 (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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the QtDataVisualization module.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "camerahelper_p.h"

#include <qmath.h>
#include <QMatrix4x4>
#include <QVector3D>

QT_BEGIN_NAMESPACE_DATAVISUALIZATION

CameraHelper::CameraHelper(QObject *parent) :
    QObject(parent),
    m_position(0, 0.25, 3),
    m_target(0, 0, 0),
    m_up(0, 1, 0),
    m_previousMousePos(0,0),
    m_xRotation(0),
    m_yRotation(0),
    m_defaultXRotation(0),
    m_defaultYRotation(0),
    m_rotationSpeed(100)
{
}

CameraHelper::~CameraHelper()
{
}

void CameraHelper::setRotationSpeed(int speed)
{
    // increase for faster rotation
    m_rotationSpeed = speed;
}

void CameraHelper::setCameraRotation(const QPointF &rotation)
{
    m_xRotation = rotation.x();
    m_defaultXRotation = m_xRotation;
    m_yRotation = rotation.y();
    m_defaultYRotation = m_yRotation;
}

void CameraHelper::setDefaultCameraOrientation(const QVector3D &defaultPosition,
                                               const QVector3D &defaultTarget,
                                               const QVector3D &defaultUp)
{
    m_position = defaultPosition;
    m_target = defaultTarget;
    m_up = defaultUp;
}

QMatrix4x4 CameraHelper::calculateViewMatrix(const QPoint &mousePos, int zoom,
                                             int screenWidth, int screenHeight, bool showUnder)
{
    QMatrix4x4 viewMatrix;
    GLfloat lowerLimit = 0.0f;

    if (showUnder)
        lowerLimit = -90.0f;

    // Calculate mouse movement since last frame
    GLfloat mouseMoveX = GLfloat(m_previousMousePos.x() - mousePos.x())
            / (screenWidth / m_rotationSpeed);
    GLfloat mouseMoveY = GLfloat(m_previousMousePos.y() - mousePos.y())
            / (screenHeight / m_rotationSpeed);
    // Apply to rotations
    m_xRotation -= mouseMoveX;
    m_yRotation -= mouseMoveY;
    // Reset at 360 in x and limit to 0...90 in y
    if (qAbs(m_xRotation) >= 360.0f)
        m_xRotation = 0.0f;
    if (m_yRotation >= 90.0f)
        m_yRotation = 90.0f;
    else if (m_yRotation <= lowerLimit)
        m_yRotation = lowerLimit;

    // Apply to view matrix
    viewMatrix.lookAt(m_position, m_target, m_up);
    // Compensate for translation (if m_target is off origin)
    viewMatrix.translate(m_target.x(), m_target.y(), m_target.z());
    // Apply rotations
    // Handle x and z rotation when y -angle is other than 0
    viewMatrix.rotate(m_xRotation, 0, qCos(qDegreesToRadians(m_yRotation)),
                      qSin(qDegreesToRadians(m_yRotation)));
    // y rotation is always "clean"
    viewMatrix.rotate(m_yRotation, 1.0f, 0.0f, 0.0f);
    // handle zoom by scaling
    viewMatrix.scale((GLfloat)zoom / 100.0f);
    // Compensate for translation (if m_target is off origin)
    viewMatrix.translate(-m_target.x(), -m_target.y(), -m_target.z());

    m_previousMousePos = mousePos;
    return viewMatrix;
}

QVector3D CameraHelper::calculateLightPosition(const QVector3D &lightPosition,
                                               GLfloat fixedRotation, GLfloat distanceModifier)
{
    // Move light with camera
    QVector3D newLightPosition;
    GLfloat radiusFactor = lightPosition.z() * (1.5f + distanceModifier); // for making sure light is outside the scene at its lowest point
    GLfloat xAngle;
    GLfloat yAngle;
    if (!fixedRotation) {
        xAngle = qDegreesToRadians(m_xRotation);
        yAngle = qDegreesToRadians(m_yRotation);
    } else {
        xAngle = qDegreesToRadians(fixedRotation);
        yAngle = 0;
    }
    GLfloat radius = (radiusFactor + lightPosition.y()); // set radius to match the highest height of the light
    GLfloat zPos = radius * qCos(xAngle) * qCos(yAngle);
    GLfloat xPos = radius * qSin(xAngle) * qCos(yAngle);
    GLfloat yPos = (radiusFactor + lightPosition.y()) * qSin(yAngle);
    // Keep light in the set position in relation to camera
    newLightPosition = QVector3D(-xPos + lightPosition.x(),
                                 yPos + lightPosition.y(),
                                 zPos + lightPosition.z());
    return newLightPosition;
}

void CameraHelper::updateMousePos(const QPoint &mousePos)
{
    m_previousMousePos = mousePos;
    // if mouse position is set to (0, 0), reset rotations
    if (QPoint(0, 0) == mousePos) {
        m_xRotation = m_defaultXRotation;
        m_yRotation = m_defaultYRotation;
    }
}

QPointF CameraHelper::getCameraRotations()
{
    QPointF rotations(m_xRotation, m_yRotation);
    return rotations;
}

void CameraHelper::setCameraPreset(Q3DCamera::CameraPreset preset)
{
    switch (preset) {
    case Q3DCamera::CameraPresetFrontLow: {
        CameraHelper::setCameraRotation(QPointF(0.0f, 0.0f));
        break;
    }
    case Q3DCamera::CameraPresetFront: {
        CameraHelper::setCameraRotation(QPointF(0.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetFrontHigh: {
        CameraHelper::setCameraRotation(QPointF(0.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetLeftLow: {
        CameraHelper::setCameraRotation(QPointF(90.0f, 0.0f));
        break;
    }
    case Q3DCamera::CameraPresetLeft: {
        CameraHelper::setCameraRotation(QPointF(90.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetLeftHigh: {
        CameraHelper::setCameraRotation(QPointF(90.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetRightLow: {
        CameraHelper::setCameraRotation(QPointF(-90.0f, 0.0f));
        break;
    }
    case Q3DCamera::CameraPresetRight: {
        CameraHelper::setCameraRotation(QPointF(-90.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetRightHigh: {
        CameraHelper::setCameraRotation(QPointF(-90.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetBehindLow: {
        CameraHelper::setCameraRotation(QPointF(180.0f, 0.0f));
        break;
    }
    case Q3DCamera::CameraPresetBehind: {
        CameraHelper::setCameraRotation(QPointF(180.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetBehindHigh: {
        CameraHelper::setCameraRotation(QPointF(180.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetIsometricLeft: {
        CameraHelper::setCameraRotation(QPointF(45.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetIsometricLeftHigh: {
        CameraHelper::setCameraRotation(QPointF(45.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetIsometricRight: {
        CameraHelper::setCameraRotation(QPointF(-45.0f, 22.5f));
        break;
    }
    case Q3DCamera::CameraPresetIsometricRightHigh: {
        CameraHelper::setCameraRotation(QPointF(-45.0f, 45.0f));
        break;
    }
    case Q3DCamera::CameraPresetDirectlyAbove: {
        CameraHelper::setCameraRotation(QPointF(0.0f, 90.0f));
        break;
    }
    case Q3DCamera::CameraPresetDirectlyAboveCW45: {
        CameraHelper::setCameraRotation(QPointF(-45.0f, 90.0f));
        break;
    }
    case Q3DCamera::CameraPresetDirectlyAboveCCW45: {
        CameraHelper::setCameraRotation(QPointF(45.0f, 90.0f));
        break;
    }
    case Q3DCamera::CameraPresetFrontBelow: {
        CameraHelper::setCameraRotation(QPointF(0.0f, -45.0f));
        break;
    }
    case Q3DCamera::CameraPresetLeftBelow: {
        CameraHelper::setCameraRotation(QPointF(90.0f, -45.0f));
        break;
    }
    case Q3DCamera::CameraPresetRightBelow: {
        CameraHelper::setCameraRotation(QPointF(-90.0f, -45.0f));
        break;
    }
    case Q3DCamera::CameraPresetBehindBelow: {
        CameraHelper::setCameraRotation(QPointF(180.0f, -45.0f));
        break;
    }
    case Q3DCamera::CameraPresetDirectlyBelow: {
        CameraHelper::setCameraRotation(QPointF(0.0f, -90.0f));
        break;
    }
    default:
        break;
    }
}

QT_END_NAMESPACE_DATAVISUALIZATION