3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/collection.h>
10 #include <msp/strings/formatter.h>
13 #include "programdata.h"
15 #include "technique.h"
23 Technique::Technique():
25 normal_pass(&passes[0]),
29 Technique::~Technique()
31 for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
32 delete i->second.shdata;
35 bool Technique::has_pass(const GL::Tag &tag) const
37 return passes.count(tag);
40 const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
42 PassMap::const_iterator i=passes.find(tag);
44 throw KeyError("Unknown pass");
48 unsigned Technique::get_texture_index(const std::string &n) const
50 for(unsigned i=0; i<textures.size(); ++i)
51 if(textures[i].name==n)
54 throw KeyError("Unknown texture slot", n);
57 const Texture *Technique::get_texture(unsigned i) const
59 if(i>=textures.size())
60 throw KeyError("Texture index out of range");
62 return textures[i].texture;
66 Technique::Loader::Loader(Technique &t, Collection &c):
67 DataFile::CollectionObjectLoader<Technique>(t, &c)
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);
78 void Technique::Loader::finish()
80 for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
83 for(unsigned j=0; j<obj.textures.size(); ++j)
85 unsigned loc=i->second.shprog->get_uniform_location(obj.textures[j].name);
86 i->second.shdata->uniform(loc, static_cast<int>(j));
91 void Technique::Loader::material_inline()
93 RefPtr<Material> mat=new Material;
95 coll->add(format("_%p", mat.get()), mat.get());
96 obj.material=mat.release();
99 void Technique::Loader::pass(const string &n)
102 if(obj.passes.count(tag))
103 throw KeyError("Duplicate pass name", n);
109 void Technique::Loader::shader(const string &n)
111 Program *shprog=coll->get<Program>(n);
112 if(shprog) // Allow for unsupported shaders
114 RefPtr<ProgramData> shdata=new ProgramData;
115 load_sub(*shdata, *shprog);
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();
124 void Technique::Loader::shader_texture(const string &n)
126 string::size_type eqsign=n.find('=');
128 if(eqsign!=string::npos)
130 tex.name=n.substr(0, eqsign);
131 tex.texture=coll->get<Texture>(n.substr(eqsign+1));
135 string::size_type dot=n.rfind('.');
136 tex.name=n.substr(0, dot);
137 tex.texture = coll->get<Texture>(n);
139 for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
142 obj.textures.push_back(tex);
145 void Technique::Loader::texture(const string &n)
148 throw Exception("Only one main texture may be specified");
150 obj.main_texture=coll->get<Texture>(n);
153 tex.texture=obj.main_texture;
154 obj.textures.push_back(tex);
157 void Technique::Loader::texture_slot(const string &n)
161 obj.textures.push_back(tex);