3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/formatter.h>
11 #include "programdata.h"
13 #include "technique.h"
21 Technique::Technique():
23 normal_pass(&passes[0]),
27 Technique::~Technique()
29 for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
30 delete i->second.shdata;
33 bool Technique::has_pass(const GL::Tag &tag) const
35 return passes.count(tag.id);
38 const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
40 map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
42 throw KeyError("Unknown pass");
46 unsigned Technique::get_texture_index(const std::string &n) const
48 for(unsigned i=0; i<textures.size(); ++i)
49 if(textures[i].name==n)
52 throw KeyError("Unknown texture slot", n);
55 const Texture *Technique::get_texture(unsigned i) const
57 if(i>=textures.size())
58 throw KeyError("Texture index out of range");
60 return textures[i].texture;
64 Technique::Loader::Loader(Technique &t, Collection &c):
68 add("material", &Technique::material);
69 add("material_inline", &Loader::material_inline);
70 add("pass", &Loader::pass);
71 add("shader", &Loader::shader);
72 add("shader_texture", &Loader::shader_texture);
73 add("texture", &Loader::texture);
74 add("texture_slot", &Loader::texture_slot);
77 void Technique::Loader::finish()
79 for(map<unsigned, ObjectPass>::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i)
82 for(unsigned j=0; j<tech.textures.size(); ++j)
84 unsigned loc=i->second.shprog->get_uniform_location(tech.textures[j].name);
85 i->second.shdata->uniform(loc, static_cast<int>(j));
90 void Technique::Loader::material_inline()
92 RefPtr<Material> mat=new Material;
94 coll.add(format("_%p", mat.get()), mat.get());
95 tech.material=mat.release();
98 void Technique::Loader::pass(const string &n)
100 unsigned id=Tag(n).id;
101 if(tech.passes.count(id))
102 throw KeyError("Duplicate pass name", n);
108 void Technique::Loader::shader(const string &n)
110 Program *shprog=coll.get<Program>(n);
111 if(shprog) // Allow for unsupported shaders
113 RefPtr<ProgramData> shdata=new ProgramData;
114 load_sub(*shdata, *shprog);
116 tech.normal_pass->shprog=shprog;
117 if(tech.normal_pass->shdata)
118 delete tech.normal_pass->shdata;
119 tech.normal_pass->shdata=shdata.release();
123 void Technique::Loader::shader_texture(const string &n)
125 string::size_type eqsign=n.find('=');
127 if(eqsign!=string::npos)
129 tex.name=n.substr(0, eqsign);
130 tex.texture=coll.get<Texture>(n.substr(eqsign+1));
134 string::size_type dot=n.rfind('.');
135 tex.name=n.substr(0, dot);
136 tex.texture = coll.get<Texture>(n);
138 for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
141 tech.textures.push_back(tex);
144 void Technique::Loader::texture(const string &n)
146 if(tech.main_texture)
147 throw Exception("Only one main texture may be specified");
149 tech.main_texture=coll.get<Texture>(n);
152 tex.texture=tech.main_texture;
153 tech.textures.push_back(tex);
156 void Technique::Loader::texture_slot(const string &n)
160 tech.textures.push_back(tex);