Merge 'remotes/trunk'
[0ad.git] / source / graphics / Decal.cpp
blob4e32c2867b23f75e155245d13fdc83f968486bf3
1 /* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
20 #include "Decal.h"
22 #include "graphics/Terrain.h"
23 #include "maths/MathUtil.h"
24 #include "ps/CStrInternStatic.h"
26 std::unique_ptr<CModelAbstract> CModelDecal::Clone() const
28 return std::make_unique<CModelDecal>(m_Terrain, m_Decal);
31 void CModelDecal::CalcVertexExtents(ssize_t& i0, ssize_t& j0, ssize_t& i1, ssize_t& j1)
33 CVector3D corner0(m_Decal.m_OffsetX + m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ + m_Decal.m_SizeZ/2);
34 CVector3D corner1(m_Decal.m_OffsetX + m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ - m_Decal.m_SizeZ/2);
35 CVector3D corner2(m_Decal.m_OffsetX - m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ - m_Decal.m_SizeZ/2);
36 CVector3D corner3(m_Decal.m_OffsetX - m_Decal.m_SizeX/2, 0, m_Decal.m_OffsetZ + m_Decal.m_SizeZ/2);
38 corner0 = GetTransform().Transform(corner0);
39 corner1 = GetTransform().Transform(corner1);
40 corner2 = GetTransform().Transform(corner2);
41 corner3 = GetTransform().Transform(corner3);
43 i0 = floor(std::min(std::min(corner0.X, corner1.X), std::min(corner2.X, corner3.X)) / TERRAIN_TILE_SIZE);
44 j0 = floor(std::min(std::min(corner0.Z, corner1.Z), std::min(corner2.Z, corner3.Z)) / TERRAIN_TILE_SIZE);
45 i1 = ceil(std::max(std::max(corner0.X, corner1.X), std::max(corner2.X, corner3.X)) / TERRAIN_TILE_SIZE);
46 j1 = ceil(std::max(std::max(corner0.Z, corner1.Z), std::max(corner2.Z, corner3.Z)) / TERRAIN_TILE_SIZE);
48 i0 = Clamp<ssize_t>(i0, 0, m_Terrain->GetVerticesPerSide() - 1);
49 j0 = Clamp<ssize_t>(j0, 0, m_Terrain->GetVerticesPerSide() - 1);
50 i1 = Clamp<ssize_t>(i1, 0, m_Terrain->GetVerticesPerSide() - 1);
51 j1 = Clamp<ssize_t>(j1, 0, m_Terrain->GetVerticesPerSide() - 1);
54 void CModelDecal::CalcBounds()
56 ssize_t i0, j0, i1, j1;
57 CalcVertexExtents(i0, j0, i1, j1);
58 m_WorldBounds = m_Terrain->GetVertexesBound(i0, j0, i1, j1);
61 void CModelDecal::SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
63 // Check if there's no intersection between the dirty range and this decal
64 ssize_t bi0, bj0, bi1, bj1;
65 CalcVertexExtents(bi0, bj0, bi1, bj1);
66 if (bi1 < i0 || bi0 > i1 || bj1 < j0 || bj0 > j1)
67 return;
69 SetDirty(RENDERDATA_UPDATE_VERTICES);
72 void CModelDecal::InvalidatePosition()
74 m_PositionValid = false;
77 void CModelDecal::ValidatePosition()
79 if (m_PositionValid)
81 ENSURE(!m_Parent || m_Parent->m_PositionValid);
82 return;
85 if (m_Parent && !m_Parent->m_PositionValid)
87 // Make sure we don't base our calculations on
88 // a parent animation state that is out of date.
89 m_Parent->ValidatePosition();
91 // Parent will recursively call our validation.
92 ENSURE(m_PositionValid);
93 return;
96 m_PositionValid = true;
99 void CModelDecal::SetTransform(const CMatrix3D& transform)
101 // Since decals are assumed to be horizontal and projected downwards
102 // onto the terrain, use just the Y-axis rotation and the translation
103 CMatrix3D newTransform;
104 newTransform.SetYRotation(transform.GetYRotation() + m_Decal.m_Angle);
105 newTransform.Translate(transform.GetTranslation());
107 CRenderableObject::SetTransform(newTransform);
108 InvalidatePosition();
111 void CModelDecal::RemoveShadows()
113 m_Decal.m_Material.AddShaderDefine(str_DISABLE_RECEIVE_SHADOWS, str_1);