]> git.tdb.fi Git - libs/gl.git/blob - source/materials/light.h
Modify sunlight color based on transmittance of the atmosphere
[libs/gl.git] / source / materials / light.h
1 #ifndef MSP_GL_LIGHT_H_
2 #define MSP_GL_LIGHT_H_
3
4 #include <vector>
5 #include <msp/datafile/objectloader.h>
6 #include "color.h"
7 #include "placeable.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Matrix;
13 class ProgramData;
14
15 /**
16 Stores properties of a single light source.  Lights can be directional, point
17 lights or spotlights.  No explicit type parameter is provided; rather the
18 other parameters determine what kind of light it is.  If the fourth component
19 of position is zero, it's a directional light.  Otherwise, if the spot cutoff
20 is not 180 degrees, it's a spotlight.  Otherwise it's an omnidirectional point
21 light.
22
23 Lights are usually grouped with a Lighting object, which can be used in a
24 Sequence::Step.
25
26 Lights do not cast shadows by themselves.  See ShadowMap for that.
27 */
28 class Light: public Placeable
29 {
30 public:
31         class Loader: public DataFile::ObjectLoader<Light>
32         {
33         public:
34                 Loader(Light &);
35
36         private:
37                 void attenuation(float, float, float);
38                 void color(float, float, float);
39                 void position(float, float, float, float);
40                 void spot_direction(float, float, float);
41                 void spot_exponent(float);
42                 void spot_cutoff(float);
43         };
44
45 private:
46         Color color;
47         Color transmittance;
48         Vector4 position;
49         Vector3 spot_dir;
50         Vector3 direction;
51         float spot_exp;
52         Geometry::Angle<float> spot_cutoff;
53         float attenuation[3];
54
55 public:
56         Light();
57
58 private:
59         void update_matrix();
60
61 public:
62         /** Sets the color of the Light.  Provided
63         to shaders as light_sources[i].color. */
64         void set_color(const Color &);
65
66         /** Sets a multiplier on how much light actually reaches the target.  Used
67         when modeling an atmosphere. */
68         void set_transmittance(const Color &);
69
70         const Color &get_color() const { return color; }
71         const Color &get_transmittance() const { return transmittance; }
72
73         DEPRECATED void set_diffuse(const Color &c) { set_color(c); }
74         DEPRECATED void set_specular(const Color &) { }
75         DEPRECATED const Color &get_diffuse() const { return color; }
76         DEPRECATED const Color &get_specular() const { return color; }
77
78         /** Sets the postion and orientation of the Light from a matrix.  Negative Z
79         axis is used as the spot direction, other axes are ignored. */
80         virtual void set_matrix(const Matrix &);
81
82         /** Sets the position of the Light.  For a directional light, set the xyz
83         components to a vector pointing towards the light and the w component to 0. */
84         void set_position(const Vector4 &);
85
86         const Vector4 &get_position() const { return position; }
87
88         /** Sets the direction of a spotlight.  Has no effect if spotlight cutoff is
89         not set. */
90         void set_spot_direction(const Vector3 &);
91
92         /** Sets the angular falloff exponent of the spotlight.  Must be >= 0. */
93         void set_spot_exponent(float);
94
95         /** Sets the cutoff angle of a spotlight.  Beyond this angle from its axis
96         the spotlight provides no illumination.  Must be between 0 and 90 degrees,
97         or exactly 180 degrees to indicate a non-spotlight. */
98         void set_spot_cutoff(const Geometry::Angle<float> &);
99
100         /** Disables spotlight, reverting to an omnidirectional point light.
101         Equivalent to setting the spot cutoff to 180 degrees. */
102         void disable_spot_cutoff();
103
104         const Vector3 &get_spot_direction() const { return spot_dir; }
105         float get_spot_exponent() const { return spot_exp; }
106         const Geometry::Angle<float> &get_spot_cutoff() const { return spot_cutoff; }
107         void set_attenuation(float, float, float);
108         const float *get_attenuation() const { return attenuation; }
109
110         /** Updates a ProgramData object with the uniforms for the Light.  A view
111         matrix and light source index must be passed in. */
112         void update_shader_data(ProgramData &, const Matrix &, unsigned) const;
113 };
114
115 } // namespace GL
116 } // namespace Msp
117
118 #endif