-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007-2011 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 "program.h"
-#include "programdata.h"
-#include "renderer.h"
-#include "technique.h"
-#include "texturing.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Object::Object():
- meshes(1)
-{ }
-
-Object::~Object()
-{
-}
-
-void Object::set_mesh(unsigned i, const Mesh *m)
-{
- if(i>meshes.size())
- throw InvalidParameterValue("LODs must be continuous");
-
- if(i==meshes.size())
- meshes.push_back(m);
- else
- meshes[i] = m;
- meshes[i].keep();
-}
-
-void Object::set_technique(const Technique *t)
-{
- technique = t;
- technique.keep();
-}
-
-void Object::render(const Tag &tag) const
-{
- const RenderPass *pass = get_pass(tag);
- if(!pass)
- return;
-
- Bind bind_shader(pass->get_shader_program());
- if(pass->get_shader_data())
- pass->get_shader_data()->apply();
- Bind bind_material(pass->get_material());
- Bind bind_texturing(pass->get_texturing());
-
- meshes.front()->draw();
-}
-
-void Object::render(Renderer &renderer, const Tag &tag) const
-{
- const RenderPass *pass = get_pass(tag);
- if(!pass)
- return;
-
- Renderer::Push push(renderer);
- renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
- renderer.set_material(pass->get_material());
- renderer.set_texturing(pass->get_texturing());
-
- meshes.front()->draw(renderer);
-}
-
-void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
-{
- const RenderPass *pass = get_pass(tag);
- if(!pass)
- return;
-
- Renderer::Push push(renderer);
- renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
- renderer.set_material(pass->get_material());
- renderer.set_texturing(pass->get_texturing());
-
- inst.setup_render(renderer, tag);
- unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
- meshes[lod]->draw(renderer);
- inst.finish_render(renderer, tag);
-}
-
-const RenderPass *Object::get_pass(const Tag &tag) const
-{
- if(!technique || !technique->has_pass(tag))
- return 0;
- return &technique->get_pass(tag);
-}
-
-
-Object::Loader::Loader(Object &o):
- DataFile::CollectionObjectLoader<Object>(o, 0)
-{
- init();
-}
-
-Object::Loader::Loader(Object &o, Collection &c):
- DataFile::CollectionObjectLoader<Object>(o, &c)
-{
- 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));
-}
-
-void Object::Loader::mesh()
-{
- RefPtr<Mesh> msh = new Mesh;
- load_sub(*msh);
- obj.meshes.front() = msh;
-}
-
-void Object::Loader::mesh(unsigned l)
-{
- 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::mesh(const std::string &n)
-{
- obj.set_mesh(get_collection().get<Mesh>(n));
-}
-
-void Object::Loader::mesh(unsigned l, const string &n)
-{
- 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
- load_sub(*tech);
- obj.technique = tech;
-}
-
-void Object::Loader::technique(const std::string &n)
-{
- obj.set_technique(get_collection().get<Technique>(n));
-}
-
-} // namespace GL
-} // namespace Msp