]> git.tdb.fi Git - libs/gl.git/blob - source/materials/rendermethod.h
Check the flat qualifier from the correct member
[libs/gl.git] / source / materials / rendermethod.h
1 #ifndef MSP_GL_RENDERPASS_H_
2 #define MSP_GL_RENDERPASS_H_
3
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/objectloader.h>
6 #include "blend.h"
7 #include "cullface.h"
8 #include "material.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Program;
14 class ProgramData;
15 class Renderer;
16 class Sampler;
17 class Texture;
18
19 /**
20 Describes the appearance of a surface with a shader, uniform values and
21 textures.
22
23 A Material can be used to automatically populate most of the fields of a
24 RenderMethod.
25 */
26 class RenderMethod
27 {
28 public:
29         class Loader: public DataFile::CollectionObjectLoader<RenderMethod>
30         {
31         private:
32                 std::string inline_base_name;
33
34                 static ActionMap shared_actions;
35
36         public:
37                 Loader(RenderMethod &, Collection &);
38         private:
39                 virtual void init_actions();
40         
41         public:
42                 void set_inline_base_name(const std::string &);
43
44         private:
45                 virtual void finish();
46
47                 void blend();
48                 void blend_factors(BlendFactor, BlendFactor);
49                 void material_inline();
50                 void material(const std::string &);
51                 void shader(const std::string &);
52                 void texture(const std::string &);
53                 void uniforms();
54                 void uniform_slot(const std::string &);
55                 void uniform_slot2(const std::string &, const std::string &);
56         };
57
58 private:
59         struct TextureSlot
60         {
61                 class Loader: public DataFile::CollectionObjectLoader<TextureSlot>
62                 {
63                 private:
64                         std::string auto_slot_name;
65
66                 public:
67                         Loader(TextureSlot &, const std::string &, Collection *);
68
69                 private:
70                         void slot_auto();
71                 };
72
73                 Tag tag;
74                 std::string slot_name;
75                 const Texture *texture;
76                 const Sampler *sampler;
77
78                 TextureSlot(Tag t): tag(t), texture(0), sampler(0) { }
79         };
80
81         const Program *shprog = 0;
82         bool shprog_from_material = false;
83         RefPtr<ProgramData> shdata;
84         std::map<Tag, Tag> uniform_slots;
85         const Material *material = 0;
86         std::string material_slot;
87         std::vector<TextureSlot> textures;
88         CullMode face_cull = CULL_BACK;
89         Blend blend;
90         bool receive_shadows = false;
91         bool image_based_lighting = false;
92
93         void maybe_create_material_shader();
94         void set_material_textures();
95
96 public:
97         /** Sets the shader program and uniform values. */
98         void set_shader_program(const Program *, const ProgramData *);
99
100         const Program *get_shader_program() const { return shprog; }
101         const ProgramData *get_shader_data() const { return shdata.get(); }
102         Tag get_slotted_uniform_tag(Tag) const;
103
104         /** Sets a Material to use as a basis for the render method.  If a shader
105         has not been explicitly set, the material's shader will be used. */
106         void set_material(const Material *);
107
108         const Material *get_material() const { return material; }
109         const std::string &get_material_slot_name() const { return material_slot; }
110         void set_texture(Tag, const Texture *, const Sampler * = 0);
111         Tag get_texture_tag(const std::string &) const;
112         void set_face_cull(CullMode);
113         CullMode get_face_cull() const { return face_cull; }
114         void set_blend(const Blend &);
115         const Blend &get_blend() const { return blend; }
116
117         /** Toggles shadows on objects using this render method.  Only affects
118         shaders created from materials.  A ShadowMap effect is required. */
119         void set_receive_shadows(bool);
120
121         bool get_receive_shadows() const { return receive_shadows; }
122
123         /** Toggles the use of an environment map as a light source.  Only affects
124         shaders created from materials.  An EnvironmentMap effect is required. */
125         void set_image_based_lighting(bool);
126
127         bool get_image_based_lighting() const { return image_based_lighting; }
128
129         void apply(Renderer &) const;
130
131         void set_debug_name(const std::string &);
132 };
133
134 } // namespace GL
135 } // namespace Msp
136
137 #endif