Distributed under the LGPL
*/
+#include <msp/datafile/collection.h>
+#include <msp/strings/formatter.h>
#include "except.h"
#include "material.h"
#include "mesh.h"
#include "objectinstance.h"
#include "program.h"
#include "programdata.h"
+#include "technique.h"
#include "texture.h"
#include "texunit.h"
namespace GL {
Object::Object():
- mesh(0),
- shprog(0),
- shdata(0),
- material(0)
+ meshes(1)
{ }
Object::~Object()
{
- delete shdata;
}
-void Object::render(const ObjectInstance *inst) const
+void Object::set_mesh(unsigned i, const Mesh *m)
{
- setup_render();
+ if(i>meshes.size())
+ throw InvalidParameterValue("LODs must be continuous");
- if(inst)
- inst->setup_render();
-
- mesh->draw();
-
- if(inst)
- inst->finish_render();
+ if(i==meshes.size())
+ meshes.push_back(m);
+ else
+ meshes[i] = m;
+ meshes[i].keep();
+}
- finish_render();
+void Object::set_technique(const Technique *t)
+{
+ technique = t;
+ technique.keep();
}
-void Object::render(const list<const ObjectInstance *> &insts) const
+void Object::render(const Tag &tag) const
{
- setup_render();
+ const RenderPass *pass = get_pass(tag);
+ if(!pass)
+ return;
- for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
- {
- (*i)->setup_render();
+ Bind bind(pass);
+ meshes.front()->draw();
+}
- mesh->draw();
+void Object::render(const ObjectInstance &inst, const Tag &tag) const
+{
+ const RenderPass *pass = get_pass(tag);
+ if(!pass)
+ return;
- (*i)->finish_render();
- }
+ Bind bind(pass);
+ render_instance(inst, tag);
+}
- finish_render();
+const RenderPass *Object::get_pass(const Tag &tag) const
+{
+ if(!technique || !technique->has_pass(tag))
+ return 0;
+ return &technique->get_pass(tag);
}
-void Object::setup_render() const
+void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
{
- if(!mesh)
- throw InvalidState("Trying to render Object without mesh");
+ inst.setup_render(tag);
+ unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
+ meshes[lod]->draw();
+ inst.finish_render(tag);
+}
- 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();
+Object::Loader::Loader(Object &o):
+ DataFile::CollectionObjectLoader<Object>(o, 0)
+{
+ init();
}
-void Object::finish_render() const
+Object::Loader::Loader(Object &o, Collection &c):
+ DataFile::CollectionObjectLoader<Object>(o, &c)
{
- if(shprog)
- Program::unbind();
- for(unsigned i=0; i<textures.size(); ++i)
- {
- TexUnit::activate(i);
- Texture::unbind();
- }
+ init();
}
+void Object::Loader::init()
+{
+ 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));
+}
-Object::Loader::Loader(Object &o, Collection &c):
- obj(o),
- coll(c)
+void Object::Loader::mesh()
{
- add("material", &Object::material);
- add("mesh", &Object::mesh);
- add("shader", &Loader::shader);
- add("texture", &Loader::texture);
+ RefPtr<Mesh> msh = new Mesh;
+ load_sub(*msh);
+ obj.meshes.front() = msh;
}
-Object::Loader::~Loader()
+void Object::Loader::mesh(unsigned l)
{
- 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));
- }
+ 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::shader(const string &n)
+void Object::Loader::mesh(const std::string &n)
{
- obj.shprog=&coll.get<Program>(n);
- if(!obj.shdata)
- obj.shdata=new ProgramData;
+ obj.set_mesh(get_collection().get<Mesh>(n));
}
-void Object::Loader::texture(const string &n)
+void Object::Loader::mesh(unsigned l, const string &n)
{
- 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));
- }
+ obj.set_mesh(l, get_collection().get<Mesh>(n));
+}
+
+void Object::Loader::technique()
+{
+ 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;
+}
+
+void Object::Loader::technique(const std::string &n)
+{
+ obj.set_technique(get_collection().get<Technique>(n));
}
} // namespace GL