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):
28 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
29 material(other.material),
30 texturing(other.texturing ? new Texturing(*other.texturing) : 0),
31 tex_names(other.tex_names),
32 back_faces(other.back_faces)
35 RenderPass::~RenderPass()
40 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
47 void RenderPass::set_material(const Material *mat)
53 void RenderPass::set_texture(unsigned index, const Texture *tex)
56 texturing = new Texturing;
58 texturing->attach(index, *tex);
61 int RenderPass::get_texture_index(const string &n) const
63 map<string, unsigned>::const_iterator i = tex_names.find(n);
64 if(i==tex_names.end())
69 void RenderPass::apply(Renderer &renderer) const
71 renderer.set_texturing(texturing);
72 renderer.set_material(material.get());
73 renderer.set_shader_program(shprog, shdata.get());
74 renderer.set_reverse_winding(back_faces);
78 RenderPass::Loader::Loader(RenderPass &p):
79 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
84 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
85 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
90 void RenderPass::Loader::init()
92 add("shader", &RenderPass::shprog);
93 add("material", &Loader::material_inline);
94 add("material", &Loader::material);
95 add("back_faces",&RenderPass::back_faces);
96 add("texunit", &Loader::texunit);
97 add("texunit", &Loader::texunit_named);
98 add("uniforms", &Loader::uniforms);
101 void RenderPass::Loader::material_inline()
103 RefPtr<Material> mat = new Material;
105 load_sub(*mat, get_collection());
111 void RenderPass::Loader::material(const string &name)
113 obj.material = &get_collection().get<Material>(name);
117 void RenderPass::Loader::texunit(unsigned i)
120 obj.texturing = new Texturing;
121 TextureLoader ldr(*obj.texturing, i, coll);
125 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
128 obj.tex_names[n] = i;
131 void RenderPass::Loader::uniforms()
134 throw invalid_operation("RenderPass::Loader::uniforms");
135 RefPtr<ProgramData> shd = new ProgramData;
141 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
142 DataFile::CollectionObjectLoader<Texturing>(t, c),
145 add("texture", &TextureLoader::texture);
146 add("texture2d", &TextureLoader::texture2d);
149 void RenderPass::TextureLoader::finish()
153 obj.attach(index, *tex);
158 void RenderPass::TextureLoader::texture(const string &name)
160 tex = &get_collection().get<Texture>(name);
164 void RenderPass::TextureLoader::texture2d()
168 load_sub(static_cast<Texture2D &>(*tex), get_collection());
170 load_sub(static_cast<Texture2D &>(*tex));