1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
5 #include "renderpass.h"
7 #include "programdata.h"
10 #include "texture2d.h"
11 #include "texturing.h"
18 RenderPass::RenderPass():
26 RenderPass::RenderPass(const RenderPass &other):
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)
37 RenderPass &RenderPass::operator=(const RenderPass &other)
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;
50 RenderPass::~RenderPass()
55 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
58 shdata = (data ? new ProgramData(*data) : 0);
61 const string &RenderPass::get_slotted_uniform_name(const string &slot) const
63 map<string, string>::const_iterator i = uniform_slots.find(slot);
64 if(i==uniform_slots.end())
72 void RenderPass::set_material(const Material *mat)
78 void RenderPass::set_texture(unsigned index, const Texture *tex)
81 texturing = new Texturing;
83 texturing->attach(index, *tex);
86 int RenderPass::get_texture_index(const string &n) const
88 map<string, unsigned>::const_iterator i = tex_names.find(n);
89 if(i==tex_names.end())
94 void RenderPass::apply(Renderer &renderer) const
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);
103 RenderPass::Loader::Loader(RenderPass &p):
104 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
109 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
110 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
115 void RenderPass::Loader::init()
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);
128 void RenderPass::Loader::material_inline()
130 RefPtr<Material> mat = new Material;
132 load_sub(*mat, get_collection());
138 void RenderPass::Loader::material(const string &name)
140 obj.material = &get_collection().get<Material>(name);
144 void RenderPass::Loader::texunit(unsigned i)
147 obj.texturing = new Texturing;
148 TextureLoader ldr(*obj.texturing, i, coll);
152 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
155 obj.tex_names[n] = i;
158 void RenderPass::Loader::uniforms()
161 throw invalid_operation("RenderPass::Loader::uniforms");
162 RefPtr<ProgramData> shd = new ProgramData(obj.shprog);
164 obj.shdata = shd.release();
167 void RenderPass::Loader::uniform_slot(const string &name)
169 uniform_slot2(name, name);
172 void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
174 obj.uniform_slots[slot] = name;
178 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
179 DataFile::CollectionObjectLoader<Texturing>(t, c),
182 add("texture", &TextureLoader::texture);
183 add("texture2d", &TextureLoader::texture2d);
186 void RenderPass::TextureLoader::finish()
190 obj.attach(index, *tex);
195 void RenderPass::TextureLoader::texture(const string &name)
197 tex = &get_collection().get<Texture>(name);
201 void RenderPass::TextureLoader::texture2d()
205 load_sub(static_cast<Texture2D &>(*tex), get_collection());
207 load_sub(static_cast<Texture2D &>(*tex));