]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Don't crash in bounding sphere generation if an object has a null mesh
[libs/gl.git] / source / object.cpp
index b53a3f0802767e9e6e80b7a6e1d0434aa4d0c474..d48b0843fb9f3b73591588631429ce09e2709a8c 100644 (file)
@@ -1,22 +1,15 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/strings/formatter.h>
-#include "except.h"
+#include <msp/datafile/collection.h>
+#include <msp/strings/format.h>
 #include "material.h"
 #include "mesh.h"
 #include "object.h"
 #include "objectinstance.h"
-#include "objectpass.h"
 #include "program.h"
 #include "programdata.h"
+#include "renderer.h"
+#include "resourcemanager.h"
 #include "technique.h"
-#include "texture.h"
-#include "texunit.h"
+#include "texturing.h"
 
 using namespace std;
 
@@ -24,172 +17,224 @@ namespace Msp {
 namespace GL {
 
 Object::Object():
-       meshes(1, static_cast<Mesh *>(0)),
-       technique(0),
-       main_texture(0),
-       material(0)
+       meshes(1)
 { }
 
+Object::Object(const Mesh *m, const Technique *t)
+{
+       set_mesh(m);
+       set_technique(t);
+}
+
+// Avoid synthesizing ~RefPtr in files including object.h
 Object::~Object()
 {
+       if(meshes[0])
+               if(ResourceManager *rm = meshes[0]->get_manager())
+                       rm->unwatch_resource(*meshes[0], *this);
 }
 
-bool Object::has_pass(const Tag &tag) const
+void Object::set_mesh(unsigned i, const Mesh *m)
 {
-       if(technique)
-               return technique->has_pass(tag);
+       if(i>meshes.size())
+               throw out_of_range("Object::set_mesh");
+
+       if(i==meshes.size())
+               meshes.push_back(m);
        else
-               return tag.id==0;
+       {
+               if(i==0 && meshes[i])
+                       if(ResourceManager *rm = meshes[i]->get_manager())
+                               rm->unwatch_resource(*meshes[i], *this);
+               meshes[i] = m;
+       }
+       meshes[i].keep();
+
+       if(i==0 && m)
+               if(ResourceManager *rm = m->get_manager())
+                       rm->watch_resource(*m, *this);
+
+       update_bounding_sphere();
 }
 
-void Object::render(const Tag &tag) const
+void Object::update_bounding_sphere()
 {
-       const ObjectPass *pass=get_pass(tag);
-       setup_render(pass);
-       meshes[0]->draw();
-       finish_render(pass);
+       vector<Vector3> points;
+       for(vector<RefPtr<const Mesh> >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i)
+       {
+               if(!*i)
+                       continue;
+
+               const VertexArray &vertices = (*i)->get_vertices();
+
+               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)));
+               }
+       }
+
+       bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
 }
 
-void Object::render(const ObjectInstance &inst, const Tag &tag) const
+const Mesh *Object::get_mesh(unsigned i) const
 {
-       const ObjectPass *pass=get_pass(tag);
-       setup_render(pass);
-       render_instance(inst, tag);
-       meshes[0]->draw();
-       finish_render(pass);
+       if(i>=meshes.size())
+               return 0;
+
+       return meshes[i].get();
 }
 
-const ObjectPass *Object::get_pass(const Tag &tag) const
+void Object::set_technique(const Technique *t)
 {
-       if(technique)
-               return &technique->get_pass(tag);
-       else if(tag.id==0)
-               return 0;
-       throw KeyError("Unknown pass");
+       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 && pass->shprog)
-       {
-               pass->shprog->bind();
-               pass->shdata->apply();
-               for(unsigned i=0; i<textures.size(); ++i)
-               {
-                       TexUnit::activate(i);
-                       textures[i]->bind();
-               }
-       }
-       else if(main_texture && (!pass || pass->use_textures))
-               main_texture->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->bind();
+       meshes.front()->draw();
 }
 
-void Object::finish_render(const ObjectPass *pass) const
+void Object::render(Renderer &renderer, const Tag &tag) const
 {
-       if(pass && pass->shprog)
-       {
-               Program::unbind();
-               for(unsigned i=textures.size(); i--;)
-               {
-                       TexUnit::activate(i);
-                       Texture::unbind();
-               }
-       }
-       else if(main_texture)
-               Texture::unbind();
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
+
+       Renderer::Push push(renderer);
+       pass->apply(renderer);
 
-       if(material)
-               Material::unbind();
+       setup_render(renderer, tag);
+       meshes.front()->draw(renderer);
+       finish_render(renderer, tag);
 }
 
-void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
+void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
 {
-       inst.setup_render(tag);
-       unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
-       meshes[lod]->draw();
-       inst.finish_render(tag);
+       const RenderPass *pass = get_pass(tag);
+       if(!pass)
+               return;
+
+       Renderer::Push push(renderer);
+       pass->apply(renderer);
+
+       setup_render(renderer, tag);
+       inst.setup_render(renderer, tag);
+       unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), meshes.size()-1);
+       meshes[lod]->draw(renderer);
+       inst.finish_render(renderer, tag);
+       finish_render(renderer, tag);
 }
 
+const RenderPass *Object::get_pass(const Tag &tag) const
+{
+       if(!technique)
+               throw logic_error("!technique");
+       if(!technique->has_pass(tag))
+               return 0;
+       return &technique->get_pass(tag);
+}
+
+void Object::resource_loaded(Resource &res)
+{
+       if(!meshes.empty() && &res==meshes.front().get() && bounding_sphere.is_empty())
+               update_bounding_sphere();
+}
+
+
+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)
+{
+       init();
+}
+
+void Object::Loader::init()
 {
-       add("lod_mesh", &Loader::lod_mesh);
-       add("material", &Object::material);
-       add("material_inline", &Loader::material_inline);
+       add("mesh",     &Loader::mesh_inline);
+       add("mesh",     &Loader::mesh_inline_lod);
        add("mesh",     &Loader::mesh);
-       add("shader_texture", &Loader::shader_texture);
+       add("mesh",     &Loader::mesh_lod);
+       add("technique", &Loader::technique_inline);
        add("technique", &Loader::technique);
-       add("texture",  &Loader::texture);
 }
 
 void Object::Loader::finish()
 {
-       for(unsigned i=0; i<obj.textures.size(); ++i)
-       {
-               if(!obj.textures[i])
-               {
-                       obj.textures[i]=obj.technique->get_texture(i);
-                       if(!obj.textures[i])
-                               throw Exception("Object does not specify all textures required by Technique");
-               }
-       }
+       obj.update_bounding_sphere();
 }
 
-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)
 {
-       RefPtr<Material> mat=new Material;
-       load_sub(*mat);
-       coll.add(format("_%p", mat.get()), mat.get());
-       obj.material=mat.release();
+       if(l>obj.meshes.size())
+               throw out_of_range("Object::Loader::mesh_inline_lod");
+
+       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::shader_texture(const string &n)
+void Object::Loader::mesh_lod(unsigned l, const string &n)
 {
-       if(!obj.technique)
-               throw InvalidState("Can't specify shader textures without a Technique");
-
-       unsigned eqsign=n.find('=');
-       if(eqsign==string::npos)
-               throw InvalidParameterValue("Must specify texture slot name");
-
-       obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
+       obj.set_mesh(l, &get_collection().get<Mesh>(n));
 }
 
-void Object::Loader::technique(const string &n)
+void Object::Loader::technique_inline()
 {
-       obj.technique=coll.get<Technique>(n);
-       obj.textures.resize(obj.technique->get_n_textures());
-       obj.material=obj.technique->get_material();
+       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)
 {
-       if(obj.main_texture)
-               throw Exception("Only one main texture may be specified");
-
-       Texture *tex=coll.get<Texture>(n);
-       if(obj.technique)
-               obj.textures[obj.technique->get_texture_index("texture")]=tex;
-       obj.main_texture=tex;
+       obj.set_technique(&get_collection().get<Technique>(n));
 }
 
 } // namespace GL