]> git.tdb.fi Git - libs/gl.git/blob - source/light.h
Immediate update camera aspect in View::set_camera
[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.
16
17 Lights do not cast shadows by themselves.  See ShadowMap for that.
18 */
19 class Light
20 {
21 private:
22         enum ParameterMask
23         {
24                 DIFFUSE = 1,
25                 SPECULAR = 2,
26                 POSITION = 4,
27                 SPOT_DIR = 8,
28                 SPOT_EXP = 16,
29                 SPOT_CUTOFF = 32,
30                 ATTENUATION = 64
31         };
32
33         Color diffuse;
34         Color specular;
35         Vector4 position;
36         Vector3 spot_dir;
37         float spot_exp;
38         float spot_cutoff;
39         float attenuation[3];
40
41 public:
42         Light();
43         ~Light();
44
45 private:
46         void update_parameter(int, int = -1) const;
47
48 public:
49         /** Sets the diffuse (direction-independent) color of the Light.  Provided
50         to shaders with the name light_sources[i].diffuse. */
51         void set_diffuse(const Color &c);
52
53         /** Sets the specular (direction-dependent) color of the Light.  Provided to
54         shaders with the name light_sources[i].diffuse. */
55         void set_specular(const Color &c);
56
57         const Color &get_diffuse() const { return diffuse; }
58         const Color &get_specular() const { return specular; }
59
60         /** Sets the position of the Light.  For a directional light, set the xyz
61         components to a vector pointing towards the light and the w component to 0. */
62         void set_position(const Vector4 &);
63
64         const Vector4 &get_position() const { return position; }
65
66         void set_spot_direction(const Vector3 &);
67         void set_spot_exponent(float);
68         void set_spot_cutoff(float);
69         const Vector3 &get_spot_direction() const { return spot_dir; }
70         float get_spot_exponent() const { return spot_exp; }
71         float get_spot_cutoff() const { return spot_cutoff; }
72         void set_attenuation(float, float, float);
73         const float *get_attenuation() const { return attenuation; }
74
75         /** Updates a ProgramData object with the uniforms for the Light.  A view
76         matrix and light source index must be passed in. */
77         void update_shader_data(ProgramData &, const Matrix &, unsigned) const;
78
79         void bind() const { return bind_to(0); }
80         void bind_to(unsigned) const;
81
82         static const Light *current(unsigned = 0);
83         static void unbind() { return unbind_from(0); }
84         static void unbind_from(unsigned);
85 };
86
87 } // namespace GL
88 } // namespace Msp
89
90 #endif