]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Allow materials to be overridden when inheriting a technique
[libs/gl.git] / source / technique.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "material.h"
5 #include "program.h"
6 #include "programdata.h"
7 #include "tag.h"
8 #include "technique.h"
9 #include "texture.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 RenderPass &Technique::add_pass(const GL::Tag &tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(const GL::Tag &tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(const GL::Tag &tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 bool Technique::has_shaders() const
32 {
33         for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
34                 if(i->second.get_shader_program())
35                         return true;
36         return false;
37 }
38
39
40 Technique::Loader::Loader(Technique &t):
41         DataFile::CollectionObjectLoader<Technique>(t, 0)
42 {
43         init();
44 }
45
46 Technique::Loader::Loader(Technique &t, Collection &c):
47         DataFile::CollectionObjectLoader<Technique>(t, &c)
48 {
49         init();
50 }
51
52 void Technique::Loader::init()
53 {
54         add("inherit", &Loader::inherit);
55         add("pass", &Loader::pass);
56 }
57
58 void Technique::Loader::inherit(const string &n)
59 {
60         obj.passes = get_collection().get<Technique>(n).get_passes();
61         InheritLoader ldr(obj, get_collection());
62         load_sub_with(ldr);
63 }
64
65 void Technique::Loader::pass(const string &n)
66 {
67         RenderPass p;
68         if(coll)
69                 load_sub(p, get_collection());
70         else
71                 load_sub(p);
72
73         insert_unique(obj.passes, n, p);
74 }
75
76
77 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
78         DataFile::CollectionObjectLoader<Technique>(t, &c)
79 {
80         add("material", &InheritLoader::material);
81         add("texture", &InheritLoader::texture);
82 }
83
84 void Technique::InheritLoader::material(const string &pass_tag, const string &name)
85 {
86         RenderPass &pass = get_item(obj.passes, pass_tag);
87         const Material &mat = get_collection().get<Material>(name);
88         if(const Material *base_mat = pass.get_material())
89         {
90                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
91                         if(i->second.get_material()==base_mat)
92                                 i->second.set_material(&mat);
93         }
94         else
95                 pass.set_material(&mat);
96 }
97
98 void Technique::InheritLoader::texture(const string &slot, const string &name)
99 {
100         Texture &tex = get_collection().get<Texture>(name);
101         for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
102         {
103                 int index = i->second.get_texture_index(slot);
104                 if(index<0)
105                         continue;
106                 i->second.set_texture(index, &tex);
107         }
108 }
109
110 } // namespace GL
111 } // namespace Msp