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"
22 const RenderPass *RenderPass::current = 0;
24 RenderPass::RenderPass():
31 RenderPass::RenderPass(const RenderPass &other):
33 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
34 own_material(other.own_material),
35 material(own_material ? new Material(*other.material) : other.material),
36 textures(other.textures)
39 RenderPass::~RenderPass()
46 void RenderPass::set_material(const Material *mat)
51 unsigned RenderPass::get_texture_index(const string &slot) const
53 for(unsigned i=0; i<textures.size(); ++i)
54 if(textures[i].name==slot)
57 throw KeyError("Unknown texture slot", slot);
60 void RenderPass::set_texture(const string &slot, const Texture *tex)
62 textures[get_texture_index(slot)] = tex;
65 void RenderPass::bind() const
70 const RenderPass *old = current;
78 else if(old && !old->shprog)
79 GL::Program::unbind();
83 else if(old && !old->material)
84 GL::Material::unbind();
86 for(unsigned i=0; i<textures.size(); ++i)
87 if(textures[i].texture)
88 textures[i].texture->bind_to(i);
91 for(unsigned i=textures.size(); i<old->textures.size(); ++i)
92 GL::Texture::unbind_from(i);
96 void RenderPass::unbind()
101 GL::Program::unbind();
103 if(current->material)
104 GL::Material::unbind();
106 for(unsigned i=current->textures.size(); i--; )
107 GL::Texture::unbind_from(i);
114 RenderPass::Loader::Loader(RenderPass &p):
115 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
120 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
121 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
126 void RenderPass::Loader::init()
128 allow_pointer_reload = false;
130 add("shader", &RenderPass::shprog);
131 add("material", &Loader::material);
132 add("material", &RenderPass::material);
133 add("texture", &Loader::texture);
134 add("uniforms", &Loader::uniforms);
137 void RenderPass::Loader::finish()
142 obj.shdata = new ProgramData;
144 for(unsigned i=0; i<obj.textures.size(); ++i)
146 unsigned loc = obj.shprog->get_uniform_location(obj.textures[i].name);
147 obj.shdata->uniform(loc, static_cast<int>(i));
152 void RenderPass::Loader::material()
155 throw InvalidState("A material is already loaded");
157 RefPtr<Material> mat = new Material;
159 obj.material = mat.release();
160 obj.own_material = true;
163 void RenderPass::Loader::texture(const string &n)
165 const Texture *tex = (n.empty() ? 0 : get_collection().get<Texture>(n));
166 TextureSlot slot(tex);
167 slot.name = (obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
169 obj.textures.push_back(slot);
172 void RenderPass::Loader::uniforms()
175 throw InvalidState("Can't load uniforms without a shader program");
177 obj.shdata = new ProgramData;
178 load_sub(*obj.shdata, *obj.shprog);
182 RenderPass::TextureSlot::TextureSlot(const Texture *t):
187 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
188 DataFile::ObjectLoader<TextureSlot>(s)
190 add("name", &TextureSlot::name);