File: objectmarker.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (307 lines) | stat: -rw-r--r-- 10,387 bytes parent folder | download
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
#include <unordered_set>

#include <QFile>

#include <osg/ClipPlane>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osgUtil/CullVisitor>

#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/visitor.hpp>

#include "../../model/prefs/state.hpp"
#include "objectmarker.hpp"
#include "worldspacewidget.hpp"

namespace
{
    class FindMaterialVisitor : public osg::NodeVisitor
    {
    public:
        FindMaterialVisitor(CSVRender::NodeMap& map)
            : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
            , mMap(map)
        {
        }

        void apply(osg::Geometry& node) override
        {
            osg::StateSet* state = node.getStateSet();
            if (state->getAttribute(osg::StateAttribute::MATERIAL))
                mMap.emplace(node.getName(), &node);

            traverse(node);
        }

    private:
        CSVRender::NodeMap& mMap;
    };

    class ToCamera : public SceneUtil::NodeCallback<ToCamera, osg::Node*, osgUtil::CullVisitor*>
    {
    public:
        ToCamera(osg::ref_ptr<osg::ClipPlane> clipPlane)
            : mClipPlane(std::move(clipPlane))
        {
        }
        void operator()(osg::Node* node, osgUtil::CullVisitor* cv)
        {
            osg::Vec3f normal = cv->getEyePoint();
            mClipPlane->setClipPlane(normal.x(), normal.y(), normal.z(), 0);
            traverse(node, cv);
        }

    private:
        osg::ref_ptr<osg::ClipPlane> mClipPlane;
    };

    auto addTagToActiveMarkerNodes = [](CSVRender::NodeMap& mMarkerNodes, CSVRender::Object* object,
                                         std::initializer_list<std::string> suffixes) {
        for (const auto& markerSuffix : suffixes)
        {
            for (char axis = 'X'; axis <= 'Z'; ++axis)
                mMarkerNodes[axis + markerSuffix]->setUserData(new CSVRender::ObjectMarkerTag(object, axis - 'X'));
        }
    };
}

namespace CSVRender
{
    ObjectMarkerTag::ObjectMarkerTag(Object* object, int axis)
        : ObjectTag(object)
        , mAxis(axis)
    {
    }

    ObjectMarker::ObjectMarker(WorldspaceWidget* worldspaceWidget, Resource::ResourceSystem* resourceSystem)
        : mWorldspaceWidget(worldspaceWidget)
        , mResourceSystem(resourceSystem)
        , mMarkerScale(CSMPrefs::get()["Rendering"]["object-marker-scale"].toDouble())
        , mSubMode(Object::Mode_None)
    {
        mBaseNode = new osg::PositionAttitudeTransform;
        mBaseNode->setNodeMask(Mask_Reference);
        mBaseNode->setScale(osg::Vec3f(mMarkerScale, mMarkerScale, mMarkerScale));

        mRootNode = new osg::PositionAttitudeTransform;
        mRootNode->addChild(mBaseNode);
        worldspaceWidget->setSelectionMarkerRoot(mRootNode);

        QFile file(":render/selection-marker");

        if (!file.open(QIODevice::ReadOnly))
            throw std::runtime_error("Failed to open selection marker file");

        auto markerData = file.readAll();

        mResourceSystem->getSceneManager()->loadSelectionMarker(mBaseNode, markerData.data(), markerData.size());

        osg::ref_ptr<osg::StateSet> baseNodeState = mBaseNode->getOrCreateStateSet();
        baseNodeState->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
        baseNodeState->setRenderBinDetails(1000, "RenderBin");

        FindMaterialVisitor matMapper(mMarkerNodes);

        mBaseNode->accept(matMapper);

        for (const auto& [name, node] : mMarkerNodes)
        {
            osg::StateSet* state = node->getStateSet();
            osg::Material* mat = static_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
            osg::Vec4f emis = mat->getEmission(osg::Material::FRONT_AND_BACK);
            mat->setEmission(osg::Material::FRONT_AND_BACK, emis / 4);
            mOriginalColors.emplace(name, emis);
        }

        SceneUtil::NodeMap sceneNodes;
        SceneUtil::NodeMapVisitor nodeMapper(sceneNodes);
        mBaseNode->accept(nodeMapper);

        mMarkerNodes.insert(sceneNodes.begin(), sceneNodes.end());

        osg::ref_ptr<osg::Node> rotateMarkers = mMarkerNodes["rotateMarkers"];
        osg::ClipPlane* clip = new osg::ClipPlane(0);
        rotateMarkers->setCullCallback(new ToCamera(clip));
        rotateMarkers->getStateSet()->setAttributeAndModes(clip, osg::StateAttribute::ON);
    }

    void ObjectMarker::toggleVisibility()
    {
        bool isVisible = mBaseNode->getNodeMask() == Mask_Reference;
        mBaseNode->setNodeMask(isVisible ? Mask_Hidden : Mask_Reference);
    }

    void ObjectMarker::updateScale(const float scale)
    {
        mMarkerScale = scale;
        mBaseNode->setScale(osg::Vec3f(scale, scale, scale));
    }

    void ObjectMarker::setSubMode(const int subMode)
    {
        if (subMode == mSubMode)
            return;
        mSubMode = subMode;
        resetMarkerHighlight();
        updateSelectionMarker();
    }

    bool ObjectMarker::hitBehindMarker(const osg::Vec3d& hitPos, osg::ref_ptr<osg::Camera> camera)
    {
        if (mSubMode != Object::Mode_Rotate)
            return false;

        osg::Vec3d center, eye, forwardVector, _;
        std::vector<osg::Node*> rotMark = mMarkerNodes["rotateMarkers"]->getParentalNodePaths()[0];
        const osg::Vec3f markerPos = osg::computeLocalToWorld(rotMark).getTrans();

        camera->getViewMatrixAsLookAt(eye, center, _);
        forwardVector = center - eye;
        forwardVector.normalize();

        return (hitPos - markerPos) * forwardVector > 0;
    }

    bool ObjectMarker::attachMarker(const std::string& refId)
    {
        const auto& object = mWorldspaceWidget->getObjectByReferenceId(refId);

        if (!object)
            removeFromSelectionHistory(refId);

        if (!object || !object->getSelected())
            return false;

        if (!object->getRootNode()->addChild(mRootNode))
            throw std::runtime_error("Failed to add marker to object");

        std::string parentMarkerNode;

        switch (mSubMode)
        {
            case (Object::Mode_Rotate):
                parentMarkerNode = "rotateMarkers";
                addTagToActiveMarkerNodes(mMarkerNodes, object, { "_Axis_Rot" });
                break;
            case (Object::Mode_Scale):
                parentMarkerNode = "scaleMarkers";
                addTagToActiveMarkerNodes(mMarkerNodes, object, { "_Axis_Scale", "_Wall_Scale" });
                break;
            case (Object::Mode_Move):
            default:
                parentMarkerNode = "moveMarkers";
                addTagToActiveMarkerNodes(mMarkerNodes, object, { "_Axis", "_Wall" });
                break;
        }

        mMarkerNodes[parentMarkerNode]->asGroup()->setNodeMask(Mask_Reference);

        return true;
    }

    void ObjectMarker::detachMarker()
    {
        for (std::size_t index = mRootNode->getNumParents(); index > 0;)
            mRootNode->getParent(--index)->removeChild(mRootNode);

        osg::ref_ptr<osg::Group> widgetRoot = mMarkerNodes["unitArrows"]->asGroup();
        for (std::size_t index = widgetRoot->getNumChildren(); index > 0;)
            widgetRoot->getChild(--index)->setNodeMask(Mask_Hidden);
    }

    void ObjectMarker::addToSelectionHistory(const std::string& refId, bool update)
    {
        auto foundObject = std::find_if(mSelectionHistory.begin(), mSelectionHistory.end(),
            [&refId](const std::string& objId) { return objId == refId; });

        if (foundObject == mSelectionHistory.end())
            mSelectionHistory.push_back(refId);
        else
            std::rotate(foundObject, foundObject + 1, mSelectionHistory.end());

        if (update)
            updateSelectionMarker(refId);
    }

    void ObjectMarker::removeFromSelectionHistory(const std::string& refId)
    {
        mSelectionHistory.erase(std::remove_if(mSelectionHistory.begin(), mSelectionHistory.end(),
                                    [&refId](const std::string& objId) { return objId == refId; }),
            mSelectionHistory.end());
    }

    void ObjectMarker::updateSelectionMarker(const std::string& refId)
    {
        if (mSelectionHistory.empty())
            return;

        detachMarker();

        if (refId.empty())
        {
            for (std::size_t index = mSelectionHistory.size(); index > 0;)
                if (attachMarker(mSelectionHistory[--index]))
                    break;
        }
        else
            attachMarker(refId);
    }

    void ObjectMarker::resetMarkerHighlight()
    {
        if (mLastHighlightedNodes.empty())
            return;

        for (const auto& [nodeName, mat] : mLastHighlightedNodes)
            mat->setEmission(osg::Material::FRONT_AND_BACK, mat->getEmission(osg::Material::FRONT_AND_BACK) / 4);

        mLastHighlightedNodes.clear();
        mLastHitNode.clear();
    }

    void ObjectMarker::updateMarkerHighlight(const std::string_view hitNode, const int axis)
    {
        if (hitNode == mLastHitNode)
            return;

        resetMarkerHighlight();

        std::string colorName;

        switch (axis)
        {
            case Object::Axis_X:
                colorName = "red";
                break;
            case Object::Axis_Y:
                colorName = "green";
                break;
            case Object::Axis_Z:
                colorName = "blue";
                break;
            default:
                throw std::runtime_error("Invalid axis for highlighting: " + std::to_string(axis));
        }

        std::vector<std::string> targetMaterials = { colorName + "-material" };

        if (mSubMode != Object::Mode_Rotate)
            targetMaterials.emplace_back(colorName + "_alpha-material");

        for (const auto& materialNodeName : targetMaterials)
        {
            osg::ref_ptr<osg::Node> matNode = mMarkerNodes[materialNodeName];
            osg::StateSet* state = matNode->getStateSet();
            osg::StateAttribute* matAttr = state->getAttribute(osg::StateAttribute::MATERIAL);

            osg::Material* mat = static_cast<osg::Material*>(matAttr);
            mat->setEmission(osg::Material::FRONT_AND_BACK, mOriginalColors[materialNodeName]);

            mLastHighlightedNodes.emplace(std::make_pair(matNode->getName(), mat));
        }

        mLastHitNode = hitNode;
    }
}