]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Require mspgbase now that Image was moved there
[libs/gl.git] / source / object.cpp
index 3ba2d64c378862ea435f6d371cc4e5ec122a5579..78f445577caa54209044e3db4dc40dd1e33b5cec 100644 (file)
@@ -24,6 +24,7 @@ namespace GL {
 
 Object::Object():
        meshes(1, static_cast<Mesh *>(0)),
+       main_texture(0),
        material(0)
 {
        normal_pass=&passes[0];
@@ -58,11 +59,6 @@ void Object::render(const ObjectInstance &inst, const Tag &tag) const
        render(get_pass(tag), &inst);
 }
 
-void Object::render(const list<const ObjectInstance *> &insts, const Tag &tag) const
-{
-       render(get_pass(tag), insts);
-}
-
 void Object::setup_render(const ObjectPass &pass) const
 {
        if(!meshes[0])
@@ -78,8 +74,8 @@ void Object::setup_render(const ObjectPass &pass) const
                        textures[i]->bind();
                }
        }
-       else if(!textures.empty())
-               textures.front()->bind();
+       else if(main_texture && pass.use_textures)
+               main_texture->bind();
 
        if(material)
                material->apply();
@@ -88,48 +84,36 @@ void Object::setup_render(const ObjectPass &pass) const
 void Object::finish_render(const ObjectPass &pass) const
 {
        if(pass.shprog)
-               Program::unbind();
-       for(unsigned i=textures.size(); i--;)
        {
-               TexUnit::activate(i);
-               Texture::unbind();
+               Program::unbind();
+               for(unsigned i=textures.size(); i--;)
+               {
+                       TexUnit::activate(i);
+                       Texture::unbind();
+               }
        }
+       else if(main_texture)
+               Texture::unbind();
 }
 
 void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
 {
        setup_render(pass);
 
-       unsigned lod=0;
-       if(inst)
-       {
-               inst->setup_render(pass);
-               lod=min(inst->get_level_of_detail(), meshes.size()-1);
-       }
-
-       meshes[lod]->draw();
-
        if(inst)
-               inst->finish_render(pass);
+               render_instance(pass, *inst);
+       else
+               meshes[0]->draw();
 
        finish_render(pass);
 }
 
-void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
+void Object::render_instance(const ObjectPass &pass, const ObjectInstance &inst) const
 {
-       setup_render(pass);
-
-       for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
-       {
-               (*i)->setup_render(pass);
-
-               unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
-               meshes[lod]->draw();
-
-               (*i)->finish_render(pass);
-       }
-
-       finish_render(pass);
+       inst.setup_render(pass);
+       unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
+       meshes[lod]->draw();
+       inst.finish_render(pass);
 }
 
 
@@ -143,10 +127,11 @@ Object::Loader::Loader(Object &o, Collection &c):
        add("mesh",     &Loader::mesh);
        add("pass",     &Loader::pass);
        add("shader",   &Loader::shader);
+       add("shader_texture", &Loader::shader_texture);
        add("texture",  &Loader::texture);
 }
 
-Object::Loader::~Loader()
+void Object::Loader::finish()
 {
        for(map<unsigned, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
                if(i->second.shdata)
@@ -164,11 +149,10 @@ void Object::Loader::lod_mesh(unsigned l, const string &n)
 
 void Object::Loader::material_inline()
 {
-       throw Exception("material_inline not supported yet");
-       /*RefPtr<Material> mat=new Material;
+       RefPtr<Material> mat=new Material;
        load_sub(*mat);
        coll.add(format("%p%p", &obj, mat.get()), mat.get());
-       obj.material=mat.release();*/
+       obj.material=mat.release();
 }
 
 void Object::Loader::mesh(const string &n)
@@ -201,7 +185,7 @@ void Object::Loader::shader(const string &n)
        }
 }
 
-void Object::Loader::texture(const string &n)
+void Object::Loader::shader_texture(const string &n)
 {
        unsigned eqsign=n.find('=');
        if(eqsign!=string::npos)
@@ -216,5 +200,15 @@ void Object::Loader::texture(const string &n)
        }
 }
 
+void Object::Loader::texture(const string &n)
+{
+       if(obj.main_texture)
+               throw Exception("Only one main texture may be specified");
+
+       obj.main_texture=coll.get<Texture>(n);
+       obj.textures.push_back(obj.main_texture);
+       textures.push_back("texture");
+}
+
 } // namespace GL
 } // namespace Msp