]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / object.cpp
index e406755c1e81e2e1d27e5f77658c19d5034c6b21..d573fdd4975695eddfd3d164eca0128d97936937 100644 (file)
@@ -1,21 +1,15 @@
-/* $Id$
-
-This file is part of libmspgl
-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 "texture.h"
-#include "texunit.h"
+#include "renderer.h"
+#include "technique.h"
+#include "texturing.h"
 
 using namespace std;
 
@@ -23,201 +17,163 @@ namespace Msp {
 namespace GL {
 
 Object::Object():
-       meshes(1, static_cast<Mesh *>(0)),
-       material(0)
-{
-       normal_pass=&passes[""];
-}
+       meshes(1)
+{ }
 
 Object::~Object()
 {
-       for(map<string, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
-               delete i->second.shdata;
 }
 
-bool Object::has_pass(const string &pn) const
+void Object::set_mesh(unsigned i, const Mesh *m)
 {
-       return passes.count(pn);
-}
-
-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;
-}
+       if(i>meshes.size())
+               throw InvalidParameterValue("LODs must be continuous");
 
-void Object::render(const ObjectInstance *inst) const
-{
-       render(*normal_pass, inst);
+       if(i==meshes.size())
+               meshes.push_back(m);
+       else
+               meshes[i] = m;
+       meshes[i].keep();
 }
 
-void Object::render(const string &pn, const ObjectInstance *inst) const
+const Mesh *Object::get_mesh(unsigned i) const
 {
-       render(get_pass(pn), inst);
-}
+       if(i>=meshes.size())
+               return 0;
 
-void Object::render(const list<const ObjectInstance *> &insts) const
-{
-       render(*normal_pass, insts);
+       return meshes[i].get();
 }
 
-void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
+void Object::set_technique(const Technique *t)
 {
-       render(get_pass(pn), insts);
+       technique = t;
+       technique.keep();
 }
 
-void Object::setup_render(const ObjectPass &pass) const
+void Object::render(const Tag &tag) const
 {
-       if(!meshes[0])
-               throw InvalidState("Trying to render Object without mesh");
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
 
-       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();
+       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());
 
-       if(material)
-               material->apply();
+       meshes.front()->draw();
 }
 
-void Object::finish_render(const ObjectPass &pass) const
+void Object::render(Renderer &renderer, const Tag &tag) const
 {
-       if(pass.shprog)
-               Program::unbind();
-       for(unsigned i=textures.size(); i--;)
-       {
-               TexUnit::activate(i);
-               Texture::unbind();
-       }
-}
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
 
-void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
-{
-       setup_render(pass);
+       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());
 
-       unsigned lod=0;
-       if(inst)
-       {
-               inst->setup_render(pass);
-               lod=min(inst->get_level_of_detail(), meshes.size()-1);
-       }
+       meshes.front()->draw(renderer);
+}
 
-       meshes[lod]->draw();
+void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
+{
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
 
-       if(inst)
-               inst->finish_render(pass);
+       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());
 
-       finish_render(pass);
+       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);
 }
 
-void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
+const RenderPass *Object::get_pass(const Tag &tag) 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();
+       if(!technique || !technique->has_pass(tag))
+               return 0;
+       return &technique->get_pass(tag);
+}
 
-               (*i)->finish_render(pass);
-       }
 
-       finish_render(pass);
+Object::Loader::Loader(Object &o):
+       DataFile::CollectionObjectLoader<Object>(o, 0)
+{
+       init();
 }
 
-
 Object::Loader::Loader(Object &o, Collection &c):
-       obj(o),
-       coll(c)
+       DataFile::CollectionObjectLoader<Object>(o, &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);
+       init();
 }
 
-Object::Loader::~Loader()
+void Object::Loader::init()
 {
-       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));
-               }
+       allow_pointer_reload = false;
+
+       add("mesh",     &Loader::mesh_inline);
+       add("mesh",     &Loader::mesh_inline_lod);
+       add("mesh",     &Loader::mesh);
+       add("mesh",     &Loader::mesh_lod);
+       add("technique", &Loader::technique_inline);
+       add("technique", &Loader::technique);
+
+       // Deprecated alias, will be removed
+       add("lod_mesh", &Loader::mesh_lod);
 }
 
-void Object::Loader::lod_mesh(unsigned l, const string &n)
+void Object::Loader::mesh_inline()
 {
-       obj.meshes.resize(l+1, 0);
-       obj.meshes[l]=coll.get<Mesh>(n);
+       RefPtr<Mesh> msh = new Mesh;
+       load_sub(*msh);
+       obj.meshes.front() = msh;
 }
 
-void Object::Loader::material_inline()
+void Object::Loader::mesh_inline_lod(unsigned l)
 {
-       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(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 string &n)
+void Object::Loader::mesh(const std::string &n)
 {
-       obj.meshes[0]=coll.get<Mesh>(n);
+       obj.set_mesh(get_collection().get<Mesh>(n));
 }
 
-void Object::Loader::pass(const string &n)
+void Object::Loader::mesh_lod(unsigned l, const string &n)
 {
-       if(obj.passes.count(n))
-               throw KeyError("Duplicate pass name");
-       ObjectPass p;
-       load_sub(p, coll);
-       obj.passes[n]=p;
+       obj.set_mesh(l, get_collection().get<Mesh>(n));
 }
 
-void Object::Loader::shader(const string &n)
+void Object::Loader::technique_inline()
 {
-       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();
-       }
+       RefPtr<Technique> tech = new Technique;
+       if(coll)
+               load_sub(*tech, get_collection());
+       else
+               load_sub(*tech);
+       obj.technique = tech;
 }
 
-void Object::Loader::texture(const string &n)
+void Object::Loader::technique(const std::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));
-       }
-       else
-       {
-               obj.textures.push_back(coll.get<Texture>(n));
-               textures.push_back(n);
-       }
+       obj.set_technique(get_collection().get<Technique>(n));
 }
 
 } // namespace GL