]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.cpp
Refactor the structure of sequence template files
[libs/gl.git] / source / builders / sequencetemplate.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/strings/format.h>
4 #include "ambientocclusion.h"
5 #include "blend.h"
6 #include "bloom.h"
7 #include "colorcurve.h"
8 #include "lighting.h"
9 #include "object.h"
10 #include "resources.h"
11 #include "scene.h"
12 #include "sequencetemplate.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 SequenceTemplate::~SequenceTemplate()
20 {
21         for(const PostProcessor &p: postprocessors)
22                 delete p.postprocessor_template;
23 }
24
25 template<>
26 SequenceTemplate::TemplateRegistry<GL::PostProcessor> &SequenceTemplate::get_registry<GL::PostProcessor>()
27 {
28         static TemplateRegistry<GL::PostProcessor> registry;
29         static bool initialized = false;
30         if(!initialized)
31         {
32                 registry.register_type<AmbientOcclusion::Template>("ambient_occlusion");
33                 registry.register_type<Bloom::Template>("bloom");
34                 registry.register_type<ColorCurve::Template>("colorcurve");
35                 initialized = true;
36         }
37         return registry;
38 }
39
40
41 SequenceTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
42         postprocessor_template(ppt)
43 { }
44
45
46 SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
47         CollectionObjectLoader<SequenceTemplate>(t, &c)
48 {
49         add("hdr", &SequenceTemplate::hdr);
50         add("alpha", &SequenceTemplate::alpha);
51         add("clear", &Loader::clear);
52         add("multisample", &Loader::multisample);
53         add("multisample", &Loader::multisample_range);
54         add("postprocessor", &Loader::postprocessor);
55         add("postprocessor", &Loader::postprocessor_with_slot);
56         add("renderable", &Loader::renderable);
57         add("renderable", &Loader::renderable_with_default);
58         add("step", &Loader::step);
59 }
60
61 void SequenceTemplate::Loader::clear()
62 {
63         ClearLoader ldr(obj);
64         load_sub_with(ldr);
65         obj.clear_enabled = true;
66 }
67
68 void SequenceTemplate::Loader::multisample(unsigned samples)
69 {
70         obj.required_multisample = samples;
71         obj.max_multisample = samples;
72 }
73
74 void SequenceTemplate::Loader::multisample_range(unsigned req, unsigned max)
75 {
76         obj.required_multisample = req;
77         obj.max_multisample = max;
78 }
79
80 void SequenceTemplate::Loader::postprocessor()
81 {
82         postprocessor_with_slot(string());
83 }
84
85 void SequenceTemplate::Loader::postprocessor_with_slot(const string &slot)
86 {
87         TemplateLoader<GL::PostProcessor> ldr(get_collection());
88         load_sub_with(ldr);
89         PostProcessor pp;
90         pp.postprocessor_template = ldr.get_object();
91         pp.slot_name = slot;
92         obj.postprocessors.push_back(pp);
93 }
94
95 void SequenceTemplate::Loader::renderable(const string &slot)
96 {
97         Renderable rend;
98         rend.slot_name = slot;
99         obj.renderables.push_back(rend);
100 }
101
102 void SequenceTemplate::Loader::renderable_with_default(const string &slot, const string &name)
103 {
104         Renderable rend;
105         rend.renderable = &get_collection().get<GL::Renderable>(name);
106         rend.slot_name = slot;
107         obj.renderables.push_back(rend);
108 }
109
110 void SequenceTemplate::Loader::step(const string &tag, const string &rend)
111 {
112         Step stp;
113         stp.tag = tag;
114         stp.renderable_name = rend;
115         Step::Loader ldr(stp, get_collection());
116         ldr.set_inline_base_name(format("%s/%d.step", get_source(), obj.steps.size()));
117         load_sub_with(ldr);
118
119         obj.steps.push_back(stp);
120 }
121
122
123 SequenceTemplate::ClearLoader::ClearLoader(SequenceTemplate &t):
124         ObjectLoader<SequenceTemplate>(t)
125 {
126         add("color", &ClearLoader::color);
127         add("depth", &ClearLoader::depth);
128         add("stencil", &ClearLoader::stencil);
129 }
130
131 void SequenceTemplate::ClearLoader::color(float r, float g, float b, float a)
132 {
133         obj.clear_colors.push_back(Color(r, g, b, a));
134 }
135
136 void SequenceTemplate::ClearLoader::depth(float d)
137 {
138         obj.clear_depth = d;
139 }
140
141 void SequenceTemplate::ClearLoader::stencil(int s)
142 {
143         obj.clear_stencil = s;
144 }
145
146
147 SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c):
148         DataFile::CollectionObjectLoader<Step>(p, &c)
149 {
150         add("depth_test", &Loader::depth_test);
151         add("depth_test", &Loader::depth_compare);
152         add("lighting", &Loader::lighting);
153         add("lighting", &Loader::lighting_inline);
154         add("stencil_test", &Loader::stencil_test);
155 }
156
157 void SequenceTemplate::Step::Loader::set_inline_base_name(const string &n)
158 {
159         inline_base_name = n;
160 }
161
162 void SequenceTemplate::Step::Loader::depth_test()
163 {
164         load_sub(obj.depth_test);
165 }
166
167 void SequenceTemplate::Step::Loader::depth_compare(Predicate c)
168 {
169         obj.depth_test = DepthTest(c);
170 }
171
172 void SequenceTemplate::Step::Loader::lighting_inline()
173 {
174         RefPtr<Lighting> lightn = new Lighting;
175         load_sub(*lightn, get_collection());
176         get_collection().add(inline_base_name+".lightn", lightn.get());
177         obj.lighting = lightn.release();
178 }
179
180 void SequenceTemplate::Step::Loader::lighting(const string &name)
181 {
182         obj.lighting = &get_collection().get<Lighting>(name);
183 }
184
185 void SequenceTemplate::Step::Loader::stencil_test()
186 {
187         load_sub(obj.stencil_test);
188 }
189
190 } // namespace GL
191 } // namespace Msp