]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Inherit Loaders from the ObjectLoader classes
[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 Technique::Technique():
24         main_texture(0),
25         normal_pass(&passes[0]),
26         material(0)
27 { }
28
29 Technique::~Technique()
30 {
31         for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
32                 delete i->second.shdata;
33 }
34
35 bool Technique::has_pass(const GL::Tag &tag) const
36 {
37         return passes.count(tag);
38 }
39
40 const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
41 {
42         PassMap::const_iterator i=passes.find(tag);
43         if(i==passes.end())
44                 throw KeyError("Unknown pass");
45         return i->second;
46 }
47
48 unsigned Technique::get_texture_index(const std::string &n) const
49 {
50         for(unsigned i=0; i<textures.size(); ++i)
51                 if(textures[i].name==n)
52                         return i;
53
54         throw KeyError("Unknown texture slot", n);
55 }
56
57 const Texture *Technique::get_texture(unsigned i) const
58 {
59         if(i>=textures.size())
60                 throw KeyError("Texture index out of range");
61
62         return textures[i].texture;
63 }
64
65
66 Technique::Loader::Loader(Technique &t, Collection &c):
67         DataFile::CollectionObjectLoader<Technique>(t, &c)
68 {
69         add("material",        &Technique::material);
70         add("material_inline", &Loader::material_inline);
71         add("pass",            &Loader::pass);
72         add("shader",          &Loader::shader);
73         add("shader_texture",  &Loader::shader_texture);
74         add("texture",         &Loader::texture);
75         add("texture_slot",    &Loader::texture_slot);
76 }
77
78 void Technique::Loader::finish()
79 {
80         for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
81                 if(i->second.shdata)
82                 {
83                         for(unsigned j=0; j<obj.textures.size(); ++j)
84                         {
85                                 unsigned loc=i->second.shprog->get_uniform_location(obj.textures[j].name);
86                                 i->second.shdata->uniform(loc, static_cast<int>(j));
87                         }
88                 }
89 }
90
91 void Technique::Loader::material_inline()
92 {
93         RefPtr<Material> mat=new Material;
94         load_sub(*mat);
95         coll->add(format("_%p", mat.get()), mat.get());
96         obj.material=mat.release();
97 }
98
99 void Technique::Loader::pass(const string &n)
100 {
101         Tag tag(n);
102         if(obj.passes.count(tag))
103                 throw KeyError("Duplicate pass name", n);
104         ObjectPass p;
105         load_sub(p, *coll);
106         obj.passes[tag]=p;
107 }
108
109 void Technique::Loader::shader(const string &n)
110 {
111         Program *shprog=coll->get<Program>(n);
112         if(shprog)  // Allow for unsupported shaders
113         {
114                 RefPtr<ProgramData> shdata=new ProgramData;
115                 load_sub(*shdata, *shprog);
116
117                 obj.normal_pass->shprog=shprog;
118                 if(obj.normal_pass->shdata)
119                         delete obj.normal_pass->shdata;
120                 obj.normal_pass->shdata=shdata.release();
121         }
122 }
123
124 void Technique::Loader::shader_texture(const string &n)
125 {
126         string::size_type eqsign=n.find('=');
127         TextureSlot tex;
128         if(eqsign!=string::npos)
129         {
130                 tex.name=n.substr(0, eqsign);
131                 tex.texture=coll->get<Texture>(n.substr(eqsign+1));
132         }
133         else
134         {
135                 string::size_type dot=n.rfind('.');
136                 tex.name=n.substr(0, dot);
137                 tex.texture = coll->get<Texture>(n);
138         }
139         for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
140                 if(!isalnum(*i))
141                         *i='_';
142         obj.textures.push_back(tex);
143 }
144
145 void Technique::Loader::texture(const string &n)
146 {
147         if(obj.main_texture)
148                 throw Exception("Only one main texture may be specified");
149
150         obj.main_texture=coll->get<Texture>(n);
151         TextureSlot tex;
152         tex.name="texture";
153         tex.texture=obj.main_texture;
154         obj.textures.push_back(tex);
155 }
156
157 void Technique::Loader::texture_slot(const string &n)
158 {
159         TextureSlot tex;
160         tex.name=n;
161         obj.textures.push_back(tex);
162 }
163
164 } // namespace GL
165 } // namespace Msp