1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
5 #include "renderpass.h"
7 #include "programdata.h"
11 #include "texture2d.h"
12 #include "texturing.h"
19 RenderPass::RenderPass():
27 RenderPass::RenderPass(const RenderPass &other):
29 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
30 material(other.material),
31 texturing(other.texturing ? new Texturing(*other.texturing) : 0),
32 tex_names(other.tex_names),
33 back_faces(other.back_faces)
36 RenderPass::~RenderPass()
41 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
48 void RenderPass::set_material(const Material *mat)
54 void RenderPass::set_texture(unsigned index, const Texture *tex)
57 texturing = new Texturing;
59 texturing->attach(index, *tex);
62 int RenderPass::get_texture_index(const string &n) const
64 map<string, unsigned>::const_iterator i = tex_names.find(n);
65 if(i==tex_names.end())
70 void RenderPass::apply(Renderer &renderer) const
72 renderer.set_texturing(texturing);
73 renderer.set_material(material.get());
74 renderer.set_shader_program(shprog, shdata.get());
75 renderer.set_reverse_winding(back_faces);
79 RenderPass::Loader::Loader(RenderPass &p):
80 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
85 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
86 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
91 void RenderPass::Loader::init()
93 add("shader", &RenderPass::shprog);
94 add("material", &Loader::material_inline);
95 add("material", &Loader::material);
96 add("back_faces",&RenderPass::back_faces);
97 add("texunit", &Loader::texunit);
98 add("texunit", &Loader::texunit_named);
99 add("uniforms", &Loader::uniforms);
102 void RenderPass::Loader::material_inline()
104 RefPtr<Material> mat = new Material;
109 void RenderPass::Loader::material(const string &name)
111 obj.material = &get_collection().get<Material>(name);
115 void RenderPass::Loader::texunit(unsigned i)
118 obj.texturing = new Texturing;
119 TextureLoader ldr(*obj.texturing, i, coll);
123 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
126 obj.tex_names[n] = i;
129 void RenderPass::Loader::uniforms()
132 throw invalid_operation("RenderPass::Loader::uniforms");
133 RefPtr<ProgramData> shd = new ProgramData;
139 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
140 DataFile::CollectionObjectLoader<Texturing>(t, c),
143 add("texture", &TextureLoader::texture);
144 add("texture2d", &TextureLoader::texture2d);
147 void RenderPass::TextureLoader::finish()
152 obj.attach(index, *tex, *env);
154 obj.attach(index, *tex);
160 void RenderPass::TextureLoader::texenv()
162 throw runtime_error("TexEnvs can't be loaded yet");
167 void RenderPass::TextureLoader::texture(const string &name)
169 tex = &get_collection().get<Texture>(name);
173 void RenderPass::TextureLoader::texture2d()
176 load_sub(static_cast<Texture2D &>(*tex));