X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftechnique.cpp;h=d963bea46c60f0d386439eac8ba6fb9ef8a967d3;hp=74cd6c311be0699e10760922452c702507458e58;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=e20075bffbbe1f3aaa9cc149953525a7e855f496 diff --git a/source/technique.cpp b/source/technique.cpp index 74cd6c31..d963bea4 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -1,10 +1,5 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include +#include #include #include "material.h" #include "program.h" @@ -18,134 +13,84 @@ using namespace std; namespace Msp { namespace GL { -Technique::Technique(): - main_texture(0), - normal_pass(&passes[0]), - material(0) -{ } - -Technique::~Technique() +RenderPass &Technique::add_pass(const GL::Tag &tag) { - for(map::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; + if(passes.count(tag)) + throw KeyError("Duplicate pass"); + + return passes[tag]; } bool Technique::has_pass(const GL::Tag &tag) const { - return passes.count(tag.id); + return passes.count(tag); } -const ObjectPass &Technique::get_pass(const GL::Tag &tag) const +const RenderPass &Technique::get_pass(const GL::Tag &tag) const { - map::const_iterator i=passes.find(tag.id); + PassMap::const_iterator i = passes.find(tag); if(i==passes.end()) throw KeyError("Unknown pass"); return i->second; } -unsigned Technique::get_texture_index(const std::string &n) const -{ - for(unsigned i=0; i(t, 0) { - if(i>=textures.size()) - throw KeyError("Texture index out of range"); - - return textures[i]; + init(); } - Technique::Loader::Loader(Technique &t, Collection &c): - tech(t), - coll(c) + DataFile::CollectionObjectLoader(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); + init(); } -void Technique::Loader::finish() +void Technique::Loader::init() { - for(map::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i) - if(i->second.shdata) - { - for(unsigned j=0; jsecond.shdata->uniform(i->second.shprog->get_uniform_location(tech.tex_names[j]), static_cast(j)); - } + add("inherit", &Loader::inherit); + add("pass", &Loader::pass); } -void Technique::Loader::material_inline() +void Technique::Loader::inherit(const string &n) { - RefPtr mat=new Material; - load_sub(*mat); - coll.add(format("_%p", mat.get()), mat.get()); - tech.material=mat.release(); + obj.passes = get_collection().get(n)->get_passes(); + InheritLoader ldr(obj, get_collection()); + load_sub_with(ldr); } void Technique::Loader::pass(const string &n) { - unsigned id=Tag(n).id; - if(tech.passes.count(id)) + Tag tag(n); + if(obj.passes.count(tag)) throw KeyError("Duplicate pass name", n); - ObjectPass p; - load_sub(p, coll); - tech.passes[id]=p; -} - -void Technique::Loader::shader(const string &n) -{ - Program *shprog=coll.get(n); - if(shprog) // Allow for unsupported shaders - { - RefPtr shdata=new ProgramData; - load_sub(*shdata, *shprog); - tech.normal_pass->shprog=shprog; - if(tech.normal_pass->shdata) - delete tech.normal_pass->shdata; - tech.normal_pass->shdata=shdata.release(); - } -} - -void Technique::Loader::shader_texture(const string &n) -{ - string::size_type eqsign=n.find('='); - if(eqsign!=string::npos) - { - tech.textures.push_back(coll.get(n.substr(eqsign+1))); - tech.tex_names.push_back(n.substr(0, eqsign)); - } + RenderPass p; + if(coll) + load_sub(p, get_collection()); else - { - tech.textures.push_back(coll.get(n)); - tech.tex_names.push_back(n); - } + load_sub(p); + obj.passes.insert(PassMap::value_type(tag, p)); } -void Technique::Loader::texture(const string &n) -{ - if(tech.main_texture) - throw Exception("Only one main texture may be specified"); - tech.main_texture=coll.get(n); - tech.textures.push_back(tech.main_texture); - tech.tex_names.push_back("texture"); +Technique::InheritLoader::InheritLoader(Technique &t, Collection &c): + DataFile::CollectionObjectLoader(t, &c) +{ + add("texture", &InheritLoader::texture); } -void Technique::Loader::texture_slot(const string &n) +void Technique::InheritLoader::texture(const std::string &slot, const string &name) { - tech.tex_names.push_back(n); - tech.textures.push_back(0); + Texture *tex = get_collection().get(name); + for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i) + { + int index = i->second.get_texture_index(slot); + if(index<0) + continue; + i->second.set_texture(index, tex); + } } } // namespace GL