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