]> 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 = NO_CULL;
89         Blend blend;
90         bool receive_shadows = false;
91         bool image_based_lighting = false;
92         bool instancing = false;
93
94         void maybe_create_material_shader();
95         void set_material_textures();
96
97 public:
98         /** Sets the shader program and uniform values. */
99         void set_shader_program(const Program *, const ProgramData *);
100
101         const Program *get_shader_program() const { return shprog; }
102         const ProgramData *get_shader_data() const { return shdata.get(); }
103         Tag get_slotted_uniform_tag(Tag) const;
104
105         /** Sets a Material to use as a basis for the render method.  If a shader
106         has not been explicitly set, the material's shader will be used. */
107         void set_material(const Material *);
108
109         const Material *get_material() const { return material; }
110         const std::string &get_material_slot_name() const { return material_slot; }
111         void set_texture(Tag, const Texture *, const Sampler * = 0);
112         Tag get_texture_tag(const std::string &) const;
113         void set_face_cull(CullMode);
114         CullMode get_face_cull() const { return face_cull; }
115         void set_blend(const Blend &);
116         const Blend &get_blend() const { return blend; }
117
118         /** Toggles shadows on objects using this render method.  Only affects
119         shaders created from materials.  A ShadowMap effect is required. */
120         void set_receive_shadows(bool);
121
122         bool get_receive_shadows() const { return receive_shadows; }
123
124         /** Toggles the use of an environment map as a light source.  Only affects
125         shaders created from materials.  An EnvironmentMap effect is required. */
126         void set_image_based_lighting(bool);
127
128         bool get_image_based_lighting() const { return image_based_lighting; }
129
130         /** Toggles instanced rendering.  Only affects shaders created from
131         materials.  Should be used with InstanceArray. */
132         void set_instancing(bool);
133
134         bool get_instancing() const { return instancing; }
135
136         void apply(Renderer &) const;
137
138         void set_debug_name(const std::string &);
139 };
140
141 } // namespace GL
142 } // namespace Msp
143
144 #endif