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):
31 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
32 material(other.material),
33 textures(other.textures)
36 RenderPass::~RenderPass()
41 void RenderPass::set_material(const Material *mat)
46 void RenderPass::set_texture(unsigned index, const Texture *tex)
48 for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
56 throw KeyError("No texture slot for that unit", lexical_cast(index));
59 void RenderPass::bind() const
61 const RenderPass *old = current();
62 if(!set_current(this))
70 else if(old && old->shprog)
71 GL::Program::unbind();
75 else if(old && old->material)
76 GL::Material::unbind();
78 unsigned used_tex_units = 0;
79 for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
81 i->texture->bind_to(i->index);
82 used_tex_units |= 1<<i->index;
86 for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
87 if(!used_tex_units&(1<<i->index))
88 Texture::unbind_from(i->index);
92 void RenderPass::unbind()
94 const RenderPass *old = current();
99 GL::Program::unbind();
102 GL::Material::unbind();
104 for(unsigned i=old->textures.size(); i--; )
105 GL::Texture::unbind_from(i);
109 RenderPass::TextureSlot::TextureSlot(unsigned i):
115 RenderPass::Loader::Loader(RenderPass &p):
116 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
121 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
122 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
127 void RenderPass::Loader::init()
129 allow_pointer_reload = false;
131 add("shader", &RenderPass::shprog);
132 add("material", static_cast<void (Loader::*)()>(&Loader::material));
133 add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
134 add("texunit", &Loader::texunit);
135 add("uniforms", &Loader::uniforms);
138 void RenderPass::Loader::finish()
140 // XXX Make shdata optional
141 if(obj.shprog && !obj.shdata)
142 obj.shdata = new ProgramData;
145 void RenderPass::Loader::material()
147 RefPtr<Material> mat = new Material;
152 void RenderPass::Loader::material(const string &name)
154 obj.material = get_collection().get<Material>(name);
158 void RenderPass::Loader::texunit(unsigned i)
162 obj.textures.push_back(slot);
165 void RenderPass::Loader::uniforms()
168 throw InvalidState("Can't load uniforms without a shader program");
170 obj.shdata = new ProgramData;
171 load_sub(*obj.shdata, *obj.shprog);
175 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
176 DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
181 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
182 DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
187 void RenderPass::TextureSlot::Loader::init()
189 add("texture", &Loader::texture);
190 add("texture2d", &Loader::texture2d);
193 void RenderPass::TextureSlot::Loader::texture(const string &name)
195 obj.texture = get_collection().get<Texture>(name);
199 void RenderPass::TextureSlot::Loader::texture2d()
201 RefPtr<Texture2D> tex = new Texture2D;