-/* $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 "error.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;
namespace GL {
Object::Object():
- meshes(1, static_cast<Mesh *>(0)),
- technique(0),
- main_texture(0),
- material(0)
+ lods(1),
+ lod0_watched(false)
{ }
-Object::~Object()
+Object::Object(const Mesh *m, const Technique *t):
+ lods(1),
+ lod0_watched(false)
{
+ set_mesh(m);
+ set_technique(t);
}
-bool Object::has_pass(const Tag &tag) const
+Object::~Object()
{
- if(technique)
- return technique->has_pass(tag);
- else
- return tag.id==0;
+ 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 Tag &tag) const
+Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
{
- const ObjectPass *pass=get_pass(tag);
- setup_render(pass);
- meshes[0]->draw();
- finish_render(pass);
-}
+ if(i>lods.size())
+ throw out_of_range(caller);
+ if(i>0 && (!lods[0].mesh || !lods[0].technique))
+ throw invalid_operation(caller);
-void Object::render(const ObjectInstance &inst, const Tag &tag) const
-{
- const ObjectPass *pass=get_pass(tag);
- setup_render(pass);
- render_instance(inst, tag);
- meshes[0]->draw();
- finish_render(pass);
-}
+ if(i==lods.size())
+ lods.push_back(lods.back());
-const ObjectPass *Object::get_pass(const Tag &tag) const
-{
- if(technique)
- return &technique->get_pass(tag);
- else if(tag.id==0)
- return 0;
- throw KeyError("Unknown pass");
+ return lods[i];
}
-void Object::setup_render(const ObjectPass *pass) const
+void Object::set_mesh(unsigned i, const Mesh *m)
{
- if(!meshes[0])
- throw InvalidState("Trying to render Object without mesh");
+ 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;
- if(pass && pass->shprog)
- {
- pass->shprog->bind();
- pass->shdata->apply();
- for(unsigned i=0; i<textures.size(); ++i)
+ if(i==0 && m)
+ if(ResourceManager *rm = m->get_manager())
{
- TexUnit::activate(i);
- textures[i]->bind();
+ rm->watch_resource(*m, *this);
+ lod0_watched = true;
}
- }
- else if(main_texture && (!pass || pass->use_textures))
- main_texture->bind();
- if(material)
- material->bind();
+ update_bounding_sphere();
}
-void Object::finish_render(const ObjectPass *pass) const
+void Object::update_bounding_sphere()
{
- if(pass && pass->shprog)
+ vector<Vector3> points;
+ for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
{
- Program::unbind();
- for(unsigned i=textures.size(); i--;)
+ if(!i->mesh || !i->mesh->is_loaded())
+ continue;
+
+ const VertexArray &vertices = i->mesh->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)
{
- TexUnit::activate(i);
- Texture::unbind();
+ const float *v = vertices[j];
+ points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
}
}
- else if(main_texture)
- Texture::unbind();
- if(material)
- Material::unbind();
+ /* 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::render_instance(const ObjectInstance &inst, const Tag &tag) const
+const Mesh *Object::get_mesh(unsigned i) const
{
- inst.setup_render(tag);
- unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
- meshes[lod]->draw();
- inst.finish_render(tag);
+ if(i>=lods.size())
+ return 0;
+
+ return lods[i].mesh.get();
}
+void Object::set_technique(unsigned i, const Technique *t)
+{
+ RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
+ ptr = t;
+ ptr.keep();
+}
-Object::Loader::Loader(Object &o, Collection &c):
- obj(o),
- coll(c)
+const Technique *Object::get_technique(unsigned i) const
{
- add("lod_mesh", &Loader::lod_mesh);
- add("material", &Object::material);
- add("material_inline", &Loader::material_inline);
- add("mesh", &Loader::mesh);
- add("shader_texture", &Loader::shader_texture);
- add("technique", &Loader::technique);
- add("texture", &Loader::texture);
+ if(i>=lods.size())
+ return 0;
+
+ return lods[i].technique.get();
}
-void Object::Loader::finish()
+void Object::render(Renderer &renderer, const Tag &tag) const
{
- if(obj.technique && !obj.main_texture)
- obj.main_texture=obj.technique->get_main_texture();
- 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");
- }
- }
+ 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::Loader::lod_mesh(unsigned l, const string &n)
+void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
{
- obj.meshes.resize(l+1, 0);
- obj.meshes[l]=coll.get<Mesh>(n);
+ 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();
}
-void Object::Loader::material_inline()
+Object::Loader::Loader(Object &o, Collection &c):
+ LodLoader(o, 0, &c)
+{
+ init();
+}
+
+void Object::Loader::init()
+{
+ add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
+ add("level_of_detail", &Loader::level_of_detail);
+}
+
+void Object::Loader::finish()
{
- RefPtr<Material> mat=new Material;
- load_sub(*mat);
- coll.add(format("_%p", mat.get()), mat.get());
- obj.material=mat.release();
+ obj.update_bounding_sphere();
}
-void Object::Loader::mesh(const string &n)
+void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
{
- obj.meshes[0]=coll.get<Mesh>(n);
+ obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
}
-void Object::Loader::shader_texture(const string &n)
+void Object::Loader::level_of_detail(unsigned i)
{
- if(!obj.technique)
- throw InvalidState("Can't specify shader textures without a Technique");
+ LodLoader ldr(obj, i, coll);
+ load_sub_with(ldr);
+}
+
- unsigned eqsign=n.find('=');
- if(eqsign==string::npos)
- throw InvalidParameterValue("Must specify texture slot name");
+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);
+}
- obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
+void Object::LodLoader::mesh(const string &n)
+{
+ obj.set_mesh(index, &get_collection().get<Mesh>(n));
}
-void Object::Loader::technique(const string &n)
+void Object::LodLoader::mesh_inline()
{
- obj.technique=coll.get<Technique>(n);
- obj.textures.resize(obj.technique->get_n_textures());
- obj.material=obj.technique->get_material();
+ RefPtr<Mesh> msh = new Mesh;
+ load_sub(*msh);
+ lod.mesh = msh;
}
-void Object::Loader::texture(const string &n)
+void Object::LodLoader::technique(const std::string &n)
{
- if(obj.main_texture)
- throw Exception("Only one main texture may be specified");
+ obj.set_technique(index, &get_collection().get<Technique>(n));
+}
- Texture *tex=coll.get<Texture>(n);
- if(obj.technique)
- obj.textures[obj.technique->get_texture_index("texture")]=tex;
- obj.main_texture=tex;
+void Object::LodLoader::technique_inline()
+{
+ RefPtr<Technique> tech = new Technique;
+ if(coll)
+ load_sub(*tech, get_collection());
+ else
+ load_sub(*tech);
+ lod.technique = tech;
}
} // namespace GL