]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Add a rendering supervisor class
[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",     static_cast<void (Loader::*)()>(&Loader::mesh));
123         add("mesh",     static_cast<void (Loader::*)(unsigned)>(&Loader::mesh));
124         add("mesh",     static_cast<void (Loader::*)(const std::string &)>(&Loader::mesh));
125         add("mesh",     static_cast<void (Loader::*)(unsigned, const std::string &)>(&Loader::mesh));
126         // Deprecated alias, will be removed
127         add("lod_mesh", static_cast<void (Loader::*)(unsigned, const std::string &)>(&Loader::mesh));
128         add("technique", static_cast<void (Loader::*)()>(&Loader::technique));
129         add("technique", static_cast<void (Loader::*)(const std::string &)>(&Loader::technique));
130 }
131
132 void Object::Loader::mesh()
133 {
134         RefPtr<Mesh> msh = new Mesh;
135         load_sub(*msh);
136         obj.meshes.front() = msh;
137 }
138
139 void Object::Loader::mesh(unsigned l)
140 {
141         if(l>obj.meshes.size())
142                 throw InvalidParameterValue("LODs must be continuous");
143
144         RefPtr<Mesh> msh = new Mesh;
145         load_sub(*msh);
146         if(l==obj.meshes.size())
147                 obj.meshes.push_back(msh);
148         else
149                 obj.meshes[l] = msh;
150 }
151
152 void Object::Loader::mesh(const std::string &n)
153 {
154         obj.set_mesh(get_collection().get<Mesh>(n));
155 }
156
157 void Object::Loader::mesh(unsigned l, const string &n)
158 {
159         obj.set_mesh(l, get_collection().get<Mesh>(n));
160 }
161
162 void Object::Loader::technique()
163 {
164         RefPtr<Technique> tech = new Technique;
165         if(coll)
166                 load_sub(*tech, get_collection());
167         else
168                 load_sub(*tech);
169         obj.technique = tech;
170 }
171
172 void Object::Loader::technique(const std::string &n)
173 {
174         obj.set_technique(get_collection().get<Technique>(n));
175 }
176
177 } // namespace GL
178 } // namespace Msp