1 /* Copyright (C) 2018 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 /*
19  * CLightEnv, a class describing the current lights
20  */
21 
22 #ifndef INCLUDED_LIGHTENV
23 #define INCLUDED_LIGHTENV
24 
25 #include "graphics/Color.h"
26 #include "maths/MathUtil.h"
27 #include "maths/Vector3D.h"
28 
29 class CMapWriter;
30 class CMapReader;
31 
32 /**
33  * Class CLightEnv: description of a lighting environment - contains all the
34  * necessary parameters for representation of the lighting within a scenario
35  */
36 class CLightEnv
37 {
38 friend class CMapWriter;
39 friend class CMapReader;
40 friend class CXMLReader;
41 private:
42 	/**
43 	 * Height of sun above the horizon, in radians.
44 	 * For example, an elevation of M_PI/2 means the sun is straight up.
45 	 */
46 	float m_Elevation;
47 
48 	/**
49 	 * Direction of sun on the compass, in radians.
50 	 * For example, a rotation of zero means the sun is in the direction (0,0,-1)
51 	 * and a rotation of M_PI/2 means the sun is in the direction (1,0,0) (not taking
52 	 * elevation into account).
53 	 */
54 	float m_Rotation;
55 
56 	/**
57 	 * Vector corresponding to m_Elevation and m_Rotation.
58 	 * Updated by CalculateSunDirection.
59 	 */
60 	CVector3D m_SunDir;
61 
62 	/**
63 	 * A string that shaders use to determine what lighting model to implement.
64 	 * Current recognised values are "old" and "standard".
65 	 */
66 	std::string m_LightingModel;
67 
68 public:
69 	RGBColor m_SunColor;
70 	RGBColor m_TerrainAmbientColor;
71 	RGBColor m_UnitsAmbientColor;
72 	RGBColor m_FogColor;
73 
74 	float m_FogFactor;
75 	float m_FogMax;
76 
77 	float m_Brightness, m_Contrast, m_Saturation, m_Bloom;
78 
79 public:
80 	CLightEnv();
81 
GetElevation()82 	float GetElevation() const { return m_Elevation; }
GetRotation()83 	float GetRotation() const { return m_Rotation; }
GetSunDir()84 	const CVector3D& GetSunDir() const { return m_SunDir; }
GetLightingModel()85 	const std::string& GetLightingModel() const { return m_LightingModel; }
86 
87 	void SetElevation(float f);
88 	void SetRotation(float f);
89 
SetLightingModel(const std::string & model)90 	void SetLightingModel(const std::string& model) { m_LightingModel = model; }
91 
92 	/**
93 	 * Calculate brightness of a point of a unit with the given normal vector,
94 	 * for rendering with CPU lighting.
95 	 * The resulting color contains both ambient and diffuse light.
96 	 * To cope with sun overbrightness, the color is scaled by 0.5.
97 	 *
98 	 * @param normal normal vector (must have length 1)
99 	 */
EvaluateUnitScaled(const CVector3D & normal)100 	RGBColor EvaluateUnitScaled(const CVector3D& normal) const
101 	{
102 		float dot = -normal.Dot(m_SunDir);
103 
104 		RGBColor color = m_UnitsAmbientColor;
105 		if (dot > 0)
106 			color += m_SunColor * dot;
107 
108 		return color * 0.5f;
109 	}
110 
111 	/**
112 	 * Compute the diffuse sun lighting color on terrain, for rendering with CPU lighting.
113 	 * To cope with sun overbrightness, the color is scaled by 0.5.
114 	 *
115 	 * @param normal normal vector (must have length 1)
116 	 */
EvaluateTerrainDiffuseScaled(const CVector3D & normal)117 	SColor4ub EvaluateTerrainDiffuseScaled(const CVector3D& normal) const
118 	{
119 		float dot = -normal.Dot(m_SunDir);
120 		return ConvertRGBColorTo4ub(m_SunColor * dot * 0.5f);
121 	}
122 
123 	/**
124 	 * Compute the diffuse sun lighting factor on terrain, for rendering with shader lighting.
125 	 *
126 	 * @param normal normal vector (must have length 1)
127 	 */
EvaluateTerrainDiffuseFactor(const CVector3D & normal)128 	SColor4ub EvaluateTerrainDiffuseFactor(const CVector3D& normal) const
129 	{
130 		float dot = -normal.Dot(m_SunDir);
131 		u8 c = static_cast<u8>(clamp(dot * 255.f, 0.f, 255.f));
132 		return SColor4ub(c, c, c, 255);
133 	}
134 
135 	// Comparison operators
136 	bool operator==(const CLightEnv& o) const
137 	{
138 		return m_Elevation == o.m_Elevation &&
139 			m_Rotation == o.m_Rotation &&
140 			m_LightingModel == o.m_LightingModel &&
141 			m_SunColor == o.m_SunColor &&
142 			m_TerrainAmbientColor == o.m_TerrainAmbientColor &&
143 			m_UnitsAmbientColor == o.m_UnitsAmbientColor &&
144 			m_FogColor == o.m_FogColor &&
145 			m_FogFactor == o.m_FogFactor &&
146 			m_FogMax == o.m_FogMax &&
147 			m_Brightness == o.m_Brightness &&
148 			m_Contrast == o.m_Contrast &&
149 			m_Saturation == o.m_Saturation &&
150 			m_Bloom == o.m_Bloom;
151 	}
152 
153 	bool operator!=(const CLightEnv& o) const
154 	{
155 		return !(*this == o);
156 	}
157 
158 private:
159 	void CalculateSunDirection();
160 };
161 
162 #endif // INCLUDED_LIGHTENV
163