]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Support collectionless loading of Technique and RenderPass
[libs/gl.git] / source / technique.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  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 bool Technique::has_pass(const GL::Tag &tag) const
24 {
25         return passes.count(tag);
26 }
27
28 const RenderPass &Technique::get_pass(const GL::Tag &tag) const
29 {
30         PassMap::const_iterator i=passes.find(tag);
31         if(i==passes.end())
32                 throw KeyError("Unknown pass");
33         return i->second;
34 }
35
36
37 Technique::Loader::Loader(Technique &t):
38         DataFile::CollectionObjectLoader<Technique>(t, 0)
39 {
40         init();
41 }
42
43 Technique::Loader::Loader(Technique &t, Collection &c):
44         DataFile::CollectionObjectLoader<Technique>(t, &c)
45 {
46         init();
47 }
48
49 void Technique::Loader::init()
50 {
51         add("inherit", &Loader::inherit);
52         add("pass", &Loader::pass);
53 }
54
55 void Technique::Loader::inherit(const string &n)
56 {
57         obj.passes=get_collection().get<Technique>(n)->get_passes();
58         InheritLoader ldr(obj, get_collection());
59         load_sub_with(ldr);
60 }
61
62 void Technique::Loader::pass(const string &n)
63 {
64         Tag tag(n);
65         if(obj.passes.count(tag))
66                 throw KeyError("Duplicate pass name", n);
67
68         RenderPass p;
69         if(coll)
70                 load_sub(p, get_collection());
71         else
72                 load_sub(p);
73         obj.passes.insert(PassMap::value_type(tag, p));
74 }
75
76
77 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
78         DataFile::CollectionObjectLoader<Technique>(t, &c)
79 {
80         add("texture", &InheritLoader::texture);
81 }
82
83 void Technique::InheritLoader::texture(const string &slot, const string &name)
84 {
85         Texture *tex=get_collection().get<Texture>(name);
86         for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
87         {
88                 try
89                 {
90                         i->second.set_texture(slot, tex);
91                 }
92                 catch(const KeyError &)
93                 { }
94         }
95 }
96
97 } // namespace GL
98 } // namespace Msp