]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Style update: add spaces around assignment operators
[libs/gl.git] / source / object.cpp
index e406755c1e81e2e1d27e5f77658c19d5034c6b21..938e83f911211cd75b2e50302b6ad00d67345005 100644 (file)
@@ -5,15 +5,16 @@ 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"
 #include "mesh.h"
 #include "object.h"
 #include "objectinstance.h"
-#include "objectpass.h"
 #include "program.h"
 #include "programdata.h"
+#include "technique.h"
 #include "texture.h"
 #include "texunit.h"
 
@@ -23,201 +24,114 @@ namespace Msp {
 namespace GL {
 
 Object::Object():
-       meshes(1, static_cast<Mesh *>(0)),
-       material(0)
-{
-       normal_pass=&passes[""];
-}
+       meshes(1),
+       technique(0),
+       own_mesh(false),
+       own_technique(false)
+}
 
 Object::~Object()
 {
-       for(map<string, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
-               delete i->second.shdata;
+       if(own_technique)
+               delete technique;
+       if(own_mesh)
+               delete meshes[0];
 }
 
-bool Object::has_pass(const string &pn) const
+void Object::render(const Tag &tag) const
 {
-       return passes.count(pn);
-}
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
 
-const ObjectPass &Object::get_pass(const string &pn) const
-{
-       map<string, ObjectPass>::const_iterator i=passes.find(pn);
-       if(i==passes.end())
-               throw KeyError("Unknown pass");
-       return i->second;
+       Bind bind(*pass);
+       meshes[0]->draw();
 }
 
-void Object::render(const ObjectInstance *inst) const
+void Object::render(const ObjectInstance &inst, const Tag &tag) const
 {
-       render(*normal_pass, inst);
-}
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
 
-void Object::render(const string &pn, const ObjectInstance *inst) const
-{
-       render(get_pass(pn), inst);
+       Bind bind(*pass);
+       render_instance(inst, tag);
+       meshes[0]->draw();
 }
 
-void Object::render(const list<const ObjectInstance *> &insts) const
+const RenderPass *Object::get_pass(const Tag &tag) const
 {
-       render(*normal_pass, insts);
+       if(!technique->has_pass(tag))
+               return 0;
+       return &technique->get_pass(tag);
 }
 
-void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
+void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
 {
-       render(get_pass(pn), insts);
+       inst.setup_render(tag);
+       unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
+       meshes[lod]->draw();
+       inst.finish_render(tag);
 }
 
-void Object::setup_render(const ObjectPass &pass) const
-{
-       if(!meshes[0])
-               throw InvalidState("Trying to render Object without mesh");
-
-       if(pass.shprog)
-       {
-               pass.shprog->bind();
-               pass.shdata->apply();
-               for(unsigned i=0; i<textures.size(); ++i)
-               {
-                       TexUnit::activate(i);
-                       textures[i]->bind();
-               }
-       }
-       else if(!textures.empty())
-               textures.front()->bind();
-
-       if(material)
-               material->apply();
-}
 
-void Object::finish_render(const ObjectPass &pass) const
+Object::Loader::Loader(Object &o):
+       DataFile::CollectionObjectLoader<Object>(o, 0)
 {
-       if(pass.shprog)
-               Program::unbind();
-       for(unsigned i=textures.size(); i--;)
-       {
-               TexUnit::activate(i);
-               Texture::unbind();
-       }
+       init();
 }
 
-void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
+Object::Loader::Loader(Object &o, Collection &c):
+       DataFile::CollectionObjectLoader<Object>(o, &c)
 {
-       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);
-
-       finish_render(pass);
+       init();
 }
 
-void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
+void Object::Loader::init()
 {
-       setup_render(pass);
+       allow_pointer_reload = false;
 
-       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);
-}
-
-
-Object::Loader::Loader(Object &o, Collection &c):
-       obj(o),
-       coll(c)
-{
        add("lod_mesh", &Loader::lod_mesh);
-       add("material", &Object::material);
-       add("material_inline", &Loader::material_inline);
-       add("mesh",     &Loader::mesh);
-       add("pass",     &Loader::pass);
-       add("shader",   &Loader::shader);
-       add("texture",  &Loader::texture);
-}
-
-Object::Loader::~Loader()
-{
-       for(map<string, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
-               if(i->second.shdata)
-               {
-                       for(unsigned j=0; j<textures.size(); ++j)
-                               i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
-               }
+       add("mesh",     static_cast<void (Loader::*)()>(&Loader::mesh));
+       add("mesh",     static_cast<void (Loader::*)(const std::string &)>(&Loader::mesh));
+       add("technique", &Loader::technique);
+       add("technique", &Object::technique);
 }
 
 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] = get_collection().get<Mesh>(n);
 }
 
-void Object::Loader::material_inline()
+void Object::Loader::mesh()
 {
-       throw Exception("material_inline not supported yet");
-       /*RefPtr<Material> mat=new Material;
-       load_sub(*mat);
-       coll.add(format("%p%p", &obj, mat.get()), mat.get());
-       obj.material=mat.release();*/
-}
+       if(obj.meshes[0])
+               throw InvalidState("A mesh is already loaded");
 
-void Object::Loader::mesh(const string &n)
-{
-       obj.meshes[0]=coll.get<Mesh>(n);
+       RefPtr<Mesh> msh = new Mesh;
+       load_sub(*msh);
+       obj.meshes[0] = msh.release();
+       obj.own_mesh = true;
 }
 
-void Object::Loader::pass(const string &n)
+void Object::Loader::mesh(const std::string &n)
 {
-       if(obj.passes.count(n))
-               throw KeyError("Duplicate pass name");
-       ObjectPass p;
-       load_sub(p, coll);
-       obj.passes[n]=p;
-}
+       if(obj.meshes[0])
+               throw InvalidState("A mesh is already loaded");
 
-void Object::Loader::shader(const string &n)
-{
-       Program *shprog=coll.get<Program>(n);
-       if(shprog)  // Allow for unsupported shaders
-       {
-               RefPtr<ProgramData> shdata=new ProgramData;
-               load_sub(*shdata, *shprog);
-
-               obj.normal_pass->shprog=shprog;
-               if(obj.normal_pass->shdata)
-                       delete obj.normal_pass->shdata;
-               obj.normal_pass->shdata=shdata.release();
-       }
+       obj.meshes[0] = get_collection().get<Mesh>(n);
 }
 
-void Object::Loader::texture(const string &n)
+void Object::Loader::technique()
 {
-       unsigned eqsign=n.find('=');
-       if(eqsign!=string::npos)
-       {
-               obj.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
-               textures.push_back(n.substr(0, eqsign));
-       }
+       RefPtr<Technique> tech = new Technique;
+       if(coll)
+               load_sub(*tech, get_collection());
        else
-       {
-               obj.textures.push_back(coll.get<Texture>(n));
-               textures.push_back(n);
-       }
+               load_sub(*tech);
+       obj.technique = tech.release();
+       obj.own_technique = true;
 }
 
 } // namespace GL