]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Inherit Loaders from the ObjectLoader classes
[libs/gl.git] / source / object.cpp
index 2026e3d58f3d0b658ed4b3bdba7fe9d211d6ffd1..f2d1f8bf74a9781c64d829e2f7950513296dbae1 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/datafile/collection.h>
 #include <msp/strings/formatter.h>
 #include "except.h"
 #include "material.h"
@@ -34,16 +35,11 @@ Object::~Object()
 {
 }
 
-bool Object::has_pass(const Tag &tag) const
-{
-       if(technique)
-               return technique->has_pass(tag);
-       else
-               return tag.id==0;
-}
-
 void Object::render(const Tag &tag) const
 {
+       if(!can_render(tag))
+               return;
+
        const ObjectPass *pass=get_pass(tag);
        setup_render(pass);
        meshes[0]->draw();
@@ -52,6 +48,9 @@ void Object::render(const Tag &tag) const
 
 void Object::render(const ObjectInstance &inst, const Tag &tag) const
 {
+       if(!can_render(tag))
+               return;
+
        const ObjectPass *pass=get_pass(tag);
        setup_render(pass);
        render_instance(inst, tag);
@@ -59,6 +58,14 @@ void Object::render(const ObjectInstance &inst, const Tag &tag) const
        finish_render(pass);
 }
 
+bool Object::can_render(const Tag &tag) const
+{
+       if(technique)
+               return technique->has_pass(tag);
+       else
+               return tag.id==0;
+}
+
 const ObjectPass *Object::get_pass(const Tag &tag) const
 {
        if(technique)
@@ -111,15 +118,14 @@ void Object::finish_render(const ObjectPass *pass) const
 void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
 {
        inst.setup_render(tag);
-       unsigned lod=min(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, Collection &c):
-       obj(o),
-       coll(c)
+       DataFile::CollectionObjectLoader<Object>(o, &c)
 {
        add("lod_mesh", &Loader::lod_mesh);
        add("material", &Object::material);
@@ -148,20 +154,20 @@ void Object::Loader::finish()
 void Object::Loader::lod_mesh(unsigned l, const string &n)
 {
        obj.meshes.resize(l+1, 0);
-       obj.meshes[l]=coll.get<Mesh>(n);
+       obj.meshes[l]=coll->get<Mesh>(n);
 }
 
 void Object::Loader::material_inline()
 {
        RefPtr<Material> mat=new Material;
        load_sub(*mat);
-       coll.add(format("_%p", mat.get()), mat.get());
+       coll->add(format("_%p", mat.get()), mat.get());
        obj.material=mat.release();
 }
 
 void Object::Loader::mesh(const string &n)
 {
-       obj.meshes[0]=coll.get<Mesh>(n);
+       obj.meshes[0]=coll->get<Mesh>(n);
 }
 
 void Object::Loader::shader_texture(const string &n)
@@ -169,16 +175,16 @@ void Object::Loader::shader_texture(const string &n)
        if(!obj.technique)
                throw InvalidState("Can't specify shader textures without a Technique");
 
-       unsigned eqsign=n.find('=');
+       string::size_type eqsign=n.find('=');
        if(eqsign==string::npos)
                throw InvalidParameterValue("Must specify texture slot name");
 
-       obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
+       obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll->get<Texture>(n.substr(eqsign+1));
 }
 
 void Object::Loader::technique(const string &n)
 {
-       obj.technique=coll.get<Technique>(n);
+       obj.technique=coll->get<Technique>(n);
        obj.textures.resize(obj.technique->get_n_textures());
        obj.material=obj.technique->get_material();
 }
@@ -188,7 +194,7 @@ void Object::Loader::texture(const string &n)
        if(obj.main_texture)
                throw Exception("Only one main texture may be specified");
 
-       Texture *tex=coll.get<Texture>(n);
+       Texture *tex=coll->get<Texture>(n);
        if(obj.technique)
                obj.textures[obj.technique->get_texture_index("texture")]=tex;
        obj.main_texture=tex;