3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/collection.h>
10 #include <msp/strings/formatter.h>
12 #include "renderpass.h"
14 #include "programdata.h"
17 #include "texture2d.h"
18 #include "texturing.h"
25 RenderPass::RenderPass():
32 RenderPass::RenderPass(const RenderPass &other):
33 Bindable<RenderPass>(other),
35 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
36 material(other.material),
37 texturing(other.texturing ? new Texturing(*other.texturing) : 0)
40 RenderPass::~RenderPass()
46 void RenderPass::set_material(const Material *mat)
52 void RenderPass::set_texture(unsigned index, const Texture *tex)
55 texturing = new Texturing;
57 texturing->attach(index, *tex);
60 void RenderPass::bind() const
62 const RenderPass *old = current();
63 if(!set_current(this))
71 else if(old && old->shprog)
76 else if(old && old->material)
81 else if(old && old->texturing)
85 void RenderPass::unbind()
87 const RenderPass *old = current();
102 RenderPass::Loader::Loader(RenderPass &p):
103 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
108 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
109 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
114 void RenderPass::Loader::init()
116 allow_pointer_reload = false;
118 add("shader", &RenderPass::shprog);
119 add("material", static_cast<void (Loader::*)()>(&Loader::material));
120 add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
121 add("texunit", &Loader::texunit);
122 add("uniforms", &Loader::uniforms);
125 void RenderPass::Loader::finish()
127 // XXX Make shdata optional
128 if(obj.shprog && !obj.shdata)
129 obj.shdata = new ProgramData;
132 void RenderPass::Loader::material()
134 RefPtr<Material> mat = new Material;
139 void RenderPass::Loader::material(const string &name)
141 obj.material = get_collection().get<Material>(name);
145 void RenderPass::Loader::texunit(unsigned i)
148 obj.texturing = new Texturing;
149 TextureLoader ldr(*obj.texturing, i, coll);
153 void RenderPass::Loader::uniforms()
156 throw InvalidState("Can't load uniforms without a shader program");
158 obj.shdata = new ProgramData;
159 load_sub(*obj.shdata, *obj.shprog);
163 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
164 DataFile::CollectionObjectLoader<Texturing>(t, c),
167 add("texture", &TextureLoader::texture);
168 add("texture2d", &TextureLoader::texture2d);
171 void RenderPass::TextureLoader::finish()
176 obj.attach(index, *tex, *env);
178 obj.attach(index, *tex);
184 void RenderPass::TextureLoader::texenv()
186 throw Exception("TexEnvs can't be loaded yet");
191 void RenderPass::TextureLoader::texture(const string &name)
193 tex = get_collection().get<Texture>(name);
197 void RenderPass::TextureLoader::texture2d()
200 load_sub(static_cast<Texture2D &>(*tex));