]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Allow copying of Uniforms and ProgramData
[libs/gl.git] / source / object.cpp
index 0469c31a99629187f17ce7d5dde803823636bb2e..edfe39e56008e580e85b292c7508395260de9e71 100644 (file)
@@ -5,13 +5,17 @@ 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"
 
@@ -21,121 +25,80 @@ namespace Msp {
 namespace GL {
 
 Object::Object():
-       mesh(0),
-       shprog(0),
-       shdata(0),
-       material(0)
+       meshes(1),
+       own_technique(false),
+       technique(0)
 { }
 
 Object::~Object()
 {
-       delete shdata;
+       if(own_technique)
+               delete technique;
 }
 
-void Object::render(const ObjectInstance *inst) const
+void Object::render(const Tag &tag) const
 {
-       setup_render();
+       const RenderPass *pass=get_pass(tag);
+       if(!pass)
+               return;
 
-       if(inst)
-               inst->setup_render();
-
-       mesh->draw();
-
-       if(inst)
-               inst->finish_render();
-
-       finish_render();
+       Bind bind(*pass);
+       meshes[0]->draw();
 }
 
-void Object::render(const list<const ObjectInstance *> &insts) const
+void Object::render(const ObjectInstance &inst, const Tag &tag) const
 {
-       setup_render();
-
-       for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
-       {
-               (*i)->setup_render();
-
-               mesh->draw();
-
-               (*i)->finish_render();
-       }
+       const RenderPass *pass=get_pass(tag);
+       if(!pass)
+               return;
 
-       finish_render();
+       Bind bind(*pass);
+       render_instance(inst, tag);
+       meshes[0]->draw();
 }
 
-void Object::setup_render() const
+const RenderPass *Object::get_pass(const Tag &tag) const
 {
-       if(!mesh)
-               throw InvalidState("Trying to render Object without mesh");
-
-       if(shprog)
-       {
-               shprog->bind();
-               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();
+       if(!technique->has_pass(tag))
+               return 0;
+       return &technique->get_pass(tag);
 }
 
-void Object::finish_render() const
+void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
 {
-       if(shprog)
-               Program::unbind();
-       for(unsigned i=0; i<textures.size(); ++i)
-       {
-               TexUnit::activate(i);
-               Texture::unbind();
-       }
+       inst.setup_render(tag);
+       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("material", &Object::material);
-       add("mesh",     &Object::mesh);
-       add("shader",   &Loader::shader);
-       add("texture",  &Loader::texture);
+       add("lod_mesh", &Loader::lod_mesh);
+       add("mesh",     &Loader::mesh);
+       add("technique", &Loader::technique);
+       add("technique", &Object::technique);
 }
 
-Object::Loader::~Loader()
+void Object::Loader::lod_mesh(unsigned l, const string &n)
 {
-       if(obj.shdata)
-       {
-               for(unsigned i=0; i<textures.size(); ++i)
-                       obj.shdata->uniform(obj.shprog->get_uniform_location(textures[i]), static_cast<int>(i));
-       }
+       obj.meshes.resize(l+1, 0);
+       obj.meshes[l]=get_collection().get<Mesh>(n);
 }
 
-void Object::Loader::shader(const string &n)
+void Object::Loader::mesh(const string &n)
 {
-       obj.shprog=&coll.get<Program>(n);
-       if(!obj.shdata)
-               obj.shdata=new ProgramData;
+       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));
-       }
-       else
-       {
-               obj.textures.push_back(&coll.get<Texture>(n));
-               textures.push_back(n);
-       }
+       RefPtr<Technique> tch=new Technique;
+       load_sub(*tch, get_collection());
+       obj.technique=tch.release();
+       obj.own_technique=true;
 }
 
 } // namespace GL