summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Render/StudioTranslationWidget.cpp
blob: 0897a866bcb8740f51a75c37991e6704bf0b5e62 (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
/****************************************************************************
**
** 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_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_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::getSelectorLineWidth() * pixelRatio;
            QT3DSF32 axisStart = CStudioPreferences::getSelectorLineLength() / 3.0f * pixelRatio;
            QT3DSF32 axisLength = CStudioPreferences::getSelectorLineLength() * 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 = TBase::SetupMVP(inWidgetContext);
        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));

        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);
        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);
        }
    }
};
}

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