X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frenderpass.cpp;h=eb4ae1f93ba145b029ff5a23847b02e557f56c37;hp=18c318b53b8d2ccddb81481411350e24beb9a0ac;hb=42c1534d95e1551c37e64a1dae288e8b75e8d8ba;hpb=09e886603fbc255f6a6241641ff42c466f8387ff diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 18c318b5..eb4ae1f9 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -1,19 +1,14 @@ -/* $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 "renderer.h" #include "texture.h" #include "texture2d.h" +#include "texturing.h" using namespace std; @@ -23,93 +18,84 @@ namespace GL { RenderPass::RenderPass(): shprog(0), shdata(0), - material(0) + material(0), + texturing(0), + back_faces(false) { } RenderPass::RenderPass(const RenderPass &other): shprog(other.shprog), - shdata(other.shdata ? new ProgramData(*other.shdata) : 0), + shdata(other.shdata), + uniform_slots(other.uniform_slots), material(other.material), - textures(other.textures) + texturing(other.texturing ? new Texturing(*other.texturing) : 0), + tex_names(other.tex_names), + back_faces(other.back_faces) { } -RenderPass::~RenderPass() +RenderPass &RenderPass::operator=(const RenderPass &other) { - delete shdata; + shprog = other.shprog; + shdata = other.shdata; + uniform_slots = other.uniform_slots; + material = other.material; + texturing = other.texturing ? new Texturing(*other.texturing) : 0; + tex_names = other.tex_names; + back_faces = other.back_faces; + return *this; } -void RenderPass::set_material(const Material *mat) +RenderPass::~RenderPass() { - material = mat; + delete texturing; } -void RenderPass::set_texture(unsigned index, const Texture *tex) +void RenderPass::set_shader_program(const Program *prog, const ProgramData *data) { - for(vector::iterator i=textures.begin(); i!=textures.end(); ++i) - if(i->index==index) - { - i->texture = tex; - i->texture.keep(); - return; - } - - throw KeyError("No texture slot for that unit", lexical_cast(index)); + shprog = prog; + shdata = new ProgramData(*data); } -void RenderPass::bind() const +const string &RenderPass::get_slotted_uniform_name(const string &slot) 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) + map::const_iterator i = uniform_slots.find(slot); + if(i==uniform_slots.end()) { - for(vector::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i) - if(!used_tex_units&(1<index)) - Texture::unbind_from(i->index); + static string empty; + return empty; } + return i->second; } -void RenderPass::unbind() +void RenderPass::set_material(const Material *mat) { - const RenderPass *old = current(); - if(!set_current(0)) - return; - - if(old->shprog) - GL::Program::unbind(); + material = mat; + material.keep(); +} - if(old->material) - GL::Material::unbind(); +void RenderPass::set_texture(unsigned index, const Texture *tex) +{ + if(!texturing) + texturing = new Texturing; - for(unsigned i=old->textures.size(); i--; ) - GL::Texture::unbind_from(i); + texturing->attach(index, *tex); } +int RenderPass::get_texture_index(const string &n) const +{ + 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) -{ } +void RenderPass::apply(Renderer &renderer) const +{ + renderer.set_texturing(texturing); + renderer.set_material(material.get()); + renderer.set_shader_program(shprog, shdata.get()); + renderer.set_reverse_winding(back_faces); +} RenderPass::Loader::Loader(RenderPass &p): @@ -126,81 +112,97 @@ RenderPass::Loader::Loader(RenderPass &p, Collection &c): 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("material_slot", &RenderPass::material_slot); + add("back_faces",&RenderPass::back_faces); add("texunit", &Loader::texunit); + add("texunit", &Loader::texunit_named); add("uniforms", &Loader::uniforms); + add("uniform_slot", &Loader::uniform_slot); } -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); + if(coll) + load_sub(*mat, get_collection()); + else + load_sub(*mat); obj.material = mat; } void RenderPass::Loader::material(const string &name) { - obj.material = get_collection().get(name); + obj.material = &get_collection().get(name); obj.material.keep(); } 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() { 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); + throw invalid_operation("RenderPass::Loader::uniforms"); + RefPtr shd = new ProgramData(obj.shprog); + load_sub(*shd); + obj.shdata = shd.release(); } +void RenderPass::Loader::uniform_slot(const string &name) +{ + uniform_slot2(name, name); +} -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s): - DataFile::CollectionObjectLoader(s, 0) +void RenderPass::Loader::uniform_slot2(const string &name, const string &slot) { - init(); + obj.uniform_slots[slot] = name; } -RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c): - DataFile::CollectionObjectLoader(s, &c) + +RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c): + DataFile::CollectionObjectLoader(t, c), + index(i) { - init(); + add("texture", &TextureLoader::texture); + add("texture2d", &TextureLoader::texture2d); } -void RenderPass::TextureSlot::Loader::init() +void RenderPass::TextureLoader::finish() { - add("texture", &Loader::texture); - add("texture2d", &Loader::texture2d); + if(tex) + { + obj.attach(index, *tex); + tex.release(); + } } -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; + if(coll) + load_sub(static_cast(*tex), get_collection()); + else + load_sub(static_cast(*tex)); } } // namespace GL