]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
def0a84a9dd019ea385919a883ff4c7e15fd3d62
[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 Tag &tag)
17 {
18         return insert_unique(passes, tag, RenderPass())->second;
19 }
20
21 bool Technique::has_pass(const Tag &tag) const
22 {
23         return passes.count(tag);
24 }
25
26 const RenderPass &Technique::get_pass(const Tag &tag) const
27 {
28         return get_item(passes, tag);
29 }
30
31 bool Technique::replace_texture(const string &slot, const Texture &tex)
32 {
33         bool replaced = false;
34         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
35         {
36                 int index = i->second.get_texture_index(slot);
37                 if(index>=0)
38                 {
39                         i->second.set_texture(index, &tex);
40                         replaced = true;
41                 }
42         }
43
44         return replaced;
45 }
46
47 bool Technique::has_shaders() const
48 {
49         for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
50                 if(i->second.get_shader_program())
51                         return true;
52         return false;
53 }
54
55
56 Technique::Loader::Loader(Technique &t):
57         DataFile::CollectionObjectLoader<Technique>(t, 0)
58 {
59         init();
60 }
61
62 Technique::Loader::Loader(Technique &t, Collection &c):
63         DataFile::CollectionObjectLoader<Technique>(t, &c)
64 {
65         init();
66 }
67
68 void Technique::Loader::init()
69 {
70         add("inherit", &Loader::inherit);
71         add("pass", &Loader::pass);
72 }
73
74 void Technique::Loader::inherit(const string &n)
75 {
76         obj.passes = get_collection().get<Technique>(n).get_passes();
77         InheritLoader ldr(obj, get_collection());
78         load_sub_with(ldr);
79 }
80
81 void Technique::Loader::pass(const string &n)
82 {
83         RenderPass p;
84         if(coll)
85                 load_sub(p, get_collection());
86         else
87                 load_sub(p);
88
89         insert_unique(obj.passes, n, p);
90 }
91
92
93 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
94         DataFile::CollectionObjectLoader<Technique>(t, &c)
95 {
96         add("material", &InheritLoader::material);
97         add("texture", &InheritLoader::texture);
98 }
99
100 void Technique::InheritLoader::material(const string &pass_tag, const string &name)
101 {
102         RenderPass &pass = get_item(obj.passes, pass_tag);
103         const Material &mat = get_collection().get<Material>(name);
104         if(const Material *base_mat = pass.get_material())
105         {
106                 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
107                         if(i->second.get_material()==base_mat)
108                                 i->second.set_material(&mat);
109         }
110         else
111                 pass.set_material(&mat);
112 }
113
114 void Technique::InheritLoader::texture(const string &slot, const string &name)
115 {
116         if(!obj.replace_texture(slot, get_collection().get<Texture>(name)))
117                 throw key_error(slot);
118 }
119
120 } // namespace GL
121 } // namespace Msp