|
| 1 | +#include "DecalShooterTool.h" |
| 2 | + |
| 3 | +#include "i18n.h" |
| 4 | +#include "iundo.h" |
| 5 | +#include "imap.h" |
| 6 | +#include "ipatch.h" |
| 7 | +#include "iscenegraph.h" |
| 8 | +#include "iselection.h" |
| 9 | + |
| 10 | +#include "scenelib.h" |
| 11 | +#include "CameraMouseToolEvent.h" |
| 12 | +#include "FaceIntersectionFinder.h" |
| 13 | +#include "ui/decalshooter/DecalShooterPanel.h" |
| 14 | + |
| 15 | +#include <cmath> |
| 16 | + |
| 17 | +namespace ui |
| 18 | +{ |
| 19 | + |
| 20 | +// Static members |
| 21 | +std::string DecalShooterTool::_name = "DecalShooterTool"; |
| 22 | +std::string DecalShooterTool::_displayName; |
| 23 | + |
| 24 | +const std::string& DecalShooterTool::NAME() |
| 25 | +{ |
| 26 | + return _name; |
| 27 | +} |
| 28 | + |
| 29 | +const std::string& DecalShooterTool::getName() |
| 30 | +{ |
| 31 | + return _name; |
| 32 | +} |
| 33 | + |
| 34 | +const std::string& DecalShooterTool::getDisplayName() |
| 35 | +{ |
| 36 | + if (_displayName.empty()) |
| 37 | + { |
| 38 | + _displayName = _("Place Decal"); |
| 39 | + } |
| 40 | + return _displayName; |
| 41 | +} |
| 42 | + |
| 43 | +MouseTool::Result DecalShooterTool::onMouseDown(Event& ev) |
| 44 | +{ |
| 45 | + try |
| 46 | + { |
| 47 | + CameraMouseToolEvent& camEvent = dynamic_cast<CameraMouseToolEvent&>(ev); |
| 48 | + |
| 49 | + SelectionTestPtr selectionTest = camEvent.getView().createSelectionTestForPoint( |
| 50 | + camEvent.getDevicePosition() |
| 51 | + ); |
| 52 | + |
| 53 | + const Matrix4& viewProjection = selectionTest->getVolume().GetViewProjection(); |
| 54 | + |
| 55 | + FaceIntersectionFinder finder(*selectionTest, viewProjection); |
| 56 | + GlobalSceneGraph().root()->traverse(finder); |
| 57 | + |
| 58 | + const FaceIntersection& intersection = finder.getResult(); |
| 59 | + |
| 60 | + if (intersection.valid) |
| 61 | + { |
| 62 | + DecalShooterPanel* panel = DecalShooterPanel::getInstance(); |
| 63 | + |
| 64 | + double width = panel ? panel->getDecalWidth() : 128.0; |
| 65 | + double height = panel ? panel->getDecalHeight() : 128.0; |
| 66 | + double offset = panel ? panel->getDecalOffset() : 0.125; |
| 67 | + std::string material = panel ? panel->getDecalMaterial() : "textures/common/decal"; |
| 68 | + |
| 69 | + createDecalAtFace( |
| 70 | + intersection.point, |
| 71 | + intersection.normal, |
| 72 | + width, |
| 73 | + height, |
| 74 | + offset, |
| 75 | + material |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + return Result::Finished; |
| 80 | + } |
| 81 | + catch (std::bad_cast&) |
| 82 | + { |
| 83 | + } |
| 84 | + |
| 85 | + return Result::Ignored; |
| 86 | +} |
| 87 | + |
| 88 | +MouseTool::Result DecalShooterTool::onMouseMove(Event& ev) |
| 89 | +{ |
| 90 | + return Result::Ignored; |
| 91 | +} |
| 92 | + |
| 93 | +MouseTool::Result DecalShooterTool::onMouseUp(Event& ev) |
| 94 | +{ |
| 95 | + return Result::Finished; |
| 96 | +} |
| 97 | + |
| 98 | +void DecalShooterTool::createDecalAtFace( |
| 99 | + const Vector3& intersectionPoint, |
| 100 | + const Vector3& normal, |
| 101 | + double width, |
| 102 | + double height, |
| 103 | + double offset, |
| 104 | + const std::string& material) |
| 105 | +{ |
| 106 | + UndoableCommand cmd("PlaceDecal"); |
| 107 | + |
| 108 | + scene::INodePtr patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def3); |
| 109 | + |
| 110 | + if (!patchNode) |
| 111 | + { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + IPatchNodePtr patchNodePtr = std::dynamic_pointer_cast<IPatchNode>(patchNode); |
| 116 | + if (!patchNodePtr) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + IPatch& patch = patchNodePtr->getPatch(); |
| 122 | + |
| 123 | + patch.setDims(3, 3); |
| 124 | + patch.setFixedSubdivisions(true, Subdivisions(1, 1)); |
| 125 | + |
| 126 | + // Build orthonormal basis for the decal plane |
| 127 | + Vector3 up(0, 0, 1); |
| 128 | + if (std::abs(normal.dot(up)) > 0.9) |
| 129 | + { |
| 130 | + up = Vector3(0, 1, 0); |
| 131 | + } |
| 132 | + |
| 133 | + Vector3 tangent = normal.cross(up).getNormalised(); |
| 134 | + Vector3 bitangent = tangent.cross(normal).getNormalised(); |
| 135 | + |
| 136 | + Vector3 center = intersectionPoint + normal * offset; |
| 137 | + |
| 138 | + double halfWidth = width * 0.5; |
| 139 | + double halfHeight = height * 0.5; |
| 140 | + |
| 141 | + Vector3 points[4]; |
| 142 | + points[0] = center - tangent * halfWidth + bitangent * halfHeight; |
| 143 | + points[1] = center + tangent * halfWidth + bitangent * halfHeight; |
| 144 | + points[2] = center + tangent * halfWidth - bitangent * halfHeight; |
| 145 | + points[3] = center - tangent * halfWidth - bitangent * halfHeight; |
| 146 | + |
| 147 | + // Set up 3x3 control point grid |
| 148 | + patch.ctrlAt(0, 0).vertex = points[0]; |
| 149 | + patch.ctrlAt(2, 0).vertex = points[1]; |
| 150 | + patch.ctrlAt(1, 0).vertex = (patch.ctrlAt(0, 0).vertex + patch.ctrlAt(2, 0).vertex) / 2; |
| 151 | + |
| 152 | + patch.ctrlAt(0, 1).vertex = (points[0] + points[3]) / 2; |
| 153 | + patch.ctrlAt(2, 1).vertex = (points[1] + points[2]) / 2; |
| 154 | + patch.ctrlAt(1, 1).vertex = (patch.ctrlAt(0, 1).vertex + patch.ctrlAt(2, 1).vertex) / 2; |
| 155 | + |
| 156 | + patch.ctrlAt(0, 2).vertex = points[3]; |
| 157 | + patch.ctrlAt(2, 2).vertex = points[2]; |
| 158 | + patch.ctrlAt(1, 2).vertex = (patch.ctrlAt(0, 2).vertex + patch.ctrlAt(2, 2).vertex) / 2; |
| 159 | + |
| 160 | + patch.setShader(material); |
| 161 | + patch.fitTexture(1, 1); |
| 162 | + patch.controlPointsChanged(); |
| 163 | + |
| 164 | + scene::INodePtr worldspawn = GlobalMapModule().findOrInsertWorldspawn(); |
| 165 | + if (worldspawn) |
| 166 | + { |
| 167 | + worldspawn->addChildNode(patchNode); |
| 168 | + } |
| 169 | + |
| 170 | + GlobalSelectionSystem().setSelectedAll(false); |
| 171 | + Node_setSelected(patchNode, true); |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace ui |
0 commit comments