Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ModelAbstract.h" 21 : 22 : #include "lib/debug.h" 23 : #include "maths/Vector3D.h" 24 : #include "ps/CLogger.h" 25 : 26 : #include <algorithm> 27 : 28 0 : const CBoundingBoxOriented& CModelAbstract::GetSelectionBox() 29 : { 30 0 : if (!m_SelectionBoxValid) 31 : { 32 0 : CalcSelectionBox(); 33 0 : m_SelectionBoxValid = true; 34 : } 35 0 : return m_SelectionBox; 36 : } 37 : 38 0 : void CModelAbstract::CalcSelectionBox() 39 : { 40 0 : if (m_CustomSelectionShape) 41 : { 42 : // custom shape 43 0 : switch(m_CustomSelectionShape->m_Type) 44 : { 45 0 : case CustomSelectionShape::BOX: 46 0 : { 47 : // create object-space bounds according to the information in the descriptor, and transform them to world-space. 48 : // the box is centered on the X and Z axes, but extends from 0 to its height on the Y axis. 49 0 : const float width = m_CustomSelectionShape->m_Size0; 50 0 : const float depth = m_CustomSelectionShape->m_Size1; 51 0 : const float height = m_CustomSelectionShape->m_Height; 52 : 53 0 : CBoundingBoxAligned bounds; 54 0 : bounds += CVector3D(-width/2.f, 0, -depth/2.f); 55 0 : bounds += CVector3D( width/2.f, height, depth/2.f); 56 : 57 0 : bounds.Transform(GetTransform(), m_SelectionBox); 58 : } 59 0 : break; 60 0 : case CustomSelectionShape::CYLINDER: 61 0 : { 62 : // TODO: unimplemented 63 0 : m_SelectionBox.SetEmpty(); 64 0 : LOGWARNING("[ModelAbstract] TODO: Cylinder selection boxes are not yet implemented. Use BOX or BOUNDS selection shapes instead."); 65 : } 66 0 : break; 67 0 : default: 68 0 : { 69 0 : m_SelectionBox.SetEmpty(); 70 : // LOGWARNING("[ModelAbstract] Unrecognized selection shape type: {}", 71 : // m_CustomSelectionShape->m_Type); 72 0 : debug_warn("[ModelAbstract] Unrecognized selection shape type"); 73 : } 74 : break; 75 : } 76 : } 77 : else 78 : { 79 : // standard method 80 : 81 : // Get the object-space bounds that should be used to construct this model (and its children)'s selection box 82 0 : CBoundingBoxAligned objBounds = GetObjectSelectionBoundsRec(); 83 0 : if (objBounds.IsEmpty()) 84 : { 85 0 : m_SelectionBox.SetEmpty(); // model does not wish to participate in selection 86 0 : return; 87 : } 88 : 89 : // Prevent the bounding box from extending through the terrain; clip the lower plane at Y=0 in object space. 90 0 : if (objBounds[1].Y > 0.f) // should always be the case, unless the models are defined really weirdly 91 0 : objBounds[0].Y = std::max(0.f, objBounds[0].Y); 92 : 93 : // transform object-space axis-aligned bounds to world-space arbitrary-aligned box 94 0 : objBounds.Transform(GetTransform(), m_SelectionBox); 95 : } 96 : 97 : }