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