]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Support the sample sampling qualifier
[libs/gl.git] / source / object.cpp
index 0469c31a99629187f17ce7d5dde803823636bb2e..1852f91b50222ead401dfa177605c31d9d1ee21b 100644 (file)
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include "except.h"
+#include <msp/datafile/collection.h>
+#include <msp/strings/format.h>
+#include "error.h"
 #include "material.h"
 #include "mesh.h"
 #include "object.h"
 #include "objectinstance.h"
 #include "program.h"
 #include "programdata.h"
-#include "texture.h"
-#include "texunit.h"
+#include "renderer.h"
+#include "resourcemanager.h"
+#include "technique.h"
+#include "texturing.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
+Matrix Object::identity_matrix;
+
 Object::Object():
-       mesh(0),
-       shprog(0),
-       shdata(0),
-       material(0)
+       lods(1),
+       lod0_watched(false)
 { }
 
+Object::Object(const Mesh *m, const Technique *t):
+       lods(1),
+       lod0_watched(false)
+{
+       set_mesh(m);
+       set_technique(t);
+}
+
+// TODO should have copy-c'tor to set watch on lod0 mesh if necessary
+
 Object::~Object()
 {
-       delete shdata;
+       if(lods[0].mesh && lod0_watched)
+               if(ResourceManager *rm = lods[0].mesh->get_manager())
+                       rm->unwatch_resource(*lods[0].mesh, *this);
 }
 
-void Object::render(const ObjectInstance *inst) const
+Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
 {
-       setup_render();
+       if(i>lods.size())
+               throw out_of_range(caller);
+       if(i>0 && (!lods[0].mesh || !lods[0].technique))
+               throw invalid_operation(caller);
 
-       if(inst)
-               inst->setup_render();
+       if(i==lods.size())
+               lods.push_back(lods.back());
 
-       mesh->draw();
+       return lods[i];
+}
 
-       if(inst)
-               inst->finish_render();
+void Object::set_mesh(unsigned i, const Mesh *m)
+{
+       RefPtr<const Mesh> &ptr = get_lod(i, "Object::set_mesh").mesh;
+       if(i==0 && ptr && lod0_watched)
+               if(ResourceManager *rm = ptr->get_manager())
+                       rm->unwatch_resource(*ptr, *this);
+       ptr = m;
+       ptr.keep();
+       lod0_watched = false;
 
-       finish_render();
+       if(i==0 && m)
+               if(ResourceManager *rm = m->get_manager())
+               {
+                       rm->watch_resource(*m, *this);
+                       lod0_watched = true;
+               }
+
+       update_bounding_sphere();
 }
 
-void Object::render(const list<const ObjectInstance *> &insts) const
+void Object::update_bounding_sphere()
 {
-       setup_render();
-
-       for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
+       vector<Vector3> points;
+       for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
        {
-               (*i)->setup_render();
+               if(!i->mesh || !i->mesh->is_loaded())
+                       continue;
 
-               mesh->draw();
+               const VertexArray &vertices = i->mesh->get_vertices();
 
-               (*i)->finish_render();
+               int offset = vertices.get_format().offset(VERTEX3);
+               bool three = true;
+               if(offset<0)
+               {
+                       offset = vertices.get_format().offset(VERTEX2);
+                       three = false;
+                       if(offset<0)
+                               continue;
+               }
+
+               unsigned n_vertices = vertices.size();
+               points.reserve(points.size()+n_vertices);
+               for(unsigned j=0; j<n_vertices; ++j)
+               {
+                       const float *v = vertices[j];
+                       points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
+               }
        }
 
-       finish_render();
+       /* Don't touch the bounding sphere if we had no vertices to avoid
+       overwriting a possible hint. */
+       if(points.empty())
+               return;
+
+       bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
 }
 
-void Object::setup_render() const
+const Mesh *Object::get_mesh(unsigned i) const
 {
-       if(!mesh)
-               throw InvalidState("Trying to render Object without mesh");
+       if(i>=lods.size())
+               return 0;
 
-       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();
+       return lods[i].mesh.get();
+}
 
-       if(material)
-               material->apply();
+void Object::set_technique(unsigned i, const Technique *t)
+{
+       RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
+       ptr = t;
+       ptr.keep();
 }
 
-void Object::finish_render() const
+const Technique *Object::get_technique(unsigned i) const
 {
-       if(shprog)
-               Program::unbind();
-       for(unsigned i=0; i<textures.size(); ++i)
-       {
-               TexUnit::activate(i);
-               Texture::unbind();
-       }
+       if(i>=lods.size())
+               return 0;
+
+       return lods[i].technique.get();
 }
 
+void Object::render(Renderer &renderer, const Tag &tag) const
+{
+       const RenderPass *pass = get_pass(tag, 0);
+       if(!pass)
+               return;
+
+       Renderer::Push push(renderer);
+       pass->apply(renderer);
+
+       setup_render(renderer, tag);
+       lods.front().mesh->draw(renderer);
+       finish_render(renderer, tag);
+}
+
+void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
+{
+       unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
+       const RenderPass *pass = get_pass(tag, lod);
+       if(!pass)
+               return;
+
+       Renderer::Push push(renderer);
+       pass->apply(renderer);
+
+       setup_render(renderer, tag);
+       inst.setup_render(renderer, tag);
+       lods[lod].mesh->draw(renderer);
+       inst.finish_render(renderer, tag);
+       finish_render(renderer, tag);
+}
+
+const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
+{
+       const Technique *tech = lods[lod].technique.get();
+       if(!tech)
+               throw logic_error("no technique");
+       if(!tech->has_pass(tag))
+               return 0;
+       return &tech->get_pass(tag);
+}
+
+void Object::resource_loaded(Resource &res)
+{
+       if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
+               update_bounding_sphere();
+}
+
+void Object::resource_removed(Resource &res)
+{
+       if(&res==lods.front().mesh.get())
+               lod0_watched = false;
+}
+
+
+Object::Loader::Loader(Object &o):
+       LodLoader(o, 0, 0)
+{
+       init();
+}
 
 Object::Loader::Loader(Object &o, Collection &c):
-       obj(o),
-       coll(c)
+       LodLoader(o, 0, &c)
 {
-       add("material", &Object::material);
-       add("mesh",     &Object::mesh);
-       add("shader",   &Loader::shader);
-       add("texture",  &Loader::texture);
+       init();
 }
 
-Object::Loader::~Loader()
+void Object::Loader::init()
 {
-       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));
-       }
+       add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
+       add("level_of_detail", &Loader::level_of_detail);
 }
 
-void Object::Loader::shader(const string &n)
+void Object::Loader::finish()
 {
-       obj.shprog=&coll.get<Program>(n);
-       if(!obj.shdata)
-               obj.shdata=new ProgramData;
+       obj.update_bounding_sphere();
 }
 
-void Object::Loader::texture(const string &n)
+void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
 {
-       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.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
+}
+
+void Object::Loader::level_of_detail(unsigned i)
+{
+       LodLoader ldr(obj, i, coll);
+       load_sub_with(ldr);
+}
+
+
+Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
+       DataFile::CollectionObjectLoader<Object>(o, c),
+       index(i),
+       lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
+{
+       add("mesh",      &LodLoader::mesh_inline);
+       add("mesh",      &LodLoader::mesh);
+       add("technique", &LodLoader::technique_inline);
+       add("technique", &LodLoader::technique);
+}
+
+void Object::LodLoader::mesh(const string &n)
+{
+       obj.set_mesh(index, &get_collection().get<Mesh>(n));
+}
+
+void Object::LodLoader::mesh_inline()
+{
+       RefPtr<Mesh> msh = new Mesh;
+       load_sub(*msh);
+       lod.mesh = msh;
+}
+
+void Object::LodLoader::technique(const std::string &n)
+{
+       obj.set_technique(index, &get_collection().get<Technique>(n));
+}
+
+void Object::LodLoader::technique_inline()
+{
+       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);
+       lod.technique = tech;
 }
 
 } // namespace GL