X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftechnique.cpp;h=aa27d99cc7857118888fced1d59e1647347c2abf;hb=7b671e6899949d095698425a9b33387e7eb13894;hp=26837b61533167e0ddc268715f2722aee008272e;hpb=927a1aa0a3a27e463ec0efc08bd08e7c4e969909;p=libs%2Fgl.git diff --git a/source/technique.cpp b/source/technique.cpp index 26837b61..aa27d99c 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -1,13 +1,6 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include -#include +#include #include "material.h" #include "program.h" #include "programdata.h" @@ -20,16 +13,9 @@ 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(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; + return insert_unique(passes, tag, RenderPass())->second; } bool Technique::has_pass(const GL::Tag &tag) const @@ -37,128 +23,73 @@ 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()) - throw KeyError("Unknown pass"); - return i->second; + return get_item(passes, tag); } -unsigned Technique::get_texture_index(const std::string &n) const +bool Technique::has_shaders() const { - for(unsigned i=0; isecond.get_shader_program()) + return true; + return false; } -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): + DataFile::CollectionObjectLoader(t, 0) +{ + init(); } - Technique::Loader::Loader(Technique &t, Collection &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(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i) - if(i->second.shdata) - { - for(unsigned j=0; jsecond.shprog->get_uniform_location(obj.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()); - obj.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) { - Tag tag(n); - if(obj.passes.count(tag)) - throw KeyError("Duplicate pass name", n); - ObjectPass p; - load_sub(p, *coll); - obj.passes[tag]=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); + RenderPass p; + if(coll) + load_sub(p, get_collection()); + else + load_sub(p); - obj.normal_pass->shprog=shprog; - if(obj.normal_pass->shdata) - delete obj.normal_pass->shdata; - obj.normal_pass->shdata=shdata.release(); - } + insert_unique(obj.passes, n, p); } -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)); - } - 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='_'; - obj.textures.push_back(tex); -} -void Technique::Loader::texture(const string &n) +Technique::InheritLoader::InheritLoader(Technique &t, Collection &c): + DataFile::CollectionObjectLoader(t, &c) { - if(obj.main_texture) - throw Exception("Only one main texture may be specified"); - - obj.main_texture=coll->get(n); - TextureSlot tex; - tex.name="texture"; - tex.texture=obj.main_texture; - obj.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; - obj.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