1 /* Copyright (C) 2022 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"
22 #include "graphics/Terrain.h"
23 #include "maths/MathUtil.h"
24 #include "ps/CStrInternStatic.h"
26 CModelAbstract
* CModelDecal::Clone() const
28 CModelDecal
* clone
= new CModelDecal(m_Terrain
, m_Decal
);
32 void CModelDecal::CalcVertexExtents(ssize_t
& i0
, ssize_t
& j0
, ssize_t
& i1
, ssize_t
& j1
)
34 CVector3D
corner0(m_Decal
.m_OffsetX
+ m_Decal
.m_SizeX
/2, 0, m_Decal
.m_OffsetZ
+ m_Decal
.m_SizeZ
/2);
35 CVector3D
corner1(m_Decal
.m_OffsetX
+ m_Decal
.m_SizeX
/2, 0, m_Decal
.m_OffsetZ
- m_Decal
.m_SizeZ
/2);
36 CVector3D
corner2(m_Decal
.m_OffsetX
- m_Decal
.m_SizeX
/2, 0, m_Decal
.m_OffsetZ
- m_Decal
.m_SizeZ
/2);
37 CVector3D
corner3(m_Decal
.m_OffsetX
- m_Decal
.m_SizeX
/2, 0, m_Decal
.m_OffsetZ
+ m_Decal
.m_SizeZ
/2);
39 corner0
= GetTransform().Transform(corner0
);
40 corner1
= GetTransform().Transform(corner1
);
41 corner2
= GetTransform().Transform(corner2
);
42 corner3
= GetTransform().Transform(corner3
);
44 i0
= floor(std::min(std::min(corner0
.X
, corner1
.X
), std::min(corner2
.X
, corner3
.X
)) / TERRAIN_TILE_SIZE
);
45 j0
= floor(std::min(std::min(corner0
.Z
, corner1
.Z
), std::min(corner2
.Z
, corner3
.Z
)) / TERRAIN_TILE_SIZE
);
46 i1
= ceil(std::max(std::max(corner0
.X
, corner1
.X
), std::max(corner2
.X
, corner3
.X
)) / TERRAIN_TILE_SIZE
);
47 j1
= ceil(std::max(std::max(corner0
.Z
, corner1
.Z
), std::max(corner2
.Z
, corner3
.Z
)) / TERRAIN_TILE_SIZE
);
49 i0
= Clamp
<ssize_t
>(i0
, 0, m_Terrain
->GetVerticesPerSide() - 1);
50 j0
= Clamp
<ssize_t
>(j0
, 0, m_Terrain
->GetVerticesPerSide() - 1);
51 i1
= Clamp
<ssize_t
>(i1
, 0, m_Terrain
->GetVerticesPerSide() - 1);
52 j1
= Clamp
<ssize_t
>(j1
, 0, m_Terrain
->GetVerticesPerSide() - 1);
55 void CModelDecal::CalcBounds()
57 ssize_t i0
, j0
, i1
, j1
;
58 CalcVertexExtents(i0
, j0
, i1
, j1
);
59 m_WorldBounds
= m_Terrain
->GetVertexesBound(i0
, j0
, i1
, j1
);
62 void CModelDecal::SetTerrainDirty(ssize_t i0
, ssize_t j0
, ssize_t i1
, ssize_t j1
)
64 // Check if there's no intersection between the dirty range and this decal
65 ssize_t bi0
, bj0
, bi1
, bj1
;
66 CalcVertexExtents(bi0
, bj0
, bi1
, bj1
);
67 if (bi1
< i0
|| bi0
> i1
|| bj1
< j0
|| bj0
> j1
)
70 SetDirty(RENDERDATA_UPDATE_VERTICES
);
73 void CModelDecal::InvalidatePosition()
75 m_PositionValid
= false;
78 void CModelDecal::ValidatePosition()
82 ENSURE(!m_Parent
|| m_Parent
->m_PositionValid
);
86 if (m_Parent
&& !m_Parent
->m_PositionValid
)
88 // Make sure we don't base our calculations on
89 // a parent animation state that is out of date.
90 m_Parent
->ValidatePosition();
92 // Parent will recursively call our validation.
93 ENSURE(m_PositionValid
);
97 m_PositionValid
= true;
100 void CModelDecal::SetTransform(const CMatrix3D
& transform
)
102 // Since decals are assumed to be horizontal and projected downwards
103 // onto the terrain, use just the Y-axis rotation and the translation
104 CMatrix3D newTransform
;
105 newTransform
.SetYRotation(transform
.GetYRotation() + m_Decal
.m_Angle
);
106 newTransform
.Translate(transform
.GetTranslation());
108 CRenderableObject::SetTransform(newTransform
);
109 InvalidatePosition();
112 void CModelDecal::RemoveShadows()
114 m_Decal
.m_Material
.AddShaderDefine(str_DISABLE_RECEIVE_SHADOWS
, str_1
);