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"
16 #include "texture2d.h"
23 RenderPass::RenderPass():
29 RenderPass::RenderPass(const RenderPass &other):
30 Bindable<RenderPass>(other),
32 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
33 material(other.material),
34 textures(other.textures)
37 RenderPass::~RenderPass()
42 void RenderPass::set_material(const Material *mat)
47 void RenderPass::set_texture(unsigned index, const Texture *tex)
49 for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
57 throw KeyError("No texture slot for that unit", lexical_cast(index));
60 void RenderPass::bind() const
62 const RenderPass *old = current();
63 if(!set_current(this))
71 else if(old && old->shprog)
72 GL::Program::unbind();
76 else if(old && old->material)
77 GL::Material::unbind();
79 unsigned used_tex_units = 0;
80 for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
82 i->texture->bind_to(i->index);
83 used_tex_units |= 1<<i->index;
87 for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
88 if(!used_tex_units&(1<<i->index))
89 Texture::unbind_from(i->index);
93 void RenderPass::unbind()
95 const RenderPass *old = current();
100 GL::Program::unbind();
103 GL::Material::unbind();
105 for(unsigned i=old->textures.size(); i--; )
106 GL::Texture::unbind_from(i);
110 RenderPass::TextureSlot::TextureSlot(unsigned i):
116 RenderPass::Loader::Loader(RenderPass &p):
117 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
122 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
123 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
128 void RenderPass::Loader::init()
130 allow_pointer_reload = false;
132 add("shader", &RenderPass::shprog);
133 add("material", static_cast<void (Loader::*)()>(&Loader::material));
134 add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
135 add("texunit", &Loader::texunit);
136 add("uniforms", &Loader::uniforms);
139 void RenderPass::Loader::finish()
141 // XXX Make shdata optional
142 if(obj.shprog && !obj.shdata)
143 obj.shdata = new ProgramData;
146 void RenderPass::Loader::material()
148 RefPtr<Material> mat = new Material;
153 void RenderPass::Loader::material(const string &name)
155 obj.material = get_collection().get<Material>(name);
159 void RenderPass::Loader::texunit(unsigned i)
163 obj.textures.push_back(slot);
166 void RenderPass::Loader::uniforms()
169 throw InvalidState("Can't load uniforms without a shader program");
171 obj.shdata = new ProgramData;
172 load_sub(*obj.shdata, *obj.shprog);
176 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
177 DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
182 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
183 DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
188 void RenderPass::TextureSlot::Loader::init()
190 add("texture", &Loader::texture);
191 add("texture2d", &Loader::texture2d);
194 void RenderPass::TextureSlot::Loader::texture(const string &name)
196 obj.texture = get_collection().get<Texture>(name);
200 void RenderPass::TextureSlot::Loader::texture2d()
202 RefPtr<Texture2D> tex = new Texture2D;