]> git.tdb.fi Git - libs/gl.git/blob - source/material.h
Don't try to access a nonexistent return expression
[libs/gl.git] / source / material.h
1 #ifndef MSP_GL_MATERIAL_H_
2 #define MSP_GL_MATERIAL_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "bindable.h"
6 #include "color.h"
7 #include "programdata.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Stores basic material properties.  This includes color and reflection
14 parameters, but does not include texturing.  Materials interact with light
15 soucres and ambient lighting to produce the base color of a surface.  Textures
16 can be used to add detail.
17
18 Material provides a set of uniform variables for use with shaders.  Standard
19 shaders generated by ProgramBuilder only use it when legacy mode is disabled.
20
21 In legacy mode, materials are applied with several calls to glMaterial.
22 */
23 class Material: public BindableWithDefault<Material>
24 {
25 public:
26         class Loader: public DataFile::CollectionObjectLoader<Material>
27         {
28         private:
29                 bool srgb;
30
31         public:
32                 Loader(Material &);
33                 Loader(Material &, Collection &);
34         private:
35                 void init();
36
37                 Color make_color(float, float, float, float);
38                 void ambient(float, float, float, float);
39                 void diffuse(float, float, float, float);
40                 void specular(float, float, float, float);
41                 void emission(float, float, float, float);
42                 void shininess(float);
43                 void reflectivity(float);
44         };
45
46 private:
47         enum ParameterMask
48         {
49                 AMBIENT = 1,
50                 DIFFUSE = 2,
51                 SPECULAR = 4,
52                 EMISSION = 8,
53                 SHININESS = 16
54         };
55
56         Color ambient;
57         Color diffuse;
58         Color specular;
59         Color emission;
60         float shininess;
61         float reflectivity;
62         ProgramData shdata;
63
64 public:
65         Material();
66
67 private:
68         void update_parameter(int) const;
69
70 public:
71         /** Sets the ambient color of the material.  Provided to shaders with the
72         name material.ambient. */
73         void set_ambient(const Color &);
74
75         /** Sets the diffuse (direction-independent) color of the material.
76         Provided to shaders with the name material.diffuse. */
77         void set_diffuse(const Color &);
78
79         /** Sets the specular (direction-dependent) color of the material.  Provided
80         to shaders with the name material.specular. */
81         void set_specular(const Color &);
82         void set_emission(const Color &);
83
84         /** Sets the specular exponent of the material.  Provided to shaders with
85         the name material.shininess. */
86         void set_shininess(float);
87
88         /** Sets the reflectivity of the material.  Provided to shaders with the
89         name reflectivity.  Has no effect when shaders are not used. */
90         void set_reflectivity(float);
91
92         const Color &get_ambient() const { return ambient; }
93         const Color &get_diffuse() const { return diffuse; }
94         const Color &get_specular() const { return specular; }
95         const Color &get_emission() const { return emission; }
96         float get_shininess() const { return shininess; }
97         float get_reflectivity() const { return reflectivity; }
98
99         /** Returns the uniforms for the material.  Not needed for shaders that use
100         the legacy built-in uniform gl_FrontMaterial and have no reflections. */
101         const ProgramData &get_shader_data() const { return shdata; }
102
103         void bind() const;
104 };
105
106 } // namespace GL
107 } // namespace Msp
108
109 #endif