summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/Render/StudioTranslationWidget.cpp
blob: 1ff7bd482dc8b17a40a11bd29c948823f7b9fedc (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
/****************************************************************************
**
** Copyright (C) 2006 NVIDIA Corporation.
** Copyright (C) 2017 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 "Qt3DSCommonPrecompile.h"
#include "StudioWidgetImpl.h"
#include "foundation/Qt3DSAtomic.h"
#include "render/Qt3DSRenderContext.h"
#include "Qt3DSRenderShaderCodeGenerator.h"
#include "Qt3DSRenderNode.h"
#include "render/Qt3DSRenderShaderProgram.h"
#include "StudioUtils.h"
#include "StudioPreferences.h"

using namespace qt3ds::widgets;

namespace {

struct STranslationWidget : public SStudioWidgetImpl<StudioWidgetTypes::Translation>
{
    typedef SStudioWidgetImpl<StudioWidgetTypes::Translation> TBase;
    NVRenderInputAssembler *m_XAxis;
    NVRenderInputAssembler *m_YAxis;
    NVRenderInputAssembler *m_ZAxis;
    NVRenderInputAssembler *m_wideXAxis;
    NVRenderInputAssembler *m_wideYAxis;
    NVRenderInputAssembler *m_wideZAxis;
    NVRenderInputAssembler *m_XPlane;
    NVRenderInputAssembler *m_YPlane;
    NVRenderInputAssembler *m_ZPlane;

    volatile QT3DSI32 mRefCount;

    STranslationWidget(NVAllocatorCallback &inAlloc)
        : TBase(inAlloc)
        , m_XAxis(nullptr)
        , m_YAxis(nullptr)
        , m_ZAxis(nullptr)
        , m_wideXAxis(nullptr)
        , m_wideYAxis(nullptr)
        , m_wideZAxis(nullptr)
        , m_XPlane(nullptr)
        , m_YPlane(nullptr)
        , m_ZPlane(nullptr)
        , mRefCount(0)
    {
    }

    QT3DS_IMPLEMENT_REF_COUNT_ADDREF_RELEASE(m_Allocator);

    void Render(IRenderWidgetContext &inWidgetContext, NVRenderContext &inRenderContext) override
    {
        // Widgets have to clear the depth buffer; they shouldn't interact with other components
        // but they should self-occlude.
        inRenderContext.SetDepthWriteEnabled(true);
        inRenderContext.Clear(qt3ds::render::NVRenderClearValues::Depth);
        inRenderContext.SetDepthTestEnabled(true);
        if (m_XAxis == nullptr) {
            TBase::SetupRender(inWidgetContext, inRenderContext);
            float pixelRatio = float(StudioUtils::devicePixelRatio());
            QT3DSF32 axisWidth = pixelRatio;
            QT3DSF32 triWidth = 4 * CStudioPreferences::selectorLineWidth() * pixelRatio;
            QT3DSF32 axisStart = CStudioPreferences::selectorLineLength() / 3.0f * pixelRatio;
            QT3DSF32 axisLength = CStudioPreferences::selectorLineLength() * pixelRatio;
            QT3DSF32 triLength = axisStart;
            m_XAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(1, 0, 0),
                                 axisStart, axisLength, triLength, axisWidth, triWidth,
                                 "TranslationWidgetXAxis");
            m_YAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(0, 1, 0),
                                 axisStart, axisLength, triLength, axisWidth, triWidth,
                                 "TranslationWidgetYAxis");
            m_ZAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(0, 0, -1),
                                 axisStart, axisLength, triLength, axisWidth, triWidth,
                                 "TranslationWidgetZAxis");

            QT3DSF32 axisPos = GetDiscPos() * pixelRatio;
            QT3DSF32 axisDiscRadius = GetDiscRadius() * pixelRatio;
            QT3DSF32 axisRingRadius = GetDiscRingRadius() * pixelRatio;
            m_XPlane =
                CreateRingedDisc(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(1, 0, 0),
                                 QT3DSVec3(0, axisPos, -axisPos), axisDiscRadius, axisRingRadius,
                                 0.0f, 1.0f, "TranslationWidgetXPlane");
            m_YPlane =
                CreateRingedDisc(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(0, 1, 0),
                                 QT3DSVec3(axisPos, 0, -axisPos), axisDiscRadius, axisRingRadius,
                                 0.0f, 1.0f, "TranslationWidgetYPlane");
            m_ZPlane =
                CreateRingedDisc(m_Allocator, inWidgetContext, inRenderContext, QT3DSVec3(0, 0, -1),
                                 QT3DSVec3(axisPos, axisPos, 0), axisDiscRadius, axisRingRadius,
                                 0.0f, 1.0f, "TranslationWidgetZPlane");
        }

        QT3DSMat44 theMVP;

        if (m_isAxisHelper) {
            auto helpWidgetInfo
                    = inWidgetContext.GetWidgetRenderInformation(*m_Node,
                                                                 QT3DSVec3(0, 0, 0),
                                                                 RenderWidgetModes::Global);
            auto viewport = inRenderContext.GetViewport();
            SCamera theTempCamera;

            QT3DSMat44 theViewProjection;
            theTempCamera.m_Flags.SetOrthographic(true);

            // Move the camera back far enough that we can see everything
            static QT3DSF32 theCameraSetback(600);

            // Move the camera to position axis helper to the lower left.
            theTempCamera.m_Position.z = -theCameraSetback;
            theTempCamera.m_Position.x = 0;
            theTempCamera.m_Position.y = 0;
            theTempCamera.m_ClipFar = 2.0f * theCameraSetback;
            // Use entire viewport
            theTempCamera.CalculateGlobalVariables(
                        viewport,
                        QT3DSVec2((QT3DSF32)viewport.m_Width, (QT3DSF32)viewport.m_Height));
            theTempCamera.CalculateViewProjectionMatrix(theViewProjection);

            // Get the rotation for the scene view camera (the actual direction of world axes)
            // and rotate the widget axes to match world axes. Scale the widget to be smaller
            // and less intrusive than actual translation widget.
            auto rot = QT3DSMat44::createIdentity();
            if (helpWidgetInfo.m_Camera->m_Flags.IsOrthographic()) {
                rot = helpWidgetInfo.m_Camera->m_LocalTransform;
                rot.setPosition({0, 0, 0});
                rot = rot.getInverse().getTranspose();
            } else {
                helpWidgetInfo.m_Camera->CalculateRotationMatrix(rot);
            }

            helpWidgetInfo.m_Camera->FlipCoordinateSystem(rot);
            // In the scale calculation 200.f = 120 / 0.6; 120 being the default width and 0.6
            // being the default scale for the axis helper
            const float scale = float(viewport.m_Width) / 200.f;
            rot.scale(QT3DSVec4(scale, scale, scale, 1));
            theMVP = theViewProjection * rot.getTranspose();

            if (m_wideXAxis == nullptr) {
                QT3DSF32 axisWidth = 6;
                QT3DSF32 triWidth = axisWidth * 3;
                QT3DSF32 axisStart = 0;
                QT3DSF32 axisLength = axisWidth * 10;
                QT3DSF32 triLength = axisWidth * 5;
                m_wideXAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext,
                                         QT3DSVec3(1, 0, 0), axisStart, axisLength, triLength,
                                         axisWidth, triWidth, "TranslationWidgetWideXAxis");
                m_wideYAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext,
                                         QT3DSVec3(0, 1, 0), axisStart, axisLength, triLength,
                                         axisWidth, triWidth, "TranslationWidgetWideYAxis");
                m_wideZAxis = CreateAxis(m_Allocator, inWidgetContext, inRenderContext,
                                         QT3DSVec3(0, 0, -1), axisStart, axisLength, triLength,
                                         axisWidth, triWidth, "TranslationWidgetWideZAxis");
            }
        } else {
            theMVP = TBase::SetupMVP(inWidgetContext);
        }

        if (isNodeBehindCamera())
            return;

        inRenderContext.SetBlendingEnabled(false);
        inRenderContext.SetDepthTestEnabled(true);
        inRenderContext.SetDepthWriteEnabled(true);
        inRenderContext.SetCullingEnabled(false);
        inRenderContext.SetActiveShader(m_Shader);
        m_Shader->SetPropertyValue("model_view_projection", theMVP);
        // temporary set color1 to white so we can hopefully see mistakes.
        m_Shader->SetPropertyValue("color1", QT3DSVec3(1, 1, 1));

        QT3DSVec3 theXColor(GetXAxisColor());
        QT3DSVec3 theYColor(GetYAxisColor());
        QT3DSVec3 theZColor(GetZAxisColor());
        QT3DSVec3 theRingColor(QT3DSVec3(.8f, .8f, .8f));

        if (!m_isAxisHelper) {
            RenderSingleToneGeometry(StudioWidgetComponentIds::XAxis, theXColor, inRenderContext,
                                     m_XAxis);
            RenderSingleToneGeometry(StudioWidgetComponentIds::YAxis, theYColor, inRenderContext,
                                     m_YAxis);
            RenderSingleToneGeometry(StudioWidgetComponentIds::ZAxis, theZColor, inRenderContext,
                                     m_ZAxis);
            RenderTwoToneGeometry(StudioWidgetComponentIds::XPlane, theXColor, theRingColor,
                                  inRenderContext, m_XPlane);
            RenderTwoToneGeometry(StudioWidgetComponentIds::YPlane, theYColor, theRingColor,
                                  inRenderContext, m_YPlane);
            RenderTwoToneGeometry(StudioWidgetComponentIds::ZPlane, theZColor, theRingColor,
                                  inRenderContext, m_ZPlane);
        } else {
            RenderSingleToneGeometry(StudioWidgetComponentIds::XAxis, theXColor, inRenderContext,
                                     m_wideXAxis);
            RenderSingleToneGeometry(StudioWidgetComponentIds::YAxis, theYColor, inRenderContext,
                                     m_wideYAxis);
            RenderSingleToneGeometry(StudioWidgetComponentIds::ZAxis, theZColor, inRenderContext,
                                     m_wideZAxis);
        }
        m_Highlight = StudioWidgetComponentIds::NoId;
    }

    void RenderPick(const QT3DSMat44 &inProjPremult, NVRenderContext &inRenderContext,
                            QSize /*inWinDimensions*/) override
    {
        if (m_XAxis && m_PickShader) {
            inRenderContext.SetDepthWriteEnabled(true);
            inRenderContext.Clear(qt3ds::render::NVRenderClearValues::Depth);
            inRenderContext.SetDepthTestEnabled(true);
            inRenderContext.SetBlendingEnabled(false);
            inRenderContext.SetDepthTestEnabled(true);
            inRenderContext.SetDepthWriteEnabled(true);
            inRenderContext.SetCullingEnabled(false);
            inRenderContext.SetActiveShader(m_PickShader);
            // The projection premultiplication step moves the viewport around till
            // it is centered over the mouse and scales everything *post* rendering (to keep
            // appropriate aspect).
            QT3DSMat44 theMVP = inProjPremult * m_PureProjection * m_TranslationScale;
            m_PickShader->SetPropertyValue("model_view_projection", theMVP);

            RenderPickBuffer(StudioWidgetComponentIds::XAxis, m_XAxis, inRenderContext);
            RenderPickBuffer(StudioWidgetComponentIds::YAxis, m_YAxis, inRenderContext);
            RenderPickBuffer(StudioWidgetComponentIds::ZAxis, m_ZAxis, inRenderContext);
            RenderPickBuffer(StudioWidgetComponentIds::XPlane, m_XPlane, inRenderContext);
            RenderPickBuffer(StudioWidgetComponentIds::YPlane, m_YPlane, inRenderContext);
            RenderPickBuffer(StudioWidgetComponentIds::ZPlane, m_ZPlane, inRenderContext);
        }
    }

    void setAsAxisHelper(bool isAxisHelper) override
    {
        m_isAxisHelper = isAxisHelper;
    }
};
}

IStudioWidget &IStudioWidget::CreateTranslationWidget(NVAllocatorCallback &inAlloc)
{
    return *QT3DS_NEW(inAlloc, STranslationWidget)(inAlloc);
}