]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Reintroduce named texture slots for Technique inheritance
[libs/gl.git] / source / technique.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2008-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/collection.h>
10 #include <msp/strings/formatter.h>
11 #include "material.h"
12 #include "program.h"
13 #include "programdata.h"
14 #include "tag.h"
15 #include "technique.h"
16 #include "texture.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace GL {
22
23 RenderPass &Technique::add_pass(const GL::Tag &tag)
24 {
25         if(passes.count(tag))
26                 throw KeyError("Duplicate pass");
27
28         return passes[tag];
29 }
30
31 bool Technique::has_pass(const GL::Tag &tag) const
32 {
33         return passes.count(tag);
34 }
35
36 const RenderPass &Technique::get_pass(const GL::Tag &tag) const
37 {
38         PassMap::const_iterator i = passes.find(tag);
39         if(i==passes.end())
40                 throw KeyError("Unknown pass");
41         return i->second;
42 }
43
44
45 Technique::Loader::Loader(Technique &t):
46         DataFile::CollectionObjectLoader<Technique>(t, 0)
47 {
48         init();
49 }
50
51 Technique::Loader::Loader(Technique &t, Collection &c):
52         DataFile::CollectionObjectLoader<Technique>(t, &c)
53 {
54         init();
55 }
56
57 void Technique::Loader::init()
58 {
59         add("inherit", &Loader::inherit);
60         add("pass", &Loader::pass);
61 }
62
63 void Technique::Loader::inherit(const string &n)
64 {
65         obj.passes = get_collection().get<Technique>(n)->get_passes();
66         InheritLoader ldr(obj, get_collection());
67         load_sub_with(ldr);
68 }
69
70 void Technique::Loader::pass(const string &n)
71 {
72         Tag tag(n);
73         if(obj.passes.count(tag))
74                 throw KeyError("Duplicate pass name", n);
75
76         RenderPass p;
77         if(coll)
78                 load_sub(p, get_collection());
79         else
80                 load_sub(p);
81         obj.passes.insert(PassMap::value_type(tag, p));
82 }
83
84
85 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
86         DataFile::CollectionObjectLoader<Technique>(t, &c)
87 {
88         add("texture", &InheritLoader::texture);
89 }
90
91 void Technique::InheritLoader::texture(const std::string &slot, const string &name)
92 {
93         Texture *tex = get_collection().get<Texture>(name);
94         for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
95         {
96                 int index = i->second.get_texture_index(slot);
97                 if(index<0)
98                         continue;
99                 i->second.set_texture(index, tex);
100         }
101 }
102
103 } // namespace GL
104 } // namespace Msp