]> git.tdb.fi Git - libs/gl.git/blob - source/lighting.h
Keep track of which components have been set in Transform
[libs/gl.git] / source / lighting.h
1 #ifndef MSP_GL_LIGHTING_H_
2 #define MSP_GL_LIGHTING_H_
3
4 #include <vector>
5 #include <msp/geometry/angle.h>
6 #include "bindable.h"
7 #include "color.h"
8 #include "gl.h"
9 #include "programdata.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Light;
15
16 /**
17 Encapsulates global lighting parameters and any number of individual light
18 sources.
19 */
20 class Lighting: public Bindable<Lighting>
21 {
22 public:
23         class Loader: public DataFile::ObjectLoader<Lighting>
24         {
25         public:
26                 Loader(Lighting &);
27
28         private:
29                 void ambient(float, float, float);
30                 void fog_color(float, float, float);
31                 void fog_density(float);
32                 void fog_half_distance(float);
33                 void horizon_angle(float);
34                 void light(unsigned);
35                 void sky_color(float, float, float);
36                 void zenith_direction(float, float, float);
37         };
38
39 private:
40         Color ambient;
41         Color sky_color;
42         Vector3 zenith_direction;
43         Geometry::Angle<float> horizon_angle;
44         Color fog_color;
45         float fog_density;
46         std::vector<const Light *> lights;
47         std::vector<Light *> owned_data;
48
49 public:
50         Lighting();
51         ~Lighting();
52
53         /** Sets the ambient lighting color.  Affects all surfaces in the scene. */
54         void set_ambient(const Color &);
55
56         const Color &get_ambient() const { return ambient; }
57
58         /** Sets the color of the sky at zenith.  Has no effect without shaders. */
59         void set_sky_color(const Color &);
60
61         /** Sets the direction of the zenith.  Defaults to positive Z axis.  Has no
62         effect without shaders. */
63         void set_zenith_direction(const Vector3 &);
64
65         /// Deprecated alias for set_zenith_direction
66         void set_sky_direction(const Vector3 &d) { set_zenith_direction(d); }
67
68         /** Sets the angle where skylight cuts off, counted from the true horizon.
69         Has no effect without shaders. */
70         void set_horizon_angle(const Geometry::Angle<float> &);
71
72         /** Sets the fog color, which is blended into distant surfaces. */
73         void set_fog_color(const Color &);
74
75         /** Sets the density of the fog.  Zero means no fog. */
76         void set_fog_density(float);
77
78         /** Sets the density of the fog so that the blending factor at the given
79         distance is 50%. */
80         void set_fog_half_distance(float);
81
82         /** Attaches a light source.  If the attachment index is greater than
83         LightUnit::get_n_units, the Lighting can't be bound for legacy mode. */
84         void attach(unsigned, const Light &);
85
86         /** Detaches a light source. */
87         void detach(unsigned);
88
89         /** Returns an attached light.  If no light is attached at that index, null
90         is returned. */
91         const Light *get_attached_light(unsigned) const;
92
93         /** Updates a ProgramData object with the uniforms for the Lighting,
94         including all attached light sources.  A view matrix must be passed in. */
95         void update_shader_data(ProgramData &, const Matrix &) const;
96
97         void bind() const;
98
99         static void unbind();
100 };
101
102 } // namespace GL
103 } // namespace Msp
104
105 #endif