]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Hide windows.h from public headers
[libs/gl.git] / source / renderpass.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
3 #include "error.h"
4 #include "material.h"
5 #include "renderpass.h"
6 #include "program.h"
7 #include "programdata.h"
8 #include "renderer.h"
9 #include "texture.h"
10 #include "texture2d.h"
11 #include "texturing.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 RenderPass::RenderPass():
19         shprog(0),
20         shdata(0),
21         material(0),
22         texturing(0),
23         back_faces(false)
24 { }
25
26 RenderPass::RenderPass(const RenderPass &other):
27         shprog(other.shprog),
28         shdata(other.shdata),
29         uniform_slots(other.uniform_slots),
30         material(other.material),
31         material_slot(other.material_slot),
32         texturing(other.texturing ? new Texturing(*other.texturing) : 0),
33         tex_names(other.tex_names),
34         back_faces(other.back_faces)
35 { }
36
37 RenderPass &RenderPass::operator=(const RenderPass &other)
38 {
39         shprog = other.shprog;
40         shdata = other.shdata;
41         uniform_slots = other.uniform_slots;
42         material = other.material;
43         material_slot = other.material_slot;
44         texturing = other.texturing ? new Texturing(*other.texturing) : 0;
45         tex_names = other.tex_names;
46         back_faces = other.back_faces;
47         return *this;
48 }
49
50 RenderPass::~RenderPass()
51 {
52         delete texturing;
53 }
54
55 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
56 {
57         shprog = prog;
58         shdata = (data ? new ProgramData(*data) : 0);
59 }
60
61 const string &RenderPass::get_slotted_uniform_name(const string &slot) const
62 {
63         map<string, string>::const_iterator i = uniform_slots.find(slot);
64         if(i==uniform_slots.end())
65         {
66                 static string empty;
67                 return empty;
68         }
69         return i->second;
70 }
71
72 void RenderPass::set_material(const Material *mat)
73 {
74         material = mat;
75         material.keep();
76 }
77
78 void RenderPass::set_texture(unsigned index, const Texture *tex)
79 {
80         if(!texturing)
81                 texturing = new Texturing;
82
83         texturing->attach(index, *tex);
84 }
85
86 int RenderPass::get_texture_index(const string &n) const
87 {
88         map<string, unsigned>::const_iterator i = tex_names.find(n);
89         if(i==tex_names.end())
90                 return -1;
91         return i->second;
92 }
93
94 void RenderPass::apply(Renderer &renderer) const
95 {
96         renderer.set_texturing(texturing);
97         renderer.set_material(material.get());
98         renderer.set_shader_program(shprog, shdata.get());
99         renderer.set_reverse_winding(back_faces);
100 }
101
102
103 RenderPass::Loader::Loader(RenderPass &p):
104         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
105 {
106         init();
107 }
108
109 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
110         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
111 {
112         init();
113 }
114
115 void RenderPass::Loader::init()
116 {
117         add("shader",   &RenderPass::shprog);
118         add("material", &Loader::material_inline);
119         add("material", &Loader::material);
120         add("material_slot", &RenderPass::material_slot);
121         add("back_faces",&RenderPass::back_faces);
122         add("texunit",  &Loader::texunit);
123         add("texunit",  &Loader::texunit_named);
124         add("uniforms", &Loader::uniforms);
125         add("uniform_slot", &Loader::uniform_slot);
126 }
127
128 void RenderPass::Loader::material_inline()
129 {
130         RefPtr<Material> mat = new Material;
131         if(coll)
132                 load_sub(*mat, get_collection());
133         else
134                 load_sub(*mat);
135         obj.material = mat;
136 }
137
138 void RenderPass::Loader::material(const string &name)
139 {
140         obj.material = &get_collection().get<Material>(name);
141         obj.material.keep();
142 }
143
144 void RenderPass::Loader::texunit(unsigned i)
145 {
146         if(!obj.texturing)
147                 obj.texturing = new Texturing;
148         TextureLoader ldr(*obj.texturing, i, coll);
149         load_sub_with(ldr);
150 }
151
152 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
153 {
154         texunit(i);
155         obj.tex_names[n] = i;
156 }
157
158 void RenderPass::Loader::uniforms()
159 {
160         if(!obj.shprog)
161                 throw invalid_operation("RenderPass::Loader::uniforms");
162         RefPtr<ProgramData> shd = new ProgramData(obj.shprog);
163         load_sub(*shd);
164         obj.shdata = shd.release();
165 }
166
167 void RenderPass::Loader::uniform_slot(const string &name)
168 {
169         uniform_slot2(name, name);
170 }
171
172 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
173 {
174         obj.uniform_slots[slot] = name;
175 }
176
177
178 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
179         DataFile::CollectionObjectLoader<Texturing>(t, c),
180         index(i)
181 {
182         add("texture",   &TextureLoader::texture);
183         add("texture2d", &TextureLoader::texture2d);
184 }
185
186 void RenderPass::TextureLoader::finish()
187 {
188         if(tex)
189         {
190                 obj.attach(index, *tex);
191                 tex.release();
192         }
193 }
194
195 void RenderPass::TextureLoader::texture(const string &name)
196 {
197         tex = &get_collection().get<Texture>(name);
198         tex.keep();
199 }
200
201 void RenderPass::TextureLoader::texture2d()
202 {
203         tex = new Texture2D;
204         if(coll)
205                 load_sub(static_cast<Texture2D &>(*tex), get_collection());
206         else
207                 load_sub(static_cast<Texture2D &>(*tex));
208 }
209
210 } // namespace GL
211 } // namespace Msp