X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=9aba5466e3e1cf548674b61f2a4b70f156eb8a93;hb=e720536d5feacf6136b5621c98b72d206e44f64c;hp=d5d5231bab47dbdd9358a489ea975841298c4a87;hpb=e759062876ee7fc81d1c2f40818d5bf97898d53d;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index d5d5231b..9aba5466 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,22 +1,15 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include "except.h" +#include +#include #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,180 +17,221 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1, static_cast(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); } -void Object::render(const Tag &tag) const +void Object::set_mesh(unsigned i, const Mesh *m) { - if(!can_render(tag)) - return; + if(i>meshes.size()) + throw out_of_range("Object::set_mesh"); + + if(i==meshes.size()) + meshes.push_back(m); + else + { + 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); - const ObjectPass *pass=get_pass(tag); - setup_render(pass); - meshes[0]->draw(); - finish_render(pass); + update_bounding_sphere(); } -void Object::render(const ObjectInstance &inst, const Tag &tag) const +void Object::update_bounding_sphere() { - if(!can_render(tag)) - return; + vector points; + for(vector >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i) + { + 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; jdraw(); - finish_render(pass); + bounding_sphere = Geometry::BoundingSphere::from_point_cloud(points.begin(), points.end()); } -bool Object::can_render(const Tag &tag) const +const Mesh *Object::get_mesh(unsigned i) const { - if(technique) - return technique->has_pass(tag); - else - return tag.id==0; + 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; ibind(); - } - } - 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); + + setup_render(renderer, tag); + meshes.front()->draw(renderer); + finish_render(renderer, tag); +} + +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); + pass->apply(renderer); + + setup_render(renderer, tag); + inst.setup_render(renderer, tag); + unsigned lod = min(inst.get_level_of_detail(renderer), meshes.size()-1); + meshes[lod]->draw(renderer); + inst.finish_render(renderer, tag); + finish_render(renderer, tag); +} - if(material) - Material::unbind(); +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::render_instance(const ObjectInstance &inst, const Tag &tag) const +void Object::resource_loaded(Resource &res) { - inst.setup_render(tag); - unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1); - meshes[lod]->draw(); - inst.finish_render(tag); + if(!meshes.empty() && &res==meshes.front().get() && bounding_sphere.is_empty()) + update_bounding_sphere(); } +Object::Loader::Loader(Object &o): + DataFile::CollectionObjectLoader(o, 0) +{ + init(); +} + Object::Loader::Loader(Object &o, Collection &c): - obj(o), - coll(c) + DataFile::CollectionObjectLoader(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() { - if(obj.technique && !obj.main_texture) - obj.main_texture=obj.technique->get_main_texture(); - for(unsigned i=0; iget_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(n); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes.front() = msh; } -void Object::Loader::material_inline() +void Object::Loader::mesh_inline_lod(unsigned l) { - RefPtr 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 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(n); + obj.set_mesh(&get_collection().get(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"); - - string::size_type 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(n.substr(eqsign+1)); + obj.set_mesh(l, &get_collection().get(n)); } -void Object::Loader::technique(const string &n) +void Object::Loader::technique_inline() { - obj.technique=coll.get(n); - obj.textures.resize(obj.technique->get_n_textures()); - obj.material=obj.technique->get_material(); + RefPtr 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(n); - if(obj.technique) - obj.textures[obj.technique->get_texture_index("texture")]=tex; - obj.main_texture=tex; + obj.set_technique(&get_collection().get(n)); } } // namespace GL