]> git.tdb.fi Git - libs/gl.git/blob - sequencetemplate.cpp
e8e451fc9a54d78a5b6d1d1bfc415a64ffef90be
[libs/gl.git] / 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
26 SequenceTemplate::PostProcessorRegistry &SequenceTemplate::get_postprocessor_registry()
27 {
28         static PostProcessorRegistry registry;
29         static bool initialized = false;
30         if(!initialized)
31         {
32                 registry.register_type<AmbientOcclusion>("ambient_occlusion");
33                 registry.register_type<Bloom>("bloom");
34                 registry.register_type<ColorCurve>("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::PostProcLoader::PostProcLoader()
47 {
48         get_postprocessor_registry().invoke_all(*this);
49 }
50
51
52 SequenceTemplate::Loader::Loader(SequenceTemplate &t, Collection &c):
53         DataFile::CollectionObjectLoader<SequenceTemplate, Resources>(t, &c)
54 {
55         add("hdr", &SequenceTemplate::hdr);
56         add("alpha", &SequenceTemplate::alpha);
57         add("clear", &Loader::clear);
58         add("multisample", &Loader::multisample);
59         add("multisample", &Loader::multisample_range);
60         add("postprocessor", &Loader::postprocessor);
61         add("step", &Loader::step);
62         add("step", &Loader::step_with_slot);
63
64         // Deprecated
65         add("pass", &Loader::step_with_slot);
66 }
67
68 void SequenceTemplate::Loader::postprocessor_loaded()
69 {
70         obj.postprocessors.push_back(get_postprocessor_template());
71 }
72
73 void SequenceTemplate::Loader::clear()
74 {
75         ClearLoader ldr(obj);
76         load_sub_with(ldr);
77         obj.clear_enabled = true;
78 }
79
80 void SequenceTemplate::Loader::multisample(unsigned samples)
81 {
82         obj.required_multisample = samples;
83         obj.max_multisample = samples;
84 }
85
86 void SequenceTemplate::Loader::multisample_range(unsigned req, unsigned max)
87 {
88         obj.required_multisample = req;
89         obj.max_multisample = max;
90 }
91
92 void SequenceTemplate::Loader::postprocessor(const string &slot)
93 {
94         PostProcLoader ldr;
95         load_sub_with(ldr);
96         PostProcessor pp;
97         pp.postprocessor_template = ldr.get_postprocessor_template();
98         pp.slot_name = slot;
99         obj.postprocessors.push_back(pp);
100 }
101
102 void SequenceTemplate::Loader::step(const string &tag)
103 {
104         step_with_slot(tag, string());
105 }
106
107 void SequenceTemplate::Loader::step_with_slot(const string &tag, const string &rend)
108 {
109         Step stp;
110         stp.tag = tag;
111         stp.slot_name = rend;
112         Step::Loader ldr(stp, *coll);
113         ldr.set_inline_base_name(format("%s/%d.step", get_source(), obj.steps.size()));
114         load_sub_with(ldr);
115
116         obj.steps.push_back(stp);
117 }
118
119
120 SequenceTemplate::ClearLoader::ClearLoader(SequenceTemplate &t):
121         ObjectLoader<SequenceTemplate>(t)
122 {
123         add("color", &ClearLoader::color);
124         add("depth", &ClearLoader::depth);
125         add("stencil", &ClearLoader::stencil);
126 }
127
128 void SequenceTemplate::ClearLoader::color(float r, float g, float b, float a)
129 {
130         obj.clear_colors.push_back(Color(r, g, b, a));
131 }
132
133 void SequenceTemplate::ClearLoader::depth(float d)
134 {
135         obj.clear_depth = d;
136 }
137
138 void SequenceTemplate::ClearLoader::stencil(int s)
139 {
140         obj.clear_stencil = s;
141 }
142
143
144 SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c):
145         DataFile::CollectionObjectLoader<Step>(p, &c)
146 {
147         add("blend", &Loader::blend);
148         add("blend", &Loader::blend_factors);
149         add("depth_test", &Loader::depth_test);
150         add("depth_test", &Loader::depth_compare);
151         add("lighting", &Loader::lighting);
152         add("lighting", &Loader::lighting_inline);
153         add("object", &Loader::object);
154         add("scene", &Loader::scene);
155         add("stencil_test", &Loader::stencil_test);
156 }
157
158 void SequenceTemplate::Step::Loader::set_inline_base_name(const string &n)
159 {
160         inline_base_name = n;
161 }
162
163 void SequenceTemplate::Step::Loader::blend()
164 {
165         load_sub(obj.blend);
166 }
167
168 void SequenceTemplate::Step::Loader::blend_factors(BlendFactor src, BlendFactor dest)
169 {
170         obj.blend = Blend(src, dest);
171 }
172
173 void SequenceTemplate::Step::Loader::depth_test()
174 {
175         load_sub(obj.depth_test);
176 }
177
178 void SequenceTemplate::Step::Loader::depth_compare(Predicate c)
179 {
180         obj.depth_test = DepthTest(c);
181 }
182
183 void SequenceTemplate::Step::Loader::lighting_inline()
184 {
185         RefPtr<Lighting> lightn = new Lighting;
186         load_sub(*lightn, get_collection());
187         get_collection().add(inline_base_name+".lightn", lightn.get());
188         obj.lighting = lightn.release();
189 }
190
191 void SequenceTemplate::Step::Loader::lighting(const string &name)
192 {
193         obj.lighting = &get_collection().get<Lighting>(name);
194 }
195
196 void SequenceTemplate::Step::Loader::object(const string &name)
197 {
198         obj.default_renderable = &get_collection().get<GL::Object>(name);
199 }
200
201 void SequenceTemplate::Step::Loader::scene(const string &name)
202 {
203         obj.default_renderable = &get_collection().get<Scene>(name);
204 }
205
206 void SequenceTemplate::Step::Loader::stencil_test()
207 {
208         load_sub(obj.stencil_test);
209 }
210
211 } // namespace GL
212 } // namespace Msp