]> git.tdb.fi Git - libs/gl.git/blob - source/light.h
Improve parameters and documentation of Light
[libs/gl.git] / source / light.h
1 #ifndef MSP_GL_LIGHT_H_
2 #define MSP_GL_LIGHT_H_
3
4 #include <vector>
5 #include "color.h"
6 #include "vector.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Matrix;
12 class ProgramData;
13
14 /**
15 Stores properties of a single light source.  Lights can be directional, point
16 lights or spotlights.  No explicit type parameter is provided; rather the
17 other parameters determine what kind of light it is.  If the fourth component
18 of position is zero, it's a directional light.  Otherwise, if the spot cutoff
19 is not 180 degrees, it's a spotlight.  Otherwise it's an omnidirectional point
20 light.
21
22 Lights are usually grouped with a Lighting object, which can be used in a
23 Pipeline::Pass.
24
25 Lights do not cast shadows by themselves.  See ShadowMap for that.
26 */
27 class Light
28 {
29 private:
30         enum ParameterMask
31         {
32                 DIFFUSE = 1,
33                 SPECULAR = 2,
34                 POSITION = 4,
35                 SPOT_DIR = 8,
36                 SPOT_EXP = 16,
37                 SPOT_CUTOFF = 32,
38                 ATTENUATION = 64
39         };
40
41         Color diffuse;
42         Color specular;
43         Vector4 position;
44         Vector3 spot_dir;
45         float spot_exp;
46         Geometry::Angle<float> spot_cutoff;
47         float attenuation[3];
48
49 public:
50         Light();
51         ~Light();
52
53 private:
54         void update_parameter(int, int = -1) const;
55
56 public:
57         /** Sets the diffuse (direction-independent) color of the Light.  Provided
58         to shaders with the name light_sources[i].diffuse. */
59         void set_diffuse(const Color &c);
60
61         /** Sets the specular (direction-dependent) color of the Light.  Provided to
62         shaders with the name light_sources[i].specular. */
63         void set_specular(const Color &c);
64
65         const Color &get_diffuse() const { return diffuse; }
66         const Color &get_specular() const { return specular; }
67
68         /** Sets the position of the Light.  For a directional light, set the xyz
69         components to a vector pointing towards the light and the w component to 0. */
70         void set_position(const Vector4 &);
71
72         const Vector4 &get_position() const { return position; }
73
74         /** Sets the direction of a spotlight.  Has no effect if spotlight cutoff is
75         not set. */
76         void set_spot_direction(const Vector3 &);
77
78         /** Sets the angular falloff exponent of the spotlight.  Must be >= 0. */
79         void set_spot_exponent(float);
80
81         /// Deprecated.
82         void set_spot_cutoff(float);
83
84         /** Sets the cutoff angle of a spotlight.  Beyond this angle from its axis
85         the spotlight provides no illumination.  Must be between 0 and 90 degrees,
86         or exactly 180 degrees to indicate a non-spotlight. */
87         void set_spot_cutoff(const Geometry::Angle<float> &);
88
89         /** Disables spotlight, reverting to an omnidirectional point light.
90         Equivalent to setting the spot cutoff to 180 degrees. */
91         void disable_spot_cutoff();
92
93         const Vector3 &get_spot_direction() const { return spot_dir; }
94         float get_spot_exponent() const { return spot_exp; }
95         const Geometry::Angle<float> &get_spot_cutoff() const { return spot_cutoff; }
96         void set_attenuation(float, float, float);
97         const float *get_attenuation() const { return attenuation; }
98
99         /** Updates a ProgramData object with the uniforms for the Light.  A view
100         matrix and light source index must be passed in. */
101         void update_shader_data(ProgramData &, const Matrix &, unsigned) const;
102
103         void bind() const { return bind_to(0); }
104         void bind_to(unsigned) const;
105
106         static const Light *current(unsigned = 0);
107         static void unbind() { return unbind_from(0); }
108         static void unbind_from(unsigned);
109 };
110
111 } // namespace GL
112 } // namespace Msp
113
114 #endif