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