]> git.tdb.fi Git - libs/gl.git/blob - source/light.h
Update deprecated things
[libs/gl.git] / source / 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 Pipeline::Pass.
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 diffuse(float, float, float);
39                 void position(float, float, float, float);
40                 void specular(float, float, float);
41                 void spot_direction(float, float, float);
42                 void spot_exponent(float);
43                 void spot_cutoff(float);
44         };
45
46 private:
47         enum ParameterMask
48         {
49                 DIFFUSE = 1,
50                 SPECULAR = 2,
51                 POSITION = 4,
52                 SPOT_DIR = 8,
53                 SPOT_EXP = 16,
54                 SPOT_CUTOFF = 32,
55                 ATTENUATION = 64
56         };
57
58         Color diffuse;
59         Color specular;
60         Vector4 position;
61         Vector3 spot_dir;
62         Vector3 direction;
63         float spot_exp;
64         Geometry::Angle<float> spot_cutoff;
65         float attenuation[3];
66
67 public:
68         Light();
69         ~Light();
70
71 private:
72         void update_parameter(int, int = -1) const;
73         void update_matrix();
74
75 public:
76         /** Sets the diffuse (direction-independent) color of the Light.  Provided
77         to shaders with the name light_sources[i].diffuse. */
78         void set_diffuse(const Color &c);
79
80         /** Sets the specular (direction-dependent) color of the Light.  Provided to
81         shaders with the name light_sources[i].specular. */
82         void set_specular(const Color &c);
83
84         const Color &get_diffuse() const { return diffuse; }
85         const Color &get_specular() const { return specular; }
86
87         /** Sets the postion and orientation of the Light from a matrix.  Negative Z
88         axis is used as the spot direction, other axes are ignored. */
89         virtual void set_matrix(const Matrix &);
90
91         /** Sets the position of the Light.  For a directional light, set the xyz
92         components to a vector pointing towards the light and the w component to 0. */
93         void set_position(const Vector4 &);
94
95         const Vector4 &get_position() const { return position; }
96
97         /** Sets the direction of a spotlight.  Has no effect if spotlight cutoff is
98         not set. */
99         void set_spot_direction(const Vector3 &);
100
101         /** Sets the angular falloff exponent of the spotlight.  Must be >= 0. */
102         void set_spot_exponent(float);
103
104         /// Deprecated.
105         void set_spot_cutoff(float);
106
107         /** Sets the cutoff angle of a spotlight.  Beyond this angle from its axis
108         the spotlight provides no illumination.  Must be between 0 and 90 degrees,
109         or exactly 180 degrees to indicate a non-spotlight. */
110         void set_spot_cutoff(const Geometry::Angle<float> &);
111
112         /** Disables spotlight, reverting to an omnidirectional point light.
113         Equivalent to setting the spot cutoff to 180 degrees. */
114         void disable_spot_cutoff();
115
116         const Vector3 &get_spot_direction() const { return spot_dir; }
117         float get_spot_exponent() const { return spot_exp; }
118         const Geometry::Angle<float> &get_spot_cutoff() const { return spot_cutoff; }
119         void set_attenuation(float, float, float);
120         const float *get_attenuation() const { return attenuation; }
121
122         /** Updates a ProgramData object with the uniforms for the Light.  A view
123         matrix and light source index must be passed in. */
124         void update_shader_data(ProgramData &, const Matrix &, unsigned) const;
125
126         void bind() const { return bind_to(0); }
127         void bind_to(unsigned) const;
128
129         static const Light *current(unsigned = 0);
130         static void unbind() { return unbind_from(0); }
131         static void unbind_from(unsigned);
132 };
133
134 } // namespace GL
135 } // namespace Msp
136
137 #endif