]> git.tdb.fi Git - libs/gl.git/blob - source/materials/lighting.h
a2f84c32e7105f5c189fb470410cf5d757794f7f
[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         struct AttachedLight
48         {
49                 const Light *light;
50                 mutable unsigned generation;
51
52                 AttachedLight(const Light *l): light(l), generation(0) { }
53         };
54
55         Color ambient;
56         Color sky_color;
57         Vector3 zenith_direction;
58         Geometry::Angle<float> horizon_angle;
59         Color fog_color;
60         float fog_density;
61         std::vector<AttachedLight> lights;
62         std::vector<Light *> owned_data;
63         mutable ProgramData shdata;
64
65 public:
66         Lighting();
67         ~Lighting();
68
69         /** Sets the ambient lighting color.  Affects all surfaces in the scene. */
70         void set_ambient(const Color &);
71
72         const Color &get_ambient() const { return ambient; }
73
74         /** Sets the color of the sky at zenith.  Has no effect without shaders. */
75         DEPRECATED void set_sky_color(const Color &);
76
77         /** Sets the direction of the zenith.  Defaults to positive Z axis.  Has no
78         effect without shaders. */
79         DEPRECATED void set_zenith_direction(const Vector3 &);
80
81         /** Sets the angle where skylight cuts off, counted from the true horizon.
82         Has no effect without shaders. */
83         DEPRECATED void set_horizon_angle(const Geometry::Angle<float> &);
84
85         /** Sets the fog color, which is blended into distant surfaces. */
86         void set_fog_color(const Color &);
87
88         /** Sets the density of the fog.  Zero means no fog. */
89         void set_fog_density(float);
90
91         /** Sets the density of the fog so that the blending factor at the given
92         distance is 50%. */
93         void set_fog_half_distance(float);
94
95         /** Attaches a light source.  If the light was already attached, does
96         nothing. */
97         void attach(const Light &);
98
99         /** Detaches a light source.  If the light was not attached, does nothing. */
100         void detach(const Light &);
101
102         DEPRECATED void attach(unsigned, const Light &l) { attach(l); }
103         DEPRECATED void detach(unsigned);
104
105         /** Returns an attached light.  If no light is attached at that index, null
106         is returned. */
107         DEPRECATED const Light *get_attached_light(unsigned) const;
108
109         /** Updates a ProgramData object with the uniforms for the Lighting,
110         including all attached light sources.  A view matrix must be passed in. */
111         DEPRECATED void update_shader_data(ProgramData &, const Matrix &) const;
112
113         const ProgramData &get_shader_data() const;
114
115         void set_debug_name(const std::string &);
116 };
117
118 } // namespace GL
119 } // namespace Msp
120
121 #endif