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