X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frenderpass.cpp;h=fc2d37ed1a72d6a6a46335f50b46db09bd96bd6a;hb=0fc02952ed449ff85f5f9e96ea2fc724c8456891;hp=d2fe668839816688fbab3d60414d2d092d15a7a8;hpb=97015ec7bddd26aa746f5227e4109b7d32438cca;p=libs%2Fgl.git diff --git a/source/renderpass.cpp b/source/renderpass.cpp index d2fe6688..fc2d37ed 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -1,191 +1,161 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include #include -#include +#include +#include "error.h" #include "material.h" #include "renderpass.h" #include "program.h" #include "programdata.h" +#include "texenv.h" #include "texture.h" +#include "texture2d.h" +#include "texturing.h" using namespace std; namespace Msp { namespace GL { -const RenderPass *RenderPass::current=0; - RenderPass::RenderPass(): shprog(0), shdata(0), - own_material(false), - material(0) + material(0), + texturing(0) { } RenderPass::RenderPass(const RenderPass &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), - textures(other.textures) + material(other.material), + texturing(other.texturing ? new Texturing(*other.texturing) : 0), + tex_names(other.tex_names) { } -/*RenderPass &RenderPass::operator=(const RenderPass &other) -{ - shprog=other.shprog; - delete shdata; - shdata=(other.shdata ? new ProgramData(*other.shdata) : 0); - material=other.material; - use_material=other.use_material; - textures=other.textures; - use_textures=other.use_textures; - - return *this; -}*/ - RenderPass::~RenderPass() { delete shdata; - if(own_material) - delete material; + delete texturing; } void RenderPass::set_material(const Material *mat) { - material=mat; + material = mat; + material.keep(); } -unsigned RenderPass::get_texture_index(const string &slot) const +void RenderPass::set_texture(unsigned index, const Texture *tex) { - for(unsigned i=0; iattach(index, *tex); } -void RenderPass::set_texture(const string &slot, const Texture *tex) +int RenderPass::get_texture_index(const string &n) const { - textures[get_texture_index(slot)]=tex; + map::const_iterator i = tex_names.find(n); + if(i==tex_names.end()) + return -1; + return i->second; } -void RenderPass::bind() const -{ - if(this==current) - return; - - const RenderPass *old=current; - current=this; - - 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(); - - for(unsigned i=0; ibind_to(i); - if(old) - { - for(unsigned i=textures.size(); itextures.size(); ++i) - GL::Texture::unbind_from(i); - } -} -void RenderPass::unbind() +RenderPass::Loader::Loader(RenderPass &p): + DataFile::CollectionObjectLoader(p, 0) { - if(current) - { - if(current->shprog) - GL::Program::unbind(); - - if(current->material) - GL::Material::unbind(); - - for(unsigned i=current->textures.size(); i--; ) - GL::Texture::unbind_from(i); - - current=0; - } + init(); } - RenderPass::Loader::Loader(RenderPass &p, Collection &c): DataFile::CollectionObjectLoader(p, &c) +{ + init(); +} + +void RenderPass::Loader::init() { add("shader", &RenderPass::shprog); + add("material", &Loader::material_inline); add("material", &Loader::material); - add("material", &RenderPass::material); - add("texture", &Loader::texture); + add("texunit", &Loader::texunit); + add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); } -void RenderPass::Loader::finish() +void RenderPass::Loader::material_inline() { - 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)); - } - } + RefPtr mat = new Material; + load_sub(*mat); + obj.material = mat; } -void RenderPass::Loader::material() +void RenderPass::Loader::material(const string &name) { - // XXX Potential memory management trouble with multiple material statements - RefPtr mat=new Material; - load_sub(*mat); - obj.material=mat.release(); - obj.own_material=true; + 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())); - 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() { if(!obj.shprog) - throw InvalidState("Can't load uniforms without a shader program"); + throw invalid_operation("RenderPass::Loader::uniforms"); 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::TextureSlot(const Texture *t): - texture(t) -{ } +RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c): + DataFile::CollectionObjectLoader(t, c), + index(i) +{ + add("texture", &TextureLoader::texture); + add("texture2d", &TextureLoader::texture2d); +} + +void RenderPass::TextureLoader::finish() +{ + if(tex) + { + if(env) + obj.attach(index, *tex, *env); + else + obj.attach(index, *tex); + tex.release(); + env.release(); + } +} +void RenderPass::TextureLoader::texenv() +{ + throw runtime_error("TexEnvs can't be loaded yet"); + /*env = new TexEnv; + load_sub(*env);*/ +} + +void RenderPass::TextureLoader::texture(const string &name) +{ + tex = &get_collection().get(name); + tex.keep(); +} -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): - DataFile::ObjectLoader(s) +void RenderPass::TextureLoader::texture2d() { - add("name", &TextureSlot::name); + tex = new Texture2D; + load_sub(static_cast(*tex)); } } // namespace GL