From 12137da42056da7d67aec4b98cc8825e98cf457c Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 23 Jan 2022 14:31:41 +0100 Subject: [PATCH] #5584: Start migrating TranslateManipulator --- libs/selection/BestPoint.h | 4 +- radiantcore/selection/Renderables.h | 11 -- .../selection/manipulators/Renderables.h | 12 ++ .../manipulators/TranslateManipulator.cpp | 112 ++++++++++++++---- .../manipulators/TranslateManipulator.h | 14 ++- 5 files changed, 113 insertions(+), 40 deletions(-) diff --git a/libs/selection/BestPoint.h b/libs/selection/BestPoint.h index 961318ffbe..bc0ab3c7a6 100644 --- a/libs/selection/BestPoint.h +++ b/libs/selection/BestPoint.h @@ -474,10 +474,10 @@ inline void LineLoop_BestPoint(const Matrix4& local2view, const Vertex3f* vertic } } -inline void Line_BestPoint(const Matrix4& local2view, const VertexCb vertices[2], SelectionIntersection& best) +inline void Line_BestPoint(const Matrix4& local2view, const Vertex3f vertices[2], SelectionIntersection& best) { Vector4 clipped[2]; - const std::size_t count = clipLine(local2view, vertices[0].vertex, vertices[1].vertex, clipped); + const std::size_t count = clipLine(local2view, vertices[0], vertices[1], clipped); BestPoint(count, clipped, best, eClipCullNone); } diff --git a/radiantcore/selection/Renderables.h b/radiantcore/selection/Renderables.h index 05224ed2c4..78e871a6c9 100644 --- a/radiantcore/selection/Renderables.h +++ b/radiantcore/selection/Renderables.h @@ -7,17 +7,6 @@ * the manipulators of the selected items */ -// Helper class for rendering an arrow (only the line part) -class RenderableArrowLine : - public RenderablePointVector -{ -public: - // Constructor instantiates a renderable array of size 2 - RenderableArrowLine() : - RenderablePointVector(GL_LINES, 2) - {} -}; - // Helper class for rendering an arrow (only the head part) class RenderableArrowHead : public OpenGLRenderable diff --git a/radiantcore/selection/manipulators/Renderables.h b/radiantcore/selection/manipulators/Renderables.h index 7dcbfb9049..fa269ef0df 100644 --- a/radiantcore/selection/manipulators/Renderables.h +++ b/radiantcore/selection/manipulators/Renderables.h @@ -253,6 +253,18 @@ class RenderableCircle : } }; +class RenderableArrowLine : + public RenderableLineStrip +{ +public: + RenderableArrowLine(const Vector3& direction, const Matrix4& localToWorld) : + RenderableLineStrip(2, localToWorld) + { + _rawPoints[0] = Vector3(0, 0, 0); + _rawPoints[1] = direction; + } +}; + class RenderablePoint : public render::RenderableGeometry { diff --git a/radiantcore/selection/manipulators/TranslateManipulator.cpp b/radiantcore/selection/manipulators/TranslateManipulator.cpp index c6adecaa0e..8636c9aef8 100644 --- a/radiantcore/selection/manipulators/TranslateManipulator.cpp +++ b/radiantcore/selection/manipulators/TranslateManipulator.cpp @@ -11,46 +11,106 @@ namespace selection const std::string RKEY_TRANSLATE_CONSTRAINED = "user/ui/xyview/translateConstrained"; -// Constructor TranslateManipulator::TranslateManipulator(ManipulationPivot& pivot, std::size_t segments, float length) : _pivot(pivot), _translator(std::bind(&ManipulationPivot::applyTranslation, &_pivot, std::placeholders::_1)), _translateFree(_translator), _translateAxis(_translator), + _arrowX({ length,0,0 }, _pivot2World._worldSpace), + _arrowY({ 0,length,0 }, _pivot2World._worldSpace), + _arrowZ({ 0,0,length }, _pivot2World._worldSpace), _arrowHeadX(3 * 2 * (segments << 3)), _arrowHeadY(3 * 2 * (segments << 3)), _arrowHeadZ(3 * 2 * (segments << 3)) { - draw_arrowline(length, &_arrowX.front(), 0); draw_arrowhead(segments, length, &_arrowHeadX._vertices.front(), TripleRemapXYZ(), TripleRemapXYZ()); - draw_arrowline(length, &_arrowY.front(), 1); draw_arrowhead(segments, length, &_arrowHeadY._vertices.front(), TripleRemapYZX(), TripleRemapYZX()); - draw_arrowline(length, &_arrowZ.front(), 2); draw_arrowhead(segments, length, &_arrowHeadZ._vertices.front(), TripleRemapZXY(), TripleRemapZXY()); draw_quad(16, &_quadScreen.front()); } -void TranslateManipulator::UpdateColours() { +void TranslateManipulator::updateColours() +{ _arrowX.setColour(colourSelected(COLOUR_X(), _selectableX.isSelected())); _arrowHeadX.setColour(colourSelected(COLOUR_X(), _selectableX.isSelected())); _arrowY.setColour(colourSelected(COLOUR_Y(), _selectableY.isSelected())); _arrowHeadY.setColour(colourSelected(COLOUR_Y(), _selectableY.isSelected())); _arrowZ.setColour(colourSelected(COLOUR_Z(), _selectableZ.isSelected())); _arrowHeadZ.setColour(colourSelected(COLOUR_Z(), _selectableZ.isSelected())); - _quadScreen.setColour(colourSelected(ManipulatorBase::COLOUR_SCREEN(), _selectableScreen.isSelected())); + _quadScreen.setColour(colourSelected(COLOUR_SCREEN(), _selectableScreen.isSelected())); +} + +bool TranslateManipulator::axisIsVisible(const Vector3& axis) const +{ + return fabs(_pivot2World._axisScreen.dot(axis)) < 0.95; +} + +void TranslateManipulator::onPreRender(const RenderSystemPtr& renderSystem, const VolumeTest& volume) +{ + if (!renderSystem) + { + clearRenderables(); + return; + } + + if (!_lineShader) + { + _lineShader = renderSystem->capture("$WIRE_OVERLAY"); + } + + _pivot2World.update(_pivot.getMatrix4(), volume.GetModelview(), volume.GetProjection(), volume.GetViewport()); + + updateColours(); + + auto x = _pivot2World._worldSpace.xCol3().getNormalised(); + auto y = _pivot2World._worldSpace.yCol3().getNormalised(); + auto z = _pivot2World._worldSpace.zCol3().getNormalised(); + + if (axisIsVisible(x)) + { + _arrowX.update(_lineShader); + } + else + { + _arrowX.clear(); + } + + if (axisIsVisible(y)) + { + _arrowY.update(_lineShader); + } + else + { + _arrowY.clear(); + } + + if (axisIsVisible(z)) + { + _arrowZ.update(_lineShader); + } + else + { + _arrowZ.clear(); + } } -bool TranslateManipulator::manipulator_show_axis(const Pivot2World& pivot, const Vector3& axis) { - return fabs(pivot._axisScreen.dot(axis)) < 0.95; +void TranslateManipulator::clearRenderables() +{ + _arrowX.clear(); + _arrowY.clear(); + _arrowZ.clear(); + + _lineShader.reset(); } void TranslateManipulator::render(IRenderableCollector& collector, const VolumeTest& volume) { +#if 0 _pivot2World.update(_pivot.getMatrix4(), volume.GetModelview(), volume.GetProjection(), volume.GetViewport()); // temp hack - UpdateColours(); + updateColours(); Vector3 x = _pivot2World._worldSpace.xCol3().getNormalised(); bool show_x = manipulator_show_axis(_pivot2World, x); @@ -88,6 +148,7 @@ void TranslateManipulator::render(IRenderableCollector& collector, const VolumeT { collector.addRenderable(*_stateFill, _arrowHeadZ, _pivot2World._worldSpace); } +#endif } void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2world) @@ -97,14 +158,13 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2 SelectionPool selector; - Vector3 x = _pivot2World._worldSpace.xCol3().getNormalised(); - bool show_x = manipulator_show_axis(_pivot2World, x); - - Vector3 y = _pivot2World._worldSpace.yCol3().getNormalised(); - bool show_y = manipulator_show_axis(_pivot2World, y); + auto x = _pivot2World._worldSpace.xCol3().getNormalised(); + auto y = _pivot2World._worldSpace.yCol3().getNormalised(); + auto z = _pivot2World._worldSpace.zCol3().getNormalised(); - Vector3 z = _pivot2World._worldSpace.zCol3().getNormalised(); - bool show_z = manipulator_show_axis(_pivot2World, z); + bool show_x = axisIsVisible(x); + bool show_y = axisIsVisible(y); + bool show_z = axisIsVisible(z); { Matrix4 local2view(test.getVolume().GetViewProjection().getMultipliedBy(_pivot2World._viewpointSpace)); @@ -126,7 +186,7 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2 if(show_x) { SelectionIntersection best; - Line_BestPoint(local2view, &_arrowX.front(), best); + Line_BestPoint(local2view, &_arrowX.getRawPoints().front(), best); Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadX._vertices.front(), &*(_arrowHeadX._vertices.end()-1)+1, best); selector.addSelectable(best, &_selectableX); } @@ -134,7 +194,7 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2 if(show_y) { SelectionIntersection best; - Line_BestPoint(local2view, &_arrowY.front(), best); + Line_BestPoint(local2view, &_arrowY.getRawPoints().front(), best); Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadY._vertices.front(), &*(_arrowHeadY._vertices.end()-1)+1, best); selector.addSelectable(best, &_selectableY); } @@ -142,17 +202,20 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2 if(show_z) { SelectionIntersection best; - Line_BestPoint(local2view, &_arrowZ.front(), best); + Line_BestPoint(local2view, &_arrowZ.getRawPoints().front(), best); Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadZ._vertices.front(), &*(_arrowHeadZ._vertices.end()-1)+1, best); selector.addSelectable(best, &_selectableZ); } } // greebo: If any of the above arrows could be selected, select the first in the SelectionPool - if(!selector.empty()) { - (*selector.begin()).second->setSelected(true); - } else { - ISelectable* selectable = NULL; + if(!selector.empty()) + { + selector.begin()->second->setSelected(true); + } + else + { + ISelectable* selectable = nullptr; if (registry::getValue(RKEY_TRANSLATE_CONSTRAINED)) { // None of the shown arrows (or quad) has been selected, select an axis based on the precedence @@ -187,7 +250,8 @@ void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2 } // If everything went ok, there is a selectable available, add it - if (selectable != NULL) { + if (selectable) + { selector.addSelectable(SelectionIntersection(0,0), selectable); selectable->setSelected(true); } diff --git a/radiantcore/selection/manipulators/TranslateManipulator.h b/radiantcore/selection/manipulators/TranslateManipulator.h index 08a4bac2ce..0465046dab 100644 --- a/radiantcore/selection/manipulators/TranslateManipulator.h +++ b/radiantcore/selection/manipulators/TranslateManipulator.h @@ -6,6 +6,7 @@ #include "ManipulatorComponents.h" #include "selection/BasicSelectable.h" #include "selection/ManipulationPivot.h" +#include "Renderables.h" namespace selection { @@ -34,6 +35,9 @@ class TranslateManipulator : selection::BasicSelectable _selectableZ; selection::BasicSelectable _selectableScreen; Pivot2World _pivot2World; + + ShaderPtr _lineShader; + public: static ShaderPtr _stateWire; static ShaderPtr _stateFill; @@ -46,15 +50,19 @@ class TranslateManipulator : return Translate; } - void UpdateColours(); - bool manipulator_show_axis(const Pivot2World& pivot, const Vector3& axis); - + void onPreRender(const RenderSystemPtr& renderSystem, const VolumeTest& volume) override; void render(IRenderableCollector& collector, const VolumeTest& volume) override; + void clearRenderables() override; + void testSelect(SelectionTest& test, const Matrix4& pivot2world) override; Component* getActiveComponent() override; void setSelected(bool select) override; bool isSelected() const override; + +private: + void updateColours(); + bool axisIsVisible(const Vector3& axis) const; }; }