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 "Decal.h"
21 :
22 : #include "graphics/RenderableObject.h"
23 : #include "graphics/Terrain.h"
24 : #include "maths/BoundingBoxAligned.h"
25 : #include "maths/MathUtil.h"
26 : #include "maths/Matrix3D.h"
27 : #include "maths/Vector3D.h"
28 : #include "ps/CStrIntern.h"
29 : #include "ps/CStrInternStatic.h"
30 :
31 : #include <algorithm>
32 : #include <cmath>
33 : #include <memory>
34 :
35 1 : std::unique_ptr<CModelAbstract> CModelDecal::Clone() const
36 : {
37 1 : return std::make_unique<CModelDecal>(m_Terrain, m_Decal);
38 : }
39 :
40 0 : void CModelDecal::CalcVertexExtents(ssize_t& i0, ssize_t& j0, ssize_t& i1, ssize_t& j1)
41 : {
42 0 : CVector3D corner0(m_Decal.m_OffsetX + m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ + m_Decal.m_SizeZ/2);
43 0 : CVector3D corner1(m_Decal.m_OffsetX + m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ - m_Decal.m_SizeZ/2);
44 0 : CVector3D corner2(m_Decal.m_OffsetX - m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ - m_Decal.m_SizeZ/2);
45 0 : CVector3D corner3(m_Decal.m_OffsetX - m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ + m_Decal.m_SizeZ/2);
46 :
47 0 : corner0 = GetTransform().Transform(corner0);
48 0 : corner1 = GetTransform().Transform(corner1);
49 0 : corner2 = GetTransform().Transform(corner2);
50 0 : corner3 = GetTransform().Transform(corner3);
51 :
52 0 : i0 = floor(std::min(std::min(corner0.X, corner1.X), std::min(corner2.X, corner3.X)) / TERRAIN_TILE_SIZE);
53 0 : j0 = floor(std::min(std::min(corner0.Z, corner1.Z), std::min(corner2.Z, corner3.Z)) / TERRAIN_TILE_SIZE);
54 0 : i1 = ceil(std::max(std::max(corner0.X, corner1.X), std::max(corner2.X, corner3.X)) / TERRAIN_TILE_SIZE);
55 0 : j1 = ceil(std::max(std::max(corner0.Z, corner1.Z), std::max(corner2.Z, corner3.Z)) / TERRAIN_TILE_SIZE);
56 :
57 0 : i0 = Clamp<ssize_t>(i0, 0, m_Terrain->GetVerticesPerSide() - 1);
58 0 : j0 = Clamp<ssize_t>(j0, 0, m_Terrain->GetVerticesPerSide() - 1);
59 0 : i1 = Clamp<ssize_t>(i1, 0, m_Terrain->GetVerticesPerSide() - 1);
60 0 : j1 = Clamp<ssize_t>(j1, 0, m_Terrain->GetVerticesPerSide() - 1);
61 0 : }
62 :
63 0 : void CModelDecal::CalcBounds()
64 : {
65 0 : ssize_t i0, j0, i1, j1;
66 0 : CalcVertexExtents(i0, j0, i1, j1);
67 0 : m_WorldBounds = m_Terrain->GetVertexesBound(i0, j0, i1, j1);
68 0 : }
69 :
70 0 : void CModelDecal::SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
71 : {
72 : // Check if there's no intersection between the dirty range and this decal
73 0 : ssize_t bi0, bj0, bi1, bj1;
74 0 : CalcVertexExtents(bi0, bj0, bi1, bj1);
75 0 : if (bi1 < i0 || bi0 > i1 || bj1 < j0 || bj0 > j1)
76 0 : return;
77 :
78 0 : SetDirty(RENDERDATA_UPDATE_VERTICES);
79 : }
80 :
81 2 : void CModelDecal::InvalidatePosition()
82 : {
83 2 : m_PositionValid = false;
84 2 : }
85 :
86 0 : void CModelDecal::ValidatePosition()
87 : {
88 0 : if (m_PositionValid)
89 : {
90 0 : ENSURE(!m_Parent || m_Parent->m_PositionValid);
91 0 : return;
92 : }
93 :
94 0 : if (m_Parent && !m_Parent->m_PositionValid)
95 : {
96 : // Make sure we don't base our calculations on
97 : // a parent animation state that is out of date.
98 0 : m_Parent->ValidatePosition();
99 :
100 : // Parent will recursively call our validation.
101 0 : ENSURE(m_PositionValid);
102 0 : return;
103 : }
104 :
105 0 : m_PositionValid = true;
106 : }
107 :
108 2 : void CModelDecal::SetTransform(const CMatrix3D& transform)
109 : {
110 : // Since decals are assumed to be horizontal and projected downwards
111 : // onto the terrain, use just the Y-axis rotation and the translation
112 2 : CMatrix3D newTransform;
113 2 : newTransform.SetYRotation(transform.GetYRotation() + m_Decal.m_Angle);
114 2 : newTransform.Translate(transform.GetTranslation());
115 :
116 2 : CRenderableObject::SetTransform(newTransform);
117 2 : InvalidatePosition();
118 2 : }
119 :
120 1 : void CModelDecal::RemoveShadowsReceive()
121 : {
122 : // We need to add the define only to passes where shadows might appear.
123 4 : for (const auto pass : {CMaterial::Pass::MAIN, CMaterial::Pass::REFLECTIONS, CMaterial::Pass::REFRACTIONS})
124 3 : m_Decal.m_Material.AddShaderDefine(pass, str_DISABLE_RECEIVE_SHADOWS, str_1);
125 1 : }
|