X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frenderpass.cpp;h=4ab07347824ef68765a72becd9e35f9a1151fe28;hp=b9a2cb83a05a09cbaab1ec3f6ea674a1fec707ac;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=2f09d68a0844d2838d116d93d3ecc69723b52f16 diff --git a/source/renderpass.cpp b/source/renderpass.cpp index b9a2cb83..4ab07347 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include #include #include @@ -12,8 +5,10 @@ Distributed under the LGPL #include "renderpass.h" #include "program.h" #include "programdata.h" +#include "texenv.h" #include "texture.h" #include "texture2d.h" +#include "texturing.h" using namespace std; @@ -23,20 +18,22 @@ namespace GL { RenderPass::RenderPass(): shprog(0), shdata(0), - material(0) + material(0), + texturing(0) { } RenderPass::RenderPass(const RenderPass &other): - Bindable(other), shprog(other.shprog), shdata(other.shdata ? new ProgramData(*other.shdata) : 0), material(other.material), - textures(other.textures) + texturing(other.texturing ? new Texturing(*other.texturing) : 0), + tex_names(other.tex_names) { } RenderPass::~RenderPass() { delete shdata; + delete texturing; } void RenderPass::set_material(const Material *mat) @@ -47,75 +44,21 @@ void RenderPass::set_material(const Material *mat) void RenderPass::set_texture(unsigned index, const Texture *tex) { - for(vector::iterator i=textures.begin(); i!=textures.end(); ++i) - if(i->index==index) - { - i->texture = tex; - i->texture.keep(); - return; - } + if(!texturing) + texturing = new Texturing; - textures.push_back(TextureSlot(index)); - textures.back().texture = tex; - textures.back().texture.keep(); + texturing->attach(index, *tex); } -void RenderPass::bind() const +int RenderPass::get_texture_index(const string &n) const { - const RenderPass *old = current(); - if(!set_current(this)) - return; - - if(shprog) - { - shprog->bind(); - shdata->apply(); - } - else if(old && old->shprog) - GL::Program::unbind(); - - if(material) - material->bind(); - else if(old && old->material) - GL::Material::unbind(); - - unsigned used_tex_units = 0; - for(vector::const_iterator i=textures.begin(); i!=textures.end(); ++i) - { - i->texture->bind_to(i->index); - used_tex_units |= 1<index; - } - if(old) - { - for(vector::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i) - if(!used_tex_units&(1<index)) - Texture::unbind_from(i->index); - } -} - -void RenderPass::unbind() -{ - const RenderPass *old = current(); - if(!set_current(0)) - return; - - if(old->shprog) - GL::Program::unbind(); - - if(old->material) - GL::Material::unbind(); - - for(unsigned i=old->textures.size(); i--; ) - GL::Texture::unbind_from(i); + map::const_iterator i = tex_names.find(n); + if(i==tex_names.end()) + return -1; + return i->second; } -RenderPass::TextureSlot::TextureSlot(unsigned i): - index(i), - texture(0) -{ } - - RenderPass::Loader::Loader(RenderPass &p): DataFile::CollectionObjectLoader(p, 0) { @@ -133,20 +76,14 @@ void RenderPass::Loader::init() allow_pointer_reload = false; add("shader", &RenderPass::shprog); - add("material", static_cast(&Loader::material)); - add("material", static_cast(&Loader::material)); + add("material", &Loader::material_inline); + add("material", &Loader::material); add("texunit", &Loader::texunit); + add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); } -void RenderPass::Loader::finish() -{ - // XXX Make shdata optional - if(obj.shprog && !obj.shdata) - obj.shdata = new ProgramData; -} - -void RenderPass::Loader::material() +void RenderPass::Loader::material_inline() { RefPtr mat = new Material; load_sub(*mat); @@ -161,9 +98,16 @@ void RenderPass::Loader::material(const string &name) void RenderPass::Loader::texunit(unsigned i) { - TextureSlot slot(i); - load_sub(slot); - obj.textures.push_back(slot); + if(!obj.texturing) + obj.texturing = new Texturing; + TextureLoader ldr(*obj.texturing, i, coll); + load_sub_with(ldr); +} + +void RenderPass::Loader::texunit_named(unsigned i, const string &n) +{ + texunit(i); + obj.tex_names[n] = i; } void RenderPass::Loader::uniforms() @@ -171,40 +115,49 @@ void RenderPass::Loader::uniforms() if(!obj.shprog) throw InvalidState("Can't load uniforms without a shader program"); if(!obj.shdata) - obj.shdata = new ProgramData; - load_sub(*obj.shdata, *obj.shprog); + obj.shdata = new ProgramData(*obj.shprog); + load_sub(*obj.shdata); } -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): - DataFile::CollectionObjectLoader(s, 0) +RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c): + DataFile::CollectionObjectLoader(t, c), + index(i) { - init(); + add("texture", &TextureLoader::texture); + add("texture2d", &TextureLoader::texture2d); } -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c): - DataFile::CollectionObjectLoader(s, &c) +void RenderPass::TextureLoader::finish() { - init(); + if(tex) + { + if(env) + obj.attach(index, *tex, *env); + else + obj.attach(index, *tex); + tex.release(); + env.release(); + } } -void RenderPass::TextureSlot::Loader::init() +void RenderPass::TextureLoader::texenv() { - add("texture", &Loader::texture); - add("texture2d", &Loader::texture2d); + throw Exception("TexEnvs can't be loaded yet"); + /*env = new TexEnv; + load_sub(*env);*/ } -void RenderPass::TextureSlot::Loader::texture(const string &name) +void RenderPass::TextureLoader::texture(const string &name) { - obj.texture = get_collection().get(name); - obj.texture.keep(); + tex = get_collection().get(name); + tex.keep(); } -void RenderPass::TextureSlot::Loader::texture2d() +void RenderPass::TextureLoader::texture2d() { - RefPtr tex = new Texture2D; - load_sub(*tex); - obj.texture = tex; + tex = new Texture2D; + load_sub(static_cast(*tex)); } } // namespace GL