]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Use RefPtrs in Object
[libs/gl.git] / source / object.cpp
index edfe39e56008e580e85b292c7508395260de9e71..c4787d9fda34d68b4a3cee61b40a6fabf5f79052 100644 (file)
@@ -12,7 +12,6 @@ Distributed under the LGPL
 #include "mesh.h"
 #include "object.h"
 #include "objectinstance.h"
-#include "objectpass.h"
 #include "program.h"
 #include "programdata.h"
 #include "technique.h"
@@ -25,41 +24,54 @@ namespace Msp {
 namespace GL {
 
 Object::Object():
-       meshes(1),
-       own_technique(false),
-       technique(0)
+       meshes(1)
 { }
 
 Object::~Object()
 {
-       if(own_technique)
-               delete technique;
+}
+
+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
 {
-       const RenderPass *pass=get_pass(tag);
+       const RenderPass *pass = get_pass(tag);
        if(!pass)
                return;
 
-       Bind bind(*pass);
-       meshes[0]->draw();
+       Bind bind(pass);
+       meshes.front()->draw();
 }
 
 void Object::render(const ObjectInstance &inst, const Tag &tag) const
 {
-       const RenderPass *pass=get_pass(tag);
+       const RenderPass *pass = get_pass(tag);
        if(!pass)
                return;
 
-       Bind bind(*pass);
+       Bind bind(pass);
        render_instance(inst, tag);
-       meshes[0]->draw();
 }
 
 const RenderPass *Object::get_pass(const Tag &tag) const
 {
-       if(!technique->has_pass(tag))
+       if(!technique || !technique->has_pass(tag))
                return 0;
        return &technique->get_pass(tag);
 }
@@ -67,38 +79,81 @@ const RenderPass *Object::get_pass(const Tag &tag) const
 void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
 {
        inst.setup_render(tag);
-       unsigned lod=min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
+       unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
        meshes[lod]->draw();
        inst.finish_render(tag);
 }
 
 
+Object::Loader::Loader(Object &o):
+       DataFile::CollectionObjectLoader<Object>(o, 0)
+{
+       init();
+}
+
 Object::Loader::Loader(Object &o, Collection &c):
        DataFile::CollectionObjectLoader<Object>(o, &c)
 {
-       add("lod_mesh", &Loader::lod_mesh);
-       add("mesh",     &Loader::mesh);
-       add("technique", &Loader::technique);
-       add("technique", &Object::technique);
+       init();
 }
 
-void Object::Loader::lod_mesh(unsigned l, const string &n)
+void Object::Loader::init()
 {
-       obj.meshes.resize(l+1, 0);
-       obj.meshes[l]=get_collection().get<Mesh>(n);
+       allow_pointer_reload = false;
+
+       add("mesh",     static_cast<void (Loader::*)()>(&Loader::mesh));
+       add("mesh",     static_cast<void (Loader::*)(unsigned)>(&Loader::mesh));
+       add("mesh",     static_cast<void (Loader::*)(const std::string &)>(&Loader::mesh));
+       add("mesh",     static_cast<void (Loader::*)(unsigned, const std::string &)>(&Loader::mesh));
+       // Deprecated alias, will be removed
+       add("lod_mesh", static_cast<void (Loader::*)(unsigned, const std::string &)>(&Loader::mesh));
+       add("technique", static_cast<void (Loader::*)()>(&Loader::technique));
+       add("technique", static_cast<void (Loader::*)(const std::string &)>(&Loader::technique));
 }
 
-void Object::Loader::mesh(const string &n)
+void Object::Loader::mesh()
 {
-       obj.meshes[0]=get_collection().get<Mesh>(n);
+       RefPtr<Mesh> msh = new Mesh;
+       load_sub(*msh);
+       obj.meshes.front() = msh;
+}
+
+void Object::Loader::mesh(unsigned l)
+{
+       if(l>obj.meshes.size())
+               throw InvalidParameterValue("LODs must be continuous");
+
+       RefPtr<Mesh> msh = new Mesh;
+       load_sub(*msh);
+       if(l==obj.meshes.size())
+               obj.meshes.push_back(msh);
+       else
+               obj.meshes[l] = msh;
+}
+
+void Object::Loader::mesh(const std::string &n)
+{
+       obj.set_mesh(get_collection().get<Mesh>(n));
+}
+
+void Object::Loader::mesh(unsigned l, const string &n)
+{
+       obj.set_mesh(l, get_collection().get<Mesh>(n));
 }
 
 void Object::Loader::technique()
 {
-       RefPtr<Technique> tch=new Technique;
-       load_sub(*tch, get_collection());
-       obj.technique=tch.release();
-       obj.own_technique=true;
+       RefPtr<Technique> tech = new Technique;
+       if(coll)
+               load_sub(*tech, get_collection());
+       else
+               load_sub(*tech);
+       obj.technique = tech;
+}
+
+void Object::Loader::technique(const std::string &n)
+{
+       obj.set_technique(get_collection().get<Technique>(n));
 }
 
 } // namespace GL