Pyrogenesis  trunk
LightEnv.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 public:
42 
43  float m_FogFactor;
44  float m_FogMax;
45 
47 
48  CLightEnv();
49 
50  float GetElevation() const { return m_Elevation; }
51  float GetRotation() const { return m_Rotation; }
52  const CVector3D& GetSunDir() const { return m_SunDir; }
53 
54  void SetElevation(float f);
55  void SetRotation(float f);
56 
57  /**
58  * Calculate brightness of a point of a unit with the given normal vector,
59  * for rendering with CPU lighting.
60  * The resulting color contains both ambient and diffuse light.
61  * To cope with sun overbrightness, the color is scaled by 0.5.
62  *
63  * @param normal normal vector (must have length 1)
64  */
65  RGBColor EvaluateUnitScaled(const CVector3D& normal) const
66  {
67  float dot = -normal.Dot(m_SunDir);
68 
69  RGBColor color = m_AmbientColor;
70  if (dot > 0)
71  color += m_SunColor * dot;
72 
73  return color * 0.5f;
74  }
75 
76  // Comparison operators
77  bool operator==(const CLightEnv& o) const
78  {
79  return m_Elevation == o.m_Elevation &&
80  m_Rotation == o.m_Rotation &&
81  m_SunColor == o.m_SunColor &&
82  m_AmbientColor == o.m_AmbientColor &&
83  m_FogColor == o.m_FogColor &&
84  m_FogFactor == o.m_FogFactor &&
85  m_FogMax == o.m_FogMax &&
86  m_Brightness == o.m_Brightness &&
87  m_Contrast == o.m_Contrast &&
88  m_Saturation == o.m_Saturation &&
89  m_Bloom == o.m_Bloom;
90  }
91 
92  bool operator!=(const CLightEnv& o) const
93  {
94  return !(*this == o);
95  }
96 
97 private:
98  friend class CMapWriter;
99  friend class CMapReader;
100  friend class CXMLReader;
101 
102  /**
103  * Height of sun above the horizon, in radians.
104  * For example, an elevation of M_PI/2 means the sun is straight up.
105  */
106  float m_Elevation;
107 
108  /**
109  * Direction of sun on the compass, in radians.
110  * For example, a rotation of zero means the sun is in the direction (0,0,-1)
111  * and a rotation of M_PI/2 means the sun is in the direction (1,0,0) (not taking
112  * elevation into account).
113  */
114  float m_Rotation;
115 
116  /**
117  * Vector corresponding to m_Elevation and m_Rotation.
118  * Updated by CalculateSunDirection.
119  */
121 
122  void CalculateSunDirection();
123 };
124 
125 #endif // INCLUDED_LIGHTENV
const CVector3D & GetSunDir() const
Definition: LightEnv.h:52
float GetElevation() const
Definition: LightEnv.h:50
void CalculateSunDirection()
Definition: LightEnv.cpp:50
bool operator==(const CLightEnv &o) const
Definition: LightEnv.h:77
Definition: Vector3D.h:30
RGBColor m_SunColor
Definition: LightEnv.h:39
Definition: MapWriter.h:38
bool operator!=(const CLightEnv &o) const
Definition: LightEnv.h:92
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:65
Definition: MapReader.cpp:406
void SetElevation(float f)
Definition: LightEnv.cpp:38
float m_Elevation
Height of sun above the horizon, in radians.
Definition: LightEnv.h:106
RGBColor m_FogColor
Definition: LightEnv.h:41
Definition: MapReader.h:45
float m_FogMax
Definition: LightEnv.h:44
float m_Rotation
Direction of sun on the compass, in radians.
Definition: LightEnv.h:114
float m_Bloom
Definition: LightEnv.h:46
float m_Contrast
Definition: LightEnv.h:46
RGBColor m_AmbientColor
Definition: LightEnv.h:40
CLightEnv()
Definition: LightEnv.cpp:25
float m_FogFactor
Definition: LightEnv.h:43
void SetRotation(float f)
Definition: LightEnv.cpp:44
float m_Saturation
Definition: LightEnv.h:46
float m_Brightness
Definition: LightEnv.h:46
float Dot(const CVector3D &vector) const
Definition: Vector3D.h:104
Class CLightEnv: description of a lighting environment - contains all the necessary parameters for re...
Definition: LightEnv.h:36
CVector3D m_SunDir
Vector corresponding to m_Elevation and m_Rotation.
Definition: LightEnv.h:120
float GetRotation() const
Definition: LightEnv.h:51