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 RenderPass::RenderPass():
29 RenderPass::RenderPass(const RenderPass &other):
31 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
32 own_material(other.own_material),
33 material(own_material ? new Material(*other.material) : other.material),
34 textures(other.textures)
37 RenderPass::~RenderPass()
44 void RenderPass::set_material(const Material *mat)
49 unsigned RenderPass::get_texture_index(const string &slot) const
51 for(unsigned i=0; i<textures.size(); ++i)
52 if(textures[i].name==slot)
55 throw KeyError("Unknown texture slot", slot);
58 void RenderPass::set_texture(const string &slot, const Texture *tex)
60 textures[get_texture_index(slot)] = tex;
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 for(unsigned i=0; i<textures.size(); ++i)
83 if(textures[i].texture)
84 textures[i].texture->bind_to(i);
87 for(unsigned i=textures.size(); i<old->textures.size(); ++i)
88 GL::Texture::unbind_from(i);
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::Loader::Loader(RenderPass &p):
110 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
115 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
116 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
121 void RenderPass::Loader::init()
123 allow_pointer_reload = false;
125 add("shader", &RenderPass::shprog);
126 add("material", &Loader::material);
127 add("material", &RenderPass::material);
128 add("texture", &Loader::texture);
129 add("uniforms", &Loader::uniforms);
132 void RenderPass::Loader::finish()
137 obj.shdata = new ProgramData;
139 for(unsigned i=0; i<obj.textures.size(); ++i)
141 unsigned loc = obj.shprog->get_uniform_location(obj.textures[i].name);
142 obj.shdata->uniform(loc, static_cast<int>(i));
147 void RenderPass::Loader::material()
150 throw InvalidState("A material is already loaded");
152 RefPtr<Material> mat = new Material;
154 obj.material = mat.release();
155 obj.own_material = true;
158 void RenderPass::Loader::texture(const string &n)
160 const Texture *tex = (n.empty() ? 0 : get_collection().get<Texture>(n));
161 TextureSlot slot(tex);
162 slot.name = (obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
164 obj.textures.push_back(slot);
167 void RenderPass::Loader::uniforms()
170 throw InvalidState("Can't load uniforms without a shader program");
172 obj.shdata = new ProgramData;
173 load_sub(*obj.shdata, *obj.shprog);
177 RenderPass::TextureSlot::TextureSlot(const Texture *t):
182 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
183 DataFile::ObjectLoader<TextureSlot>(s)
185 add("name", &TextureSlot::name);