]> git.tdb.fi Git - libs/gl.git/blob - source/pipelinetemplate.cpp
14f6e2c024eed34fc123112161b15465e3f60cb5
[libs/gl.git] / source / pipelinetemplate.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include "ambientocclusion.h"
4 #include "blend.h"
5 #include "bloom.h"
6 #include "colorcurve.h"
7 #include "lighting.h"
8 #include "pipelinetemplate.h"
9 #include "tests.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 PipelineTemplate::PipelineTemplate():
17         hdr(false),
18         required_multisample(0),
19         max_multisample(0)
20 { }
21
22 PipelineTemplate::~PipelineTemplate()
23 {
24         for(PostProcessorArray::iterator i=postprocessors.begin(); i!=postprocessors.end(); ++i)
25                 delete i->postprocessor_template;
26 }
27
28
29 PipelineTemplate::PostProcessorRegistry &PipelineTemplate::get_postprocessor_registry()
30 {
31         static PostProcessorRegistry registry;
32         static bool initialized = false;
33         if(!initialized)
34         {
35                 registry.register_type<AmbientOcclusion>("ambient_occlusion");
36                 registry.register_type<Bloom>("bloom");
37                 registry.register_type<ColorCurve>("colorcurve");
38                 initialized = true;
39         }
40         return registry;
41 }
42
43
44 PipelineTemplate::Pass::~Pass()
45 { }
46
47
48 PipelineTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
49         postprocessor_template(ppt)
50 { }
51
52
53 PipelineTemplate::PostProcLoader::PostProcLoader()
54 {
55         get_postprocessor_registry().add_all(*this);
56 }
57
58
59 PipelineTemplate::Loader::Loader(PipelineTemplate &t):
60         DataFile::CollectionObjectLoader<PipelineTemplate>(t, 0)
61 {
62         init();
63 }
64
65 PipelineTemplate::Loader::Loader(PipelineTemplate &t, Collection &c):
66         DataFile::CollectionObjectLoader<PipelineTemplate>(t, &c)
67 {
68         init();
69 }
70
71 void PipelineTemplate::Loader::init()
72 {
73         add("hdr", &PipelineTemplate::hdr);
74         add("multisample", &Loader::multisample);
75         add("multisample", &Loader::multisample_range);
76         add("pass", &Loader::pass);
77         add("postprocessor", &Loader::postprocessor);
78 }
79
80 void PipelineTemplate::Loader::postprocessor_loaded()
81 {
82         obj.postprocessors.push_back(get_postprocessor_template());
83 }
84
85 void PipelineTemplate::Loader::multisample(unsigned samples)
86 {
87         obj.required_multisample = samples;
88         obj.max_multisample = samples;
89 }
90
91 void PipelineTemplate::Loader::multisample_range(unsigned req, unsigned max)
92 {
93         obj.required_multisample = req;
94         obj.max_multisample = max;
95 }
96
97 void PipelineTemplate::Loader::pass(const string &tag, const string &rend)
98 {
99         Pass pss;;
100         pss.tag = tag;
101         pss.renderable_name = rend;
102         if(coll)
103                 load_sub(pss, *coll);
104         else
105                 load_sub(pss);
106
107         obj.passes.push_back(pss);
108 }
109
110 void PipelineTemplate::Loader::postprocessor(const std::string &slot)
111 {
112         PostProcLoader ldr;
113         load_sub_with(ldr);
114         PostProcessor pp;
115         pp.postprocessor_template = ldr.get_postprocessor_template();
116         pp.slot_name = slot;
117         obj.postprocessors.push_back(pp);
118 }
119
120
121 PipelineTemplate::Pass::Loader::Loader(Pass &p):
122         DataFile::CollectionObjectLoader<Pass>(p, 0)
123 {
124         init();
125 }
126
127 PipelineTemplate::Pass::Loader::Loader(Pass &p, Collection &c):
128         DataFile::CollectionObjectLoader<Pass>(p, &c)
129 {
130         init();
131 }
132
133 void PipelineTemplate::Pass::Loader::init()
134 {
135         add("blend", &Loader::blend);
136         add("blend", &Loader::blend_predefined);
137         add("depth_test", &Loader::depth_test);
138         add("depth_test", &Loader::depth_test_predefined);
139         add("lighting", &Loader::lighting);
140         add("lighting", &Loader::lighting_inline);
141 }
142
143 void PipelineTemplate::Pass::Loader::blend_predefined(const string &name)
144 {
145         const Blend *bln = 0;
146         if(name=="alpha")
147                 bln = &Blend::alpha();
148         else if(name=="additive")
149                 bln = &Blend::additive();
150         else if(name=="additive_alpha")
151                 bln = &Blend::additive_alpha();
152         else
153                 throw key_error(name);
154
155         obj.blend = bln;
156         obj.blend.keep();
157 }
158
159 void PipelineTemplate::Pass::Loader::blend(BlendFactor src, BlendFactor dest)
160 {
161         obj.blend = new Blend(src, dest);
162 }
163
164 void PipelineTemplate::Pass::Loader::depth_test_predefined(const string &name)
165 {
166         const DepthTest *dtest = 0;
167         if(name=="lequal")
168                 dtest = &DepthTest::lequal();
169         else
170                 throw key_error(name);
171
172         obj.depth_test = dtest;
173         obj.depth_test.keep();
174 }
175
176 void PipelineTemplate::Pass::Loader::depth_test(Predicate pred)
177 {
178         obj.depth_test = new DepthTest(pred);
179 }
180
181 void PipelineTemplate::Pass::Loader::lighting_inline()
182 {
183         RefPtr<Lighting> lightn = new Lighting;
184         load_sub(*lightn);
185         obj.lighting = lightn;
186 }
187
188 void PipelineTemplate::Pass::Loader::lighting(const string &name)
189 {
190         obj.lighting = &get_collection().get<Lighting>(name);
191         obj.lighting.keep();
192 }
193
194 /*void PipelineTemplate::Pass::Loader::scene(const string &name)
195 {
196         obj.default_renderable = get_collection().get<Scene>(name);
197 }*/
198
199 } // namespace GL
200 } // namespace Msp