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)
48 void RenderPass::set_texture(unsigned index, const Texture *tex)
50 for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
58 textures.push_back(TextureSlot(index));
59 textures.back().texture = tex;
60 textures.back().texture.keep();
63 void RenderPass::bind() const
65 const RenderPass *old = current();
66 if(!set_current(this))
74 else if(old && old->shprog)
75 GL::Program::unbind();
79 else if(old && old->material)
80 GL::Material::unbind();
82 unsigned used_tex_units = 0;
83 for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
85 i->texture->bind_to(i->index);
86 used_tex_units |= 1<<i->index;
90 for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
91 if(!used_tex_units&(1<<i->index))
92 Texture::unbind_from(i->index);
96 void RenderPass::unbind()
98 const RenderPass *old = current();
103 GL::Program::unbind();
106 GL::Material::unbind();
108 for(unsigned i=old->textures.size(); i--; )
109 GL::Texture::unbind_from(i);
113 RenderPass::TextureSlot::TextureSlot(unsigned i):
119 RenderPass::Loader::Loader(RenderPass &p):
120 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
125 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
126 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
131 void RenderPass::Loader::init()
133 allow_pointer_reload = false;
135 add("shader", &RenderPass::shprog);
136 add("material", static_cast<void (Loader::*)()>(&Loader::material));
137 add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
138 add("texunit", &Loader::texunit);
139 add("uniforms", &Loader::uniforms);
142 void RenderPass::Loader::finish()
144 // XXX Make shdata optional
145 if(obj.shprog && !obj.shdata)
146 obj.shdata = new ProgramData;
149 void RenderPass::Loader::material()
151 RefPtr<Material> mat = new Material;
156 void RenderPass::Loader::material(const string &name)
158 obj.material = get_collection().get<Material>(name);
162 void RenderPass::Loader::texunit(unsigned i)
166 obj.textures.push_back(slot);
169 void RenderPass::Loader::uniforms()
172 throw InvalidState("Can't load uniforms without a shader program");
174 obj.shdata = new ProgramData;
175 load_sub(*obj.shdata, *obj.shprog);
179 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
180 DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
185 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
186 DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
191 void RenderPass::TextureSlot::Loader::init()
193 add("texture", &Loader::texture);
194 add("texture2d", &Loader::texture2d);
197 void RenderPass::TextureSlot::Loader::texture(const string &name)
199 obj.texture = get_collection().get<Texture>(name);
203 void RenderPass::TextureSlot::Loader::texture2d()
205 RefPtr<Texture2D> tex = new Texture2D;