summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Render/Q3DSVisualAidWidget.cpp
blob: 6e2d79baf7d2b477207e94adfa2e1f4b38a3d7c2 (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
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Q3DSVisualAidWidget.h"
#include "Q3DSWidgetUtils.h"
#include <Qt3DCore/qtransform.h>

namespace Q3DStudio {

Q3DSVisualAidWidget::Q3DSVisualAidWidget() {}

Q3DSVisualAidWidget::Q3DSVisualAidWidget(Q3DSUipPresentation *presentation, Q3DSLayerNode *layer,
                                         Q3DSLayerNode *pickingLayer, VisualAidType type,
                                         Q3DSGraphObject *graphObject, quint64 id)
{
    m_type = type;
    m_graphObject = graphObject;
    m_presentation = presentation;

    const QString basicMaterial = QStringLiteral(
            "<MetaData>\n"
            "<Property name=\"color\" type=\"Color\" default=\"1.0 0.0 0.0\""
            " stage=\"fragment\" />\n"
            "<Property name=\"opacity\" type=\"Float\" default=\"1.0\""
            " stage=\"fragment\" />\n"
            "</MetaData>\n"
            "<Shaders type=\"GLSL\" version=\"330\">\n"
            "<Shader>\n"
            "<VertexShader>\n"
            "attribute vec3 attr_pos;\n"
            "uniform mat4 modelViewProjection;\n"
            "void main() {\n"
            "gl_Position = modelViewProjection * vec4(attr_pos, 1.0);\n"
            "</VertexShader>\n"
            "<FragmentShader>\n"
            "void main() {\n"
            "fragOutput = vec4(color, opacity);\n"
            "</FragmentShader>\n"
            "</Shader>\n"
            "</Shaders>\n"
            "<Passes><Pass></Pass></Passes>\n");

    const QString cameraMaterial = QStringLiteral(
            "<MetaData>\n"
            "<Property name=\"color\" type=\"Color\" default=\"1.0 0.0 0.0\""
            " stage=\"fragment\" />\n"
            "<Property name=\"opacity\" type=\"Float\" default=\"1.0\""
            " stage=\"fragment\" />\n"
            "<Property name=\"invProjMat\" type=\"Matrix4x4\" default=\""
            "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\""
            " stage=\"vertex\" />\n"
            "</MetaData>\n"
            "<Shaders type=\"GLSL\" version=\"330\">\n"
            "<Shader>\n"
            "<VertexShader>\n"
            "attribute vec3 attr_pos;\n"
            "uniform mat4 modelViewProjection;\n"
            "void main() {\n"
            "vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n"
            "if (length(attr_pos) > 0.001) {"
            "pos = invProjMat * vec4(attr_pos, 1.0);\n"
            "pos = pos / pos.w;\n"
            "}\n"
            "gl_Position = modelViewProjection * vec4(pos.xyz, 1.0);\n"
            "</VertexShader>\n"
            "<FragmentShader>\n"
            "void main() {\n"
            "fragOutput = vec4(color, opacity);\n"
            "</FragmentShader>\n"
            "</Shader>\n"
            "</Shaders>\n"
            "<Passes><Pass></Pass></Passes>\n");

    const QString billboardMaterial = QStringLiteral(
            "<MetaData>\n"
            "<Property name=\"color\" type=\"Color\" default=\"1.0 0.0 0.0\""
            " stage=\"fragment\" />\n"
            "<Property name=\"image\" type=\"Texture\""
            " default=\":/images/Asset-Camera-Pick.png\" stage=\"fragment\" />\n"
            "<Property name=\"billboardMatrix\" type=\"Matrix4x4\" default=\""
            "0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\""
            " stage=\"vertex\" />\n"
            "</MetaData>\n"
            "<Shaders type=\"GLSL\" version=\"330\">\n"
            "<Shader>\n"
            "<VertexShader>\n"
            "attribute vec3 attr_pos;\n"
            "attribute vec2 attr_uv0;\n"
            "varying vec2 texCoords;\n"
            "void main() {\n"
            "texCoords = attr_uv0;\n"
            "gl_Position = billboardMatrix * vec4(attr_pos.xyz, 1.0);\n"
            "</VertexShader>\n"
            "<FragmentShader>\n"
            "varying vec2 texCoords;\n"
            "void main() {\n"
            "fragOutput = texture(image, texCoords);\n"
            "</FragmentShader>\n"
            "</Shader>\n"
            "</Shaders>\n"
            "<Passes><Pass></Pass></Passes>\n");

    if (m_type == VisualAidType::Camera) {
        const QString name = QStringLiteral("StudioVisualAidWidgetFrustum")
                + QString::number(id);
        m_wireframeMaterial = createWidgetCustomMaterial(presentation, name, cameraMaterial,
                                                         Qt::white, 0.25f);
        if (m_wireframeMaterial) {
            m_wireframe = createWidgetModel(presentation, layer, name,
                                            QStringLiteral(":/res/Frustum.mesh"),
                                            QVector3D(1, 1, 1), true);
            m_wireframe->appendChildNode(m_wireframeMaterial);
        }
    } else if (m_type == VisualAidType::DirectionalLight) {
        const QString name = QStringLiteral("StudioVisualAidWidgetDirectionalLight")
                + QString::number(id);
        m_wireframeMaterial = createWidgetCustomMaterial(presentation, name, basicMaterial,
                                                         Qt::white, 0.25f);
        if (m_wireframeMaterial) {
            m_wireframe = createWidgetModel(presentation, layer, name,
                                            QStringLiteral(":/res/DirLight.mesh"),
                                            QVector3D(50, 50, 50), true);
            m_wireframe->appendChildNode(m_wireframeMaterial);
        }
    } else if (m_type == VisualAidType::PointLight) {
        const QString name = QStringLiteral("StudioVisualAidWidgetPointLight")
                + QString::number(id);
        m_wireframeMaterial = createWidgetCustomMaterial(presentation, name, basicMaterial,
                                                         Qt::white, 0.25f);
        if (m_wireframeMaterial) {
            m_wireframe = createWidgetModel(presentation, layer, name,
                                            QStringLiteral(":/res/PointLight.mesh"),
                                            QVector3D(50, 50, 50), true);
            m_wireframe->appendChildNode(m_wireframeMaterial);
        }
    } else if (m_type == VisualAidType::AreaLight) {
        const QString name = QStringLiteral("StudioVisualAidWidgetAreaLight")
                + QString::number(id);
        m_wireframeMaterial = createWidgetCustomMaterial(presentation, name, basicMaterial,
                                                         Qt::white, 0.25f);
        if (m_wireframeMaterial) {
            m_wireframe = createWidgetModel(presentation, layer, name,
                                            QStringLiteral(":/res/AreaLight.mesh"),
                                            QVector3D(50, 50, 50), true);
            m_wireframe->appendChildNode(m_wireframeMaterial);
        }
    }

    const QString iconName = QStringLiteral("StudioVisualAidWidgetIcon")
            + QString::number(id);
    m_iconMaterial = createWidgetCustomMaterial(presentation, iconName, billboardMaterial,
                                                Qt::white, 0.25f);
    if (m_iconMaterial) {
        m_icon = createWidgetModel(presentation, layer, iconName,
                                   QStringLiteral(":/res/Icon.mesh"), QVector3D(1, 1, 1));
        m_icon->appendChildNode(m_iconMaterial);

        Q3DSPropertyChangeList imageChange = {
            Q3DSPropertyChange::fromVariant(QStringLiteral("image"),
            m_type == VisualAidType::Camera ? QStringLiteral(":/images/Asset-Camera-Pick.png")
                                            : QStringLiteral(":/images/Asset-Light-Pick.png"))
        };

        m_iconMaterial->applyPropertyChanges(imageChange);
        m_iconMaterial->notifyPropertyChanges(imageChange);
    }

    const QString colBoxName = QStringLiteral("StudioVisualAidWidgetCollisionBox")
            + QString::number(id);
    m_collisionBox = createWidgetModel(presentation, pickingLayer, colBoxName,
                                       QStringLiteral("#Cube"), QVector3D(0.5f, 0.5f, 0.5f));
    m_collisionBox->appendChildNode(createWidgetDefaultMaterial(presentation, colBoxName,
                                                                Qt::white, 0.0f));

    if (!isCreated()) {
        destroy();
        m_graphObject = nullptr;
        m_presentation = nullptr;
    }
}

bool Q3DSVisualAidWidget::isCreated() const
{
    return m_wireframe && m_icon && m_collisionBox;
}

Q3DSGraphObject *Q3DSVisualAidWidget::graphObject() const
{
    return m_graphObject;
}

VisualAidType Q3DSVisualAidWidget::type() const
{
    return m_type;
}

bool Q3DSVisualAidWidget::hasGraphObject(Q3DSGraphObject *graphObject) const
{
    return m_graphObject == graphObject;
}

bool Q3DSVisualAidWidget::hasCollisionBox(Q3DSGraphObject *graphObject) const
{
    return m_collisionBox == graphObject;
}

void Q3DSVisualAidWidget::destroy()
{
    if (m_wireframe) {
        m_presentation->masterSlide()->removeObject(m_wireframe);
        m_presentation->unlinkObject(m_wireframe);
        delete m_wireframe;
        m_wireframe = nullptr;
        m_wireframeMaterial = nullptr;
    }

    if (m_icon) {
        m_presentation->masterSlide()->removeObject(m_icon);
        m_presentation->unlinkObject(m_icon);
        delete m_icon;
        m_icon = nullptr;
        m_iconMaterial = nullptr;
    }

    if (m_collisionBox) {
        m_presentation->masterSlide()->removeObject(m_collisionBox);
        m_presentation->unlinkObject(m_collisionBox);
        delete m_collisionBox;
        m_collisionBox = nullptr;
    }

    m_graphObject = nullptr;
    m_presentation = nullptr;
}

void Q3DSVisualAidWidget::update(Q3DSCameraNode *layerCameraNode, Q3DSGraphObject *selectedObject)
{
    if (!isCreated())
        return;

    Q3DSNode *node = static_cast<Q3DSNode *>(m_graphObject);
    QVector3D position;
    QVector3D rotation;
    QVector3D scale;
    calculateGlobalProperties(node, position, rotation, scale);

    m_collisionBox->notifyPropertyChanges({ m_collisionBox->setPosition(position),
                                            m_collisionBox->setRotation(rotation) });

    Q3DSPropertyChangeList list;
    list.append(m_wireframe->setPosition(position));
    list.append(m_wireframe->setRotation(rotation));

    if (m_type == VisualAidType::Camera) {
        Q3DSCameraNode *cameraNode = static_cast<Q3DSCameraNode *>(m_graphObject);
        Q3DSCameraAttached *cameraAttached = cameraNode->attached<Q3DSCameraAttached>();
        Qt3DRender::QCamera *camera = cameraAttached->camera;

        QMatrix4x4 invProjMat = camera->projectionMatrix().inverted();

        if (cameraNode->orthographic()) {
            list.append(m_wireframe->setScale(
                            QVector3D(m_presentation->presentationWidth() * 0.5f,
                                      m_presentation->presentationHeight() * 0.5f, 1)));
        } else {
            list.append(m_wireframe->setScale(QVector3D(1, 1, 1)));
        }

        Q3DSPropertyChangeList projectionChange = {
            Q3DSPropertyChange::fromVariant(
            QStringLiteral("invProjMat"), invProjMat)
        };

        m_wireframeMaterial->applyPropertyChanges(projectionChange);
        m_wireframeMaterial->notifyPropertyChanges(projectionChange);
    } else {
        list.append(m_wireframe->setScale(scale * 50.0f));
    }

    m_wireframe->notifyPropertyChanges(list);

    Q3DSCameraAttached *layerCameraAttached = layerCameraNode->attached<Q3DSCameraAttached>();
    Qt3DRender::QCamera *layerCamera = layerCameraAttached->camera;

    const QMatrix4x4 globalTransform = composeTransformMatrix(node);
    const QMatrix4x4 cameraTransform = composeTransformMatrix(layerCameraNode);
    const QMatrix4x4 cameraViewMatrix = calculateCameraViewMatrix(cameraTransform);

    QMatrix4x4 billboardMatrix;
    billboardMatrix.setColumn(3, (cameraViewMatrix * globalTransform).column(3));
    billboardMatrix.scale(16, 16, 1);
    billboardMatrix = layerCamera->projectionMatrix() * billboardMatrix;

    Q3DSPropertyChangeList billboardChange = {
        Q3DSPropertyChange::fromVariant(
        QStringLiteral("billboardMatrix"), billboardMatrix)
    };

    m_iconMaterial->applyPropertyChanges(billboardChange);
    m_iconMaterial->notifyPropertyChanges(billboardChange);

    if (!m_isSelected && selectedObject == m_graphObject) {
        m_isSelected = true;
        Q3DSPropertyChangeList colorChange = {
            Q3DSPropertyChange::fromVariant(
            QStringLiteral("color"), QColor(Qt::red))
        };

        m_wireframeMaterial->applyPropertyChanges(colorChange);
        m_wireframeMaterial->notifyPropertyChanges(colorChange);
    } else if (m_isSelected && selectedObject != m_graphObject) {
        m_isSelected = false;
        Q3DSPropertyChangeList colorChange = {
            Q3DSPropertyChange::fromVariant(
            QStringLiteral("color"), QColor(Qt::white))
        };

        m_wireframeMaterial->applyPropertyChanges(colorChange);
        m_wireframeMaterial->notifyPropertyChanges(colorChange);
    }
}

}