1 /* Copyright (C) 2013 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/Terrain.h"
23 #include "maths/MathUtil.h"
24 
Clone() const25 CModelAbstract* CModelDecal::Clone() const
26 {
27 	CModelDecal* clone = new CModelDecal(m_Terrain, m_Decal);
28 	return clone;
29 }
30 
CalcVertexExtents(ssize_t & i0,ssize_t & j0,ssize_t & i1,ssize_t & j1)31 void CModelDecal::CalcVertexExtents(ssize_t& i0, ssize_t& j0, ssize_t& i1, ssize_t& j1)
32 {
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);
37 
38 	corner0 = GetTransform().Transform(corner0);
39 	corner1 = GetTransform().Transform(corner1);
40 	corner2 = GetTransform().Transform(corner2);
41 	corner3 = GetTransform().Transform(corner3);
42 
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);
47 
48 	i0 = clamp(i0, (ssize_t)0, m_Terrain->GetVerticesPerSide()-1);
49 	j0 = clamp(j0, (ssize_t)0, m_Terrain->GetVerticesPerSide()-1);
50 	i1 = clamp(i1, (ssize_t)0, m_Terrain->GetVerticesPerSide()-1);
51 	j1 = clamp(j1, (ssize_t)0, m_Terrain->GetVerticesPerSide()-1);
52 }
53 
CalcBounds()54 void CModelDecal::CalcBounds()
55 {
56 	ssize_t i0, j0, i1, j1;
57 	CalcVertexExtents(i0, j0, i1, j1);
58 	m_WorldBounds = m_Terrain->GetVertexesBound(i0, j0, i1, j1);
59 }
60 
SetTerrainDirty(ssize_t i0,ssize_t j0,ssize_t i1,ssize_t j1)61 void CModelDecal::SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
62 {
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;
68 
69 	SetDirty(RENDERDATA_UPDATE_VERTICES);
70 }
71 
InvalidatePosition()72 void CModelDecal::InvalidatePosition()
73 {
74 	m_PositionValid = false;
75 }
76 
ValidatePosition()77 void CModelDecal::ValidatePosition()
78 {
79 	if (m_PositionValid)
80 	{
81 		ENSURE(!m_Parent || m_Parent->m_PositionValid);
82 		return;
83 	}
84 
85 	if (m_Parent && !m_Parent->m_PositionValid)
86 	{
87 		// Make sure we don't base our calculations on
88 		// a parent animation state that is out of date.
89 		m_Parent->ValidatePosition();
90 
91 		// Parent will recursively call our validation.
92 		ENSURE(m_PositionValid);
93 		return;
94 	}
95 
96 	m_PositionValid = true;
97 }
98 
SetTransform(const CMatrix3D & transform)99 void CModelDecal::SetTransform(const CMatrix3D& transform)
100 {
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());
106 
107 	CRenderableObject::SetTransform(newTransform);
108 	InvalidatePosition();
109 }
110 
RemoveShadows()111 void CModelDecal::RemoveShadows()
112 {
113 	m_Decal.m_Material.AddShaderDefine(str_DISABLE_RECEIVE_SHADOWS, str_1);
114 	m_Decal.m_Material.RecomputeCombinedShaderDefines();
115 }
116