]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Add a method to get the mesh of an Object
[libs/gl.git] / source / object.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/datafile/collection.h>
9 #include <msp/strings/formatter.h>
10 #include "except.h"
11 #include "material.h"
12 #include "mesh.h"
13 #include "object.h"
14 #include "objectinstance.h"
15 #include "program.h"
16 #include "programdata.h"
17 #include "renderer.h"
18 #include "technique.h"
19 #include "texturing.h"
20
21 using namespace std;
22
23 namespace Msp {
24 namespace GL {
25
26 Object::Object():
27         meshes(1)
28 { }
29
30 Object::~Object()
31 {
32 }
33
34 void Object::set_mesh(unsigned i, const Mesh *m)
35 {
36         if(i>meshes.size())
37                 throw InvalidParameterValue("LODs must be continuous");
38
39         if(i==meshes.size())
40                 meshes.push_back(m);
41         else
42                 meshes[i] = m;
43         meshes[i].keep();
44 }
45
46 const Mesh *Object::get_mesh(unsigned i) const
47 {
48         if(i>=meshes.size())
49                 return 0;
50
51         return meshes[i].get();
52 }
53
54 void Object::set_technique(const Technique *t)
55 {
56         technique = t;
57         technique.keep();
58 }
59
60 void Object::render(const Tag &tag) const
61 {
62         const RenderPass *pass = get_pass(tag);
63         if(!pass)
64                 return;
65
66         Bind bind_shader(pass->get_shader_program());
67         if(pass->get_shader_data())
68                 pass->get_shader_data()->apply();
69         Bind bind_material(pass->get_material());
70         Bind bind_texturing(pass->get_texturing());
71
72         meshes.front()->draw();
73 }
74
75 void Object::render(Renderer &renderer, const Tag &tag) const
76 {
77         const RenderPass *pass = get_pass(tag);
78         if(!pass)
79                 return;
80
81         Renderer::Push push(renderer);
82         renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
83         renderer.set_material(pass->get_material());
84         renderer.set_texturing(pass->get_texturing());
85
86         meshes.front()->draw(renderer);
87 }
88
89 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
90 {
91         const RenderPass *pass = get_pass(tag);
92         if(!pass)
93                 return;
94
95         Renderer::Push push(renderer);
96         renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
97         renderer.set_material(pass->get_material());
98         renderer.set_texturing(pass->get_texturing());
99
100         inst.setup_render(renderer, tag);
101         unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
102         meshes[lod]->draw(renderer);
103         inst.finish_render(renderer, tag);
104 }
105
106 const RenderPass *Object::get_pass(const Tag &tag) const
107 {
108         if(!technique || !technique->has_pass(tag))
109                 return 0;
110         return &technique->get_pass(tag);
111 }
112
113
114 Object::Loader::Loader(Object &o):
115         DataFile::CollectionObjectLoader<Object>(o, 0)
116 {
117         init();
118 }
119
120 Object::Loader::Loader(Object &o, Collection &c):
121         DataFile::CollectionObjectLoader<Object>(o, &c)
122 {
123         init();
124 }
125
126 void Object::Loader::init()
127 {
128         allow_pointer_reload = false;
129
130         add("mesh",     &Loader::mesh_inline);
131         add("mesh",     &Loader::mesh_inline_lod);
132         add("mesh",     &Loader::mesh);
133         add("mesh",     &Loader::mesh_lod);
134         add("technique", &Loader::technique_inline);
135         add("technique", &Loader::technique);
136
137         // Deprecated alias, will be removed
138         add("lod_mesh", &Loader::mesh_lod);
139 }
140
141 void Object::Loader::mesh_inline()
142 {
143         RefPtr<Mesh> msh = new Mesh;
144         load_sub(*msh);
145         obj.meshes.front() = msh;
146 }
147
148 void Object::Loader::mesh_inline_lod(unsigned l)
149 {
150         if(l>obj.meshes.size())
151                 throw InvalidParameterValue("LODs must be continuous");
152
153         RefPtr<Mesh> msh = new Mesh;
154         load_sub(*msh);
155         if(l==obj.meshes.size())
156                 obj.meshes.push_back(msh);
157         else
158                 obj.meshes[l] = msh;
159 }
160
161 void Object::Loader::mesh(const std::string &n)
162 {
163         obj.set_mesh(get_collection().get<Mesh>(n));
164 }
165
166 void Object::Loader::mesh_lod(unsigned l, const string &n)
167 {
168         obj.set_mesh(l, get_collection().get<Mesh>(n));
169 }
170
171 void Object::Loader::technique_inline()
172 {
173         RefPtr<Technique> tech = new Technique;
174         if(coll)
175                 load_sub(*tech, get_collection());
176         else
177                 load_sub(*tech);
178         obj.technique = tech;
179 }
180
181 void Object::Loader::technique(const std::string &n)
182 {
183         obj.set_technique(get_collection().get<Technique>(n));
184 }
185
186 } // namespace GL
187 } // namespace Msp