From 2f09d68a0844d2838d116d93d3ecc69723b52f16 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 7 Nov 2010 14:24:10 +0000 Subject: [PATCH 1/1] Use RefPtrs in Object Disallow gaps in Object's mesh array Add methods for dynamic creation of Objects, Techniques and RenderPasses --- source/object.cpp | 74 ++++++++++++++++++++++++++++--------------- source/object.h | 15 +++++---- source/renderpass.cpp | 5 ++- source/technique.cpp | 8 +++++ source/technique.h | 1 + 5 files changed, 70 insertions(+), 33 deletions(-) diff --git a/source/object.cpp b/source/object.cpp index 5ae04feb..c4787d9f 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -24,18 +24,29 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1), - technique(0), - own_mesh(false), - own_technique(false) + meshes(1) { } Object::~Object() { - if(own_technique) - delete technique; - if(own_mesh) - delete meshes[0]; +} + +void Object::set_mesh(unsigned i, const Mesh *m) +{ + if(i>meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); + + if(i==meshes.size()) + meshes.push_back(m); + else + meshes[i] = m; + meshes[i].keep(); +} + +void Object::set_technique(const Technique *t) +{ + technique = t; + technique.keep(); } void Object::render(const Tag &tag) const @@ -45,7 +56,7 @@ void Object::render(const Tag &tag) const return; Bind bind(pass); - meshes[0]->draw(); + meshes.front()->draw(); } void Object::render(const ObjectInstance &inst, const Tag &tag) const @@ -56,7 +67,6 @@ void Object::render(const ObjectInstance &inst, const Tag &tag) const Bind bind(pass); render_instance(inst, tag); - meshes[0]->draw(); } const RenderPass *Object::get_pass(const Tag &tag) const @@ -91,36 +101,44 @@ void Object::Loader::init() { allow_pointer_reload = false; - add("lod_mesh", &Loader::lod_mesh); add("mesh", static_cast(&Loader::mesh)); + add("mesh", static_cast(&Loader::mesh)); add("mesh", static_cast(&Loader::mesh)); - add("technique", &Loader::technique); - add("technique", &Object::technique); + add("mesh", static_cast(&Loader::mesh)); + // Deprecated alias, will be removed + add("lod_mesh", static_cast(&Loader::mesh)); + add("technique", static_cast(&Loader::technique)); + add("technique", static_cast(&Loader::technique)); } -void Object::Loader::lod_mesh(unsigned l, const string &n) +void Object::Loader::mesh() { - obj.meshes.resize(l+1, 0); - obj.meshes[l] = get_collection().get(n); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes.front() = msh; } -void Object::Loader::mesh() +void Object::Loader::mesh(unsigned l) { - if(obj.meshes[0]) - throw InvalidState("A mesh is already loaded"); + if(l>obj.meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); RefPtr msh = new Mesh; load_sub(*msh); - obj.meshes[0] = msh.release(); - obj.own_mesh = true; + if(l==obj.meshes.size()) + obj.meshes.push_back(msh); + else + obj.meshes[l] = msh; } void Object::Loader::mesh(const std::string &n) { - if(obj.meshes[0]) - throw InvalidState("A mesh is already loaded"); + obj.set_mesh(get_collection().get(n)); +} - obj.meshes[0] = get_collection().get(n); +void Object::Loader::mesh(unsigned l, const string &n) +{ + obj.set_mesh(l, get_collection().get(n)); } void Object::Loader::technique() @@ -130,8 +148,12 @@ void Object::Loader::technique() load_sub(*tech, get_collection()); else load_sub(*tech); - obj.technique = tech.release(); - obj.own_technique = true; + obj.technique = tech; +} + +void Object::Loader::technique(const std::string &n) +{ + obj.set_technique(get_collection().get(n)); } } // namespace GL diff --git a/source/object.h b/source/object.h index 2b5f4a85..4655695b 100644 --- a/source/object.h +++ b/source/object.h @@ -31,10 +31,8 @@ similar objects. See class ObjectInstance. class Object: public Renderable { private: - std::vector meshes; - Technique *technique; - bool own_mesh:1; - bool own_technique:1; + std::vector > meshes; + RefPtr technique; public: class Loader: public DataFile::CollectionObjectLoader @@ -46,16 +44,21 @@ public: void init(); private: - void lod_mesh(unsigned, const std::string &); void mesh(); + void mesh(unsigned); void mesh(const std::string &); + void mesh(unsigned, const std::string &); void technique(); + void technique(const std::string &); }; Object(); ~Object(); - const Technique *get_technique() const { return technique; } + void set_mesh(const Mesh *m) { set_mesh(0, m); } + void set_mesh(unsigned, const Mesh *); + void set_technique(const Technique *); + const Technique *get_technique() const { return technique.get(); } /** Renders the object. A tag can be provided to render a non-default pass. diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 93e2bc47..b9a2cb83 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -42,6 +42,7 @@ RenderPass::~RenderPass() void RenderPass::set_material(const Material *mat) { material = mat; + material.keep(); } void RenderPass::set_texture(unsigned index, const Texture *tex) @@ -54,7 +55,9 @@ void RenderPass::set_texture(unsigned index, const Texture *tex) return; } - throw KeyError("No texture slot for that unit", lexical_cast(index)); + textures.push_back(TextureSlot(index)); + textures.back().texture = tex; + textures.back().texture.keep(); } void RenderPass::bind() const diff --git a/source/technique.cpp b/source/technique.cpp index 9cbb005a..9d13a897 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -20,6 +20,14 @@ using namespace std; namespace Msp { namespace GL { +RenderPass &Technique::add_pass(const GL::Tag &tag) +{ + if(passes.count(tag)) + throw KeyError("Duplicate pass"); + + return passes[tag]; +} + bool Technique::has_pass(const GL::Tag &tag) const { return passes.count(tag); diff --git a/source/technique.h b/source/technique.h index 80da88f7..608af19d 100644 --- a/source/technique.h +++ b/source/technique.h @@ -49,6 +49,7 @@ private: PassMap passes; public: + RenderPass &add_pass(const GL::Tag &); bool has_pass(const GL::Tag &) const; const RenderPass &get_pass(const GL::Tag &) const; const PassMap &get_passes() const { return passes; } -- 2.45.2