Pyrogenesis  trunk
LightEnv.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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  */
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:
73 
74  float m_FogFactor;
75  float m_FogMax;
76 
78 
79 public:
80  CLightEnv();
81 
82  float GetElevation() const { return m_Elevation; }
83  float GetRotation() const { return m_Rotation; }
84  const CVector3D& GetSunDir() const { return m_SunDir; }
85  const std::string& GetLightingModel() const { return m_LightingModel; }
86 
87  void SetElevation(float f);
88  void SetRotation(float f);
89 
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  */
100  RGBColor EvaluateUnitScaled(const CVector3D& normal) const
101  {
102  float dot = -normal.Dot(m_SunDir);
103 
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  */
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  */
129  {
130  float dot = -normal.Dot(m_SunDir);
131  int c = clamp((int)(dot * 255), 0, 255);
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
float GetElevation() const
Definition: LightEnv.h:82
const CVector3D & GetSunDir() const
Definition: LightEnv.h:84
void CalculateSunDirection()
Definition: LightEnv.cpp:56
RGBColor m_TerrainAmbientColor
Definition: LightEnv.h:70
const std::string & GetLightingModel() const
Definition: LightEnv.h:85
float Dot(const CVector3D &vector) const
Definition: Vector3D.h:99
SColor4ub EvaluateTerrainDiffuseScaled(const CVector3D &normal) const
Compute the diffuse sun lighting color on terrain, for rendering with CPU lighting.
Definition: LightEnv.h:117
Definition: Vector3D.h:28
std::string m_LightingModel
A string that shaders use to determine what lighting model to implement.
Definition: LightEnv.h:66
RGBColor m_SunColor
Definition: LightEnv.h:69
Definition: MapWriter.h:41
Definition: MapReader.cpp:409
void SetElevation(float f)
Definition: LightEnv.cpp:44
float m_Elevation
Height of sun above the horizon, in radians.
Definition: LightEnv.h:46
RGBColor EvaluateUnitScaled(const CVector3D &normal) const
Calculate brightness of a point of a unit with the given normal vector, for rendering with CPU lighti...
Definition: LightEnv.h:100
RGBColor m_UnitsAmbientColor
Definition: LightEnv.h:71
RGBColor m_FogColor
Definition: LightEnv.h:72
Definition: MapReader.h:45
float m_FogMax
Definition: LightEnv.h:75
float m_Rotation
Direction of sun on the compass, in radians.
Definition: LightEnv.h:54
float m_Bloom
Definition: LightEnv.h:77
void SetLightingModel(const std::string &model)
Definition: LightEnv.h:90
Definition: SColor.h:30
float m_Contrast
Definition: LightEnv.h:77
SColor4ub(* ConvertRGBColorTo4ub)(const RGBColor &src)
Definition: Color.cpp:45
float GetRotation() const
Definition: LightEnv.h:83
static size_t model
Definition: x86_x64.cpp:221
CLightEnv()
Definition: LightEnv.cpp:29
float m_FogFactor
Definition: LightEnv.h:74
void SetRotation(float f)
Definition: LightEnv.cpp:50
float m_Saturation
Definition: LightEnv.h:77
float m_Brightness
Definition: LightEnv.h:77
bool operator!=(const CLightEnv &o) const
Definition: LightEnv.h:153
SColor4ub EvaluateTerrainDiffuseFactor(const CVector3D &normal) const
Compute the diffuse sun lighting factor on terrain, for rendering with shader lighting.
Definition: LightEnv.h:128
bool operator==(const CLightEnv &o) const
Definition: LightEnv.h:136
Class CLightEnv: description of a lighting environment - contains all the necessary parameters for re...
Definition: LightEnv.h:36
T clamp(T value, T min, T max)
Definition: MathUtil.h:32
CVector3D m_SunDir
Vector corresponding to m_Elevation and m_Rotation.
Definition: LightEnv.h:60