1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
6 #include "objectinstance.h"
8 #include "programdata.h"
10 #include "technique.h"
11 #include "texturing.h"
22 Object::Object(const Mesh *m, const Technique *t)
32 void Object::set_mesh(unsigned i, const Mesh *m)
35 throw out_of_range("Object::set_mesh");
44 const Mesh *Object::get_mesh(unsigned i) const
49 return meshes[i].get();
52 void Object::set_technique(const Technique *t)
58 void Object::render(const Tag &tag) const
60 const RenderPass *pass = get_pass(tag);
64 Bind bind_shader(pass->get_shader_program());
65 if(pass->get_shader_data())
66 pass->get_shader_data()->apply();
67 Bind bind_material(pass->get_material());
68 Bind bind_texturing(pass->get_texturing());
70 meshes.front()->draw();
73 void Object::render(Renderer &renderer, const Tag &tag) const
75 const RenderPass *pass = get_pass(tag);
79 Renderer::Push push(renderer);
80 renderer.set_shader_program(pass->get_shader_program(), pass->get_shader_data());
81 renderer.set_material(pass->get_material());
82 renderer.set_texturing(pass->get_texturing());
84 meshes.front()->draw(renderer);
87 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
89 const RenderPass *pass = get_pass(tag);
93 Renderer::Push push(renderer);
94 renderer.set_shader_program(pass->get_shader_program(), pass->get_shader_data());
95 renderer.set_material(pass->get_material());
96 renderer.set_texturing(pass->get_texturing());
98 inst.setup_render(renderer, tag);
99 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), meshes.size()-1);
100 meshes[lod]->draw(renderer);
101 inst.finish_render(renderer, tag);
104 const RenderPass *Object::get_pass(const Tag &tag) const
106 if(!technique || !technique->has_pass(tag))
108 return &technique->get_pass(tag);
112 Object::Loader::Loader(Object &o):
113 DataFile::CollectionObjectLoader<Object>(o, 0)
118 Object::Loader::Loader(Object &o, Collection &c):
119 DataFile::CollectionObjectLoader<Object>(o, &c)
124 void Object::Loader::init()
126 add("mesh", &Loader::mesh_inline);
127 add("mesh", &Loader::mesh_inline_lod);
128 add("mesh", &Loader::mesh);
129 add("mesh", &Loader::mesh_lod);
130 add("technique", &Loader::technique_inline);
131 add("technique", &Loader::technique);
133 // Deprecated alias, will be removed
134 add("lod_mesh", &Loader::mesh_lod);
137 void Object::Loader::mesh_inline()
139 RefPtr<Mesh> msh = new Mesh;
141 obj.meshes.front() = msh;
144 void Object::Loader::mesh_inline_lod(unsigned l)
146 if(l>obj.meshes.size())
147 throw out_of_range("Object::Loader::mesh_inline_lod");
149 RefPtr<Mesh> msh = new Mesh;
151 if(l==obj.meshes.size())
152 obj.meshes.push_back(msh);
157 void Object::Loader::mesh(const std::string &n)
159 obj.set_mesh(&get_collection().get<Mesh>(n));
162 void Object::Loader::mesh_lod(unsigned l, const string &n)
164 obj.set_mesh(l, &get_collection().get<Mesh>(n));
167 void Object::Loader::technique_inline()
169 RefPtr<Technique> tech = new Technique;
171 load_sub(*tech, get_collection());
174 obj.technique = tech;
177 void Object::Loader::technique(const std::string &n)
179 obj.set_technique(&get_collection().get<Technique>(n));