X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftechnique.cpp;h=d963bea46c60f0d386439eac8ba6fb9ef8a967d3;hp=53c7a9ae76cacdc3ba68b05bd54db5000de452e2;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=3959bf80b361fd1e84bebe22e58f5875e1b68afb diff --git a/source/technique.cpp b/source/technique.cpp index 53c7a9ae..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,146 +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].texture; + 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.shprog->get_uniform_location(tech.textures[j].name); - i->second.shdata->uniform(loc, 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('='); - TextureSlot tex; - if(eqsign!=string::npos) - { - tex.name=n.substr(0, eqsign); - tex.texture=coll.get(n.substr(eqsign+1)); - } + RenderPass p; + if(coll) + load_sub(p, get_collection()); else - { - string::size_type dot=n.rfind('.'); - tex.name=n.substr(0, dot); - tex.texture = coll.get(n); - } - for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i) - if(!isalnum(*i)) - *i='_'; - tech.textures.push_back(tex); + load_sub(p); + obj.passes.insert(PassMap::value_type(tag, p)); } -void Technique::Loader::texture(const string &n) + +Technique::InheritLoader::InheritLoader(Technique &t, Collection &c): + DataFile::CollectionObjectLoader(t, &c) { - if(tech.main_texture) - throw Exception("Only one main texture may be specified"); - - tech.main_texture=coll.get(n); - TextureSlot tex; - tex.name="texture"; - tex.texture=tech.main_texture; - tech.textures.push_back(tex); + add("texture", &InheritLoader::texture); } -void Technique::Loader::texture_slot(const string &n) +void Technique::InheritLoader::texture(const std::string &slot, const string &name) { - TextureSlot tex; - tex.name=n; - tech.textures.push_back(tex); + 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