]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Rename some Loader methods to avoid having to static_cast the pointers
[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 void Object::set_technique(const Technique *t)
47 {
48         technique = t;
49         technique.keep();
50 }
51
52 void Object::render(const Tag &tag) const
53 {
54         const RenderPass *pass = get_pass(tag);
55         if(!pass)
56                 return;
57
58         Bind bind_shader(pass->get_shader_program());
59         if(pass->get_shader_data())
60                 pass->get_shader_data()->apply();
61         Bind bind_material(pass->get_material());
62         Bind bind_texturing(pass->get_texturing());
63
64         meshes.front()->draw();
65 }
66
67 void Object::render(Renderer &renderer, const Tag &tag) const
68 {
69         const RenderPass *pass = get_pass(tag);
70         if(!pass)
71                 return;
72
73         Renderer::Push push(renderer);
74         renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
75         renderer.set_material(pass->get_material());
76         renderer.set_texturing(pass->get_texturing());
77
78         meshes.front()->draw(renderer);
79 }
80
81 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
82 {
83         const RenderPass *pass = get_pass(tag);
84         if(!pass)
85                 return;
86
87         Renderer::Push push(renderer);
88         renderer.set_shader(pass->get_shader_program(), pass->get_shader_data());
89         renderer.set_material(pass->get_material());
90         renderer.set_texturing(pass->get_texturing());
91
92         inst.setup_render(renderer, tag);
93         unsigned lod = min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
94         meshes[lod]->draw(renderer);
95         inst.finish_render(renderer, tag);
96 }
97
98 const RenderPass *Object::get_pass(const Tag &tag) const
99 {
100         if(!technique || !technique->has_pass(tag))
101                 return 0;
102         return &technique->get_pass(tag);
103 }
104
105
106 Object::Loader::Loader(Object &o):
107         DataFile::CollectionObjectLoader<Object>(o, 0)
108 {
109         init();
110 }
111
112 Object::Loader::Loader(Object &o, Collection &c):
113         DataFile::CollectionObjectLoader<Object>(o, &c)
114 {
115         init();
116 }
117
118 void Object::Loader::init()
119 {
120         allow_pointer_reload = false;
121
122         add("mesh",     &Loader::mesh_inline);
123         add("mesh",     &Loader::mesh_inline_lod);
124         add("mesh",     &Loader::mesh);
125         add("mesh",     &Loader::mesh_lod);
126         add("technique", &Loader::technique_inline);
127         add("technique", &Loader::technique);
128
129         // Deprecated alias, will be removed
130         add("lod_mesh", &Loader::mesh_lod);
131 }
132
133 void Object::Loader::mesh_inline()
134 {
135         RefPtr<Mesh> msh = new Mesh;
136         load_sub(*msh);
137         obj.meshes.front() = msh;
138 }
139
140 void Object::Loader::mesh_inline_lod(unsigned l)
141 {
142         if(l>obj.meshes.size())
143                 throw InvalidParameterValue("LODs must be continuous");
144
145         RefPtr<Mesh> msh = new Mesh;
146         load_sub(*msh);
147         if(l==obj.meshes.size())
148                 obj.meshes.push_back(msh);
149         else
150                 obj.meshes[l] = msh;
151 }
152
153 void Object::Loader::mesh(const std::string &n)
154 {
155         obj.set_mesh(get_collection().get<Mesh>(n));
156 }
157
158 void Object::Loader::mesh_lod(unsigned l, const string &n)
159 {
160         obj.set_mesh(l, get_collection().get<Mesh>(n));
161 }
162
163 void Object::Loader::technique_inline()
164 {
165         RefPtr<Technique> tech = new Technique;
166         if(coll)
167                 load_sub(*tech, get_collection());
168         else
169                 load_sub(*tech);
170         obj.technique = tech;
171 }
172
173 void Object::Loader::technique(const std::string &n)
174 {
175         obj.set_technique(get_collection().get<Technique>(n));
176 }
177
178 } // namespace GL
179 } // namespace Msp