X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frenderpass.cpp;h=93e2bc479b2d7197c3f9f8fe3bc59703c04d3579;hb=936a307eaf8dafd0874c74d211e991bd81a74387;hp=06726086bffd9067c6c88332ca8db52c01efa12f;hpb=745b3030d7fa32c1f6be29548f978af640eb4021;p=libs%2Fgl.git diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 06726086..93e2bc47 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -13,104 +13,106 @@ Distributed under the LGPL #include "program.h" #include "programdata.h" #include "texture.h" +#include "texture2d.h" using namespace std; namespace Msp { namespace GL { -const RenderPass *RenderPass::current=0; - RenderPass::RenderPass(): shprog(0), shdata(0), - own_material(false), material(0) { } RenderPass::RenderPass(const RenderPass &other): + Bindable(other), shprog(other.shprog), shdata(other.shdata ? new ProgramData(*other.shdata) : 0), - own_material(other.own_material), - material(own_material ? new Material(*other.material) : other.material), + material(other.material), textures(other.textures) { } RenderPass::~RenderPass() { delete shdata; - if(own_material) - delete material; } void RenderPass::set_material(const Material *mat) { - material=mat; + material = mat; } -unsigned RenderPass::get_texture_index(const string &slot) const +void RenderPass::set_texture(unsigned index, const Texture *tex) { - for(unsigned i=0; i::iterator i=textures.begin(); i!=textures.end(); ++i) + if(i->index==index) + { + i->texture = tex; + i->texture.keep(); + return; + } -void RenderPass::set_texture(const string &slot, const Texture *tex) -{ - textures[get_texture_index(slot)]=tex; + throw KeyError("No texture slot for that unit", lexical_cast(index)); } void RenderPass::bind() const { - if(this==current) + const RenderPass *old = current(); + if(!set_current(this)) return; - const RenderPass *old=current; - current=this; - if(shprog) { shprog->bind(); shdata->apply(); } - else if(old && !old->shprog) + else if(old && old->shprog) GL::Program::unbind(); if(material) material->bind(); - else if(old && !old->material) + else if(old && old->material) GL::Material::unbind(); - for(unsigned i=0; ibind_to(i); + 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(unsigned i=textures.size(); itextures.size(); ++i) - GL::Texture::unbind_from(i); + 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() { - if(current) - { - if(current->shprog) - GL::Program::unbind(); + const RenderPass *old = current(); + if(!set_current(0)) + return; - if(current->material) - GL::Material::unbind(); + if(old->shprog) + GL::Program::unbind(); - for(unsigned i=current->textures.size(); i--; ) - GL::Texture::unbind_from(i); + if(old->material) + GL::Material::unbind(); - current=0; - } + for(unsigned i=old->textures.size(); i--; ) + GL::Texture::unbind_from(i); } +RenderPass::TextureSlot::TextureSlot(unsigned i): + index(i), + texture(0) +{ } + + RenderPass::Loader::Loader(RenderPass &p): DataFile::CollectionObjectLoader(p, 0) { @@ -125,46 +127,38 @@ RenderPass::Loader::Loader(RenderPass &p, Collection &c): void RenderPass::Loader::init() { - allow_pointer_reload=false; + allow_pointer_reload = false; add("shader", &RenderPass::shprog); - add("material", &Loader::material); - add("material", &RenderPass::material); - add("texture", &Loader::texture); + add("material", static_cast(&Loader::material)); + add("material", static_cast(&Loader::material)); + add("texunit", &Loader::texunit); add("uniforms", &Loader::uniforms); } void RenderPass::Loader::finish() { - if(obj.shprog) - { - if(!obj.shdata) - obj.shdata=new ProgramData; - - for(unsigned i=0; iget_uniform_location(obj.textures[i].name); - obj.shdata->uniform(loc, static_cast(i)); - } - } + // XXX Make shdata optional + if(obj.shprog && !obj.shdata) + obj.shdata = new ProgramData; } void RenderPass::Loader::material() { - if(obj.material) - throw InvalidState("A material is already loaded"); - - RefPtr mat=new Material; + RefPtr mat = new Material; load_sub(*mat); - obj.material=mat.release(); - obj.own_material=true; + obj.material = mat; +} + +void RenderPass::Loader::material(const string &name) +{ + obj.material = get_collection().get(name); + obj.material.keep(); } -void RenderPass::Loader::texture(const string &n) +void RenderPass::Loader::texunit(unsigned i) { - const Texture *tex=(n.empty() ? 0 : get_collection().get(n)); - TextureSlot slot(tex); - slot.name=(obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size())); + TextureSlot slot(i); load_sub(slot); obj.textures.push_back(slot); } @@ -174,20 +168,40 @@ void RenderPass::Loader::uniforms() if(!obj.shprog) throw InvalidState("Can't load uniforms without a shader program"); if(!obj.shdata) - obj.shdata=new ProgramData; + obj.shdata = new ProgramData; load_sub(*obj.shdata, *obj.shprog); } -RenderPass::TextureSlot::TextureSlot(const Texture *t): - texture(t) -{ } +RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): + DataFile::CollectionObjectLoader(s, 0) +{ + init(); +} +RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c): + DataFile::CollectionObjectLoader(s, &c) +{ + init(); +} -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): - DataFile::ObjectLoader(s) +void RenderPass::TextureSlot::Loader::init() +{ + add("texture", &Loader::texture); + add("texture2d", &Loader::texture2d); +} + +void RenderPass::TextureSlot::Loader::texture(const string &name) +{ + obj.texture = get_collection().get(name); + obj.texture.keep(); +} + +void RenderPass::TextureSlot::Loader::texture2d() { - add("name", &TextureSlot::name); + RefPtr tex = new Texture2D; + load_sub(*tex); + obj.texture = tex; } } // namespace GL