]> git.tdb.fi Git - libs/gl.git/blobdiff - source/technique.cpp
Allow copying of Uniforms and ProgramData
[libs/gl.git] / source / technique.cpp
index 26837b61533167e0ddc268715f2722aee008272e..78060e01f9b89e80e28f7171fc40cea23188d327 100644 (file)
@@ -20,24 +20,12 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Technique::Technique():
-       main_texture(0),
-       normal_pass(&passes[0]),
-       material(0)
-{ }
-
-Technique::~Technique()
-{
-       for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
-               delete i->second.shdata;
-}
-
 bool Technique::has_pass(const GL::Tag &tag) const
 {
        return passes.count(tag);
 }
 
-const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
+const RenderPass &Technique::get_pass(const GL::Tag &tag) const
 {
        PassMap::const_iterator i=passes.find(tag);
        if(i==passes.end())
@@ -45,55 +33,19 @@ const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
        return i->second;
 }
 
-unsigned Technique::get_texture_index(const std::string &n) const
-{
-       for(unsigned i=0; i<textures.size(); ++i)
-               if(textures[i].name==n)
-                       return i;
-
-       throw KeyError("Unknown texture slot", n);
-}
-
-const Texture *Technique::get_texture(unsigned i) const
-{
-       if(i>=textures.size())
-               throw KeyError("Texture index out of range");
-
-       return textures[i].texture;
-}
-
 
 Technique::Loader::Loader(Technique &t, Collection &c):
        DataFile::CollectionObjectLoader<Technique>(t, &c)
 {
-       add("material",        &Technique::material);
-       add("material_inline", &Loader::material_inline);
-       add("pass",            &Loader::pass);
-       add("shader",          &Loader::shader);
-       add("shader_texture",  &Loader::shader_texture);
-       add("texture",         &Loader::texture);
-       add("texture_slot",    &Loader::texture_slot);
-}
-
-void Technique::Loader::finish()
-{
-       for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
-               if(i->second.shdata)
-               {
-                       for(unsigned j=0; j<obj.textures.size(); ++j)
-                       {
-                               unsigned loc=i->second.shprog->get_uniform_location(obj.textures[j].name);
-                               i->second.shdata->uniform(loc, static_cast<int>(j));
-                       }
-               }
+       add("inherit", &Loader::inherit);
+       add("pass", &Loader::pass);
 }
 
-void Technique::Loader::material_inline()
+void Technique::Loader::inherit(const string &n)
 {
-       RefPtr<Material> mat=new Material;
-       load_sub(*mat);
-       coll->add(format("_%p", mat.get()), mat.get());
-       obj.material=mat.release();
+       obj.passes=get_collection().get<Technique>(n)->get_passes();
+       InheritLoader ldr(obj, get_collection());
+       load_sub_with(ldr);
 }
 
 void Technique::Loader::pass(const string &n)
@@ -101,64 +53,31 @@ void Technique::Loader::pass(const string &n)
        Tag tag(n);
        if(obj.passes.count(tag))
                throw KeyError("Duplicate pass name", n);
-       ObjectPass p;
+
+       RenderPass p;
        load_sub(p, *coll);
-       obj.passes[tag]=p;
+       obj.passes.insert(PassMap::value_type(tag, p));
 }
 
-void Technique::Loader::shader(const string &n)
-{
-       Program *shprog=coll->get<Program>(n);
-       if(shprog)  // Allow for unsupported shaders
-       {
-               RefPtr<ProgramData> shdata=new ProgramData;
-               load_sub(*shdata, *shprog);
 
-               obj.normal_pass->shprog=shprog;
-               if(obj.normal_pass->shdata)
-                       delete obj.normal_pass->shdata;
-               obj.normal_pass->shdata=shdata.release();
-       }
+Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
+       DataFile::CollectionObjectLoader<Technique>(t, &c)
+{
+       add("texture", &InheritLoader::texture);
 }
 
-void Technique::Loader::shader_texture(const string &n)
+void Technique::InheritLoader::texture(const string &slot, const string &name)
 {
-       string::size_type eqsign=n.find('=');
-       TextureSlot tex;
-       if(eqsign!=string::npos)
-       {
-               tex.name=n.substr(0, eqsign);
-               tex.texture=coll->get<Texture>(n.substr(eqsign+1));
-       }
-       else
+       Texture *tex=get_collection().get<Texture>(name);
+       for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
        {
-               string::size_type dot=n.rfind('.');
-               tex.name=n.substr(0, dot);
-               tex.texture = coll->get<Texture>(n);
+               try
+               {
+                       i->second.set_texture(slot, tex);
+               }
+               catch(const KeyError &)
+               { }
        }
-       for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
-               if(!isalnum(*i))
-                       *i='_';
-       obj.textures.push_back(tex);
-}
-
-void Technique::Loader::texture(const string &n)
-{
-       if(obj.main_texture)
-               throw Exception("Only one main texture may be specified");
-
-       obj.main_texture=coll->get<Texture>(n);
-       TextureSlot tex;
-       tex.name="texture";
-       tex.texture=obj.main_texture;
-       obj.textures.push_back(tex);
-}
-
-void Technique::Loader::texture_slot(const string &n)
-{
-       TextureSlot tex;
-       tex.name=n;
-       obj.textures.push_back(tex);
 }
 
 } // namespace GL