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<tex_names.size(); ++i)
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");
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)
83 i->second.shdata->uniform(i->second.shprog->get_uniform_location(tech.tex_names[j]), static_cast<int>(j));
87 void Technique::Loader::material_inline()
89 RefPtr<Material> mat=new Material;
91 coll.add(format("_%p", mat.get()), mat.get());
92 tech.material=mat.release();
95 void Technique::Loader::pass(const string &n)
97 unsigned id=Tag(n).id;
98 if(tech.passes.count(id))
99 throw KeyError("Duplicate pass name", n);
105 void Technique::Loader::shader(const string &n)
107 Program *shprog=coll.get<Program>(n);
108 if(shprog) // Allow for unsupported shaders
110 RefPtr<ProgramData> shdata=new ProgramData;
111 load_sub(*shdata, *shprog);
113 tech.normal_pass->shprog=shprog;
114 if(tech.normal_pass->shdata)
115 delete tech.normal_pass->shdata;
116 tech.normal_pass->shdata=shdata.release();
120 void Technique::Loader::shader_texture(const string &n)
122 unsigned eqsign=n.find('=');
123 if(eqsign!=string::npos)
125 tech.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
126 tech.tex_names.push_back(n.substr(0, eqsign));
130 tech.textures.push_back(coll.get<Texture>(n));
131 tech.tex_names.push_back(n);
135 void Technique::Loader::texture(const string &n)
137 if(tech.main_texture)
138 throw Exception("Only one main texture may be specified");
140 tech.main_texture=coll.get<Texture>(n);
141 tech.textures.push_back(tech.main_texture);
142 tech.tex_names.push_back("texture");
145 void Technique::Loader::texture_slot(const string &n)
147 tech.tex_names.push_back(n);
148 tech.textures.push_back(0);