X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=637af62a3753becb9407e346fb471bad9213f94a;hb=e042e093a4761f1227d4dd80d695e01642e17ffd;hp=5ae04feb03027dd078a99335d651adce924bfa2c;hpb=42ace9ac1350d3ae009bdd2fb335ac1e57d1b36b;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 5ae04feb..637af62a 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,22 +1,14 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include -#include -#include "except.h" +#include #include "material.h" #include "mesh.h" #include "object.h" #include "objectinstance.h" #include "program.h" #include "programdata.h" +#include "renderer.h" #include "technique.h" -#include "texture.h" -#include "texunit.h" +#include "texturing.h" using namespace std; @@ -24,18 +16,69 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1), - technique(0), - own_mesh(false), - own_technique(false) + 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() +{ } + +void Object::set_mesh(unsigned i, const Mesh *m) +{ + if(i>meshes.size()) + throw out_of_range("Object::set_mesh"); + + if(i==meshes.size()) + meshes.push_back(m); + else + meshes[i] = m; + meshes[i].keep(); +} + +void Object::update_bounding_sphere() { - if(own_technique) - delete technique; - if(own_mesh) - delete meshes[0]; + 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); + if(offset<0) + { + // TODO Handle two-dimensional meshes + bounding_sphere = Geometry::BoundingSphere(); + return; + } + + unsigned n_vertices = vertices.size(); + points.reserve(points.size()+n_vertices); + for(unsigned j=0; j::from_point_cloud(points.begin(), points.end()); +} + +const Mesh *Object::get_mesh(unsigned i) const +{ + if(i>=meshes.size()) + return 0; + + return meshes[i].get(); +} + +void Object::set_technique(const Technique *t) +{ + technique = t; + technique.keep(); } void Object::render(const Tag &tag) const @@ -44,34 +87,53 @@ void Object::render(const Tag &tag) const if(!pass) return; - Bind bind(pass); - meshes[0]->draw(); + 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()); + + meshes.front()->draw(); } -void Object::render(const ObjectInstance &inst, const Tag &tag) const +void Object::render(Renderer &renderer, const Tag &tag) const { const RenderPass *pass = get_pass(tag); if(!pass) return; - Bind bind(pass); - render_instance(inst, tag); - meshes[0]->draw(); + Renderer::Push push(renderer); + pass->apply(renderer); + + setup_render(renderer, tag); + meshes.front()->draw(renderer); + finish_render(renderer, tag); } -const RenderPass *Object::get_pass(const Tag &tag) const +void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const { - if(!technique || !technique->has_pass(tag)) - return 0; - return &technique->get_pass(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(inst.get_level_of_detail(renderer), meshes.size()-1); + meshes[lod]->draw(renderer); + inst.finish_render(renderer, tag); + finish_render(renderer, tag); } -void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const +const RenderPass *Object::get_pass(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); + if(!technique) + throw logic_error("!technique"); + if(!technique->has_pass(tag)) + return 0; + return &technique->get_pass(tag); } @@ -89,49 +151,62 @@ Object::Loader::Loader(Object &o, Collection &c): void Object::Loader::init() { - allow_pointer_reload = false; - - add("lod_mesh", &Loader::lod_mesh); - add("mesh", static_cast(&Loader::mesh)); - add("mesh", static_cast(&Loader::mesh)); + 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); - add("technique", &Object::technique); } -void Object::Loader::lod_mesh(unsigned l, const string &n) +void Object::Loader::finish() { - obj.meshes.resize(l+1, 0); - obj.meshes[l] = get_collection().get(n); + obj.update_bounding_sphere(); } -void Object::Loader::mesh() +void Object::Loader::mesh_inline() { - if(obj.meshes[0]) - throw InvalidState("A mesh is already loaded"); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes.front() = msh; +} + +void Object::Loader::mesh_inline_lod(unsigned l) +{ + if(l>obj.meshes.size()) + throw out_of_range("Object::Loader::mesh_inline_lod"); RefPtr msh = new Mesh; load_sub(*msh); - obj.meshes[0] = msh.release(); - obj.own_mesh = true; + if(l==obj.meshes.size()) + obj.meshes.push_back(msh); + else + obj.meshes[l] = msh; } void Object::Loader::mesh(const std::string &n) { - if(obj.meshes[0]) - throw InvalidState("A mesh is already loaded"); + obj.set_mesh(&get_collection().get(n)); +} - obj.meshes[0] = get_collection().get(n); +void Object::Loader::mesh_lod(unsigned l, const string &n) +{ + obj.set_mesh(l, &get_collection().get(n)); } -void Object::Loader::technique() +void Object::Loader::technique_inline() { RefPtr tech = new Technique; if(coll) load_sub(*tech, get_collection()); else load_sub(*tech); - obj.technique = tech.release(); - obj.own_technique = true; + obj.technique = tech; +} + +void Object::Loader::technique(const std::string &n) +{ + obj.set_technique(&get_collection().get(n)); } } // namespace GL