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::operator=(const RenderPass &other)
43 shdata=(other.shdata ? new ProgramData(*other.shdata) : 0);
44 material=other.material;
45 use_material=other.use_material;
46 textures=other.textures;
47 use_textures=other.use_textures;
52 RenderPass::~RenderPass()
59 void RenderPass::set_material(const Material *mat)
64 unsigned RenderPass::get_texture_index(const string &slot) const
66 for(unsigned i=0; i<textures.size(); ++i)
67 if(textures[i].name==slot)
70 throw KeyError("Unknown texture slot", slot);
73 void RenderPass::set_texture(const string &slot, const Texture *tex)
75 textures[get_texture_index(slot)]=tex;
78 void RenderPass::bind() const
83 const RenderPass *old=current;
91 else if(old && !old->shprog)
92 GL::Program::unbind();
96 else if(old && !old->material)
97 GL::Material::unbind();
99 for(unsigned i=0; i<textures.size(); ++i)
100 if(textures[i].texture)
101 textures[i].texture->bind_to(i);
104 for(unsigned i=textures.size(); i<old->textures.size(); ++i)
105 GL::Texture::unbind_from(i);
109 void RenderPass::unbind()
114 GL::Program::unbind();
116 if(current->material)
117 GL::Material::unbind();
119 for(unsigned i=current->textures.size(); i--; )
120 GL::Texture::unbind_from(i);
127 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
128 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
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()
154 // XXX Potential memory management trouble with multiple material statements
155 RefPtr<Material> mat=new Material;
157 obj.material=mat.release();
158 obj.own_material=true;
161 void RenderPass::Loader::texture(const string &n)
163 const Texture *tex=(n.empty() ? 0 : get_collection().get<Texture>(n));
164 TextureSlot slot(tex);
165 slot.name=(obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
167 obj.textures.push_back(slot);
170 void RenderPass::Loader::uniforms()
173 throw InvalidState("Can't load uniforms without a shader program");
175 obj.shdata=new ProgramData;
176 load_sub(*obj.shdata, *obj.shprog);
180 RenderPass::TextureSlot::TextureSlot(const Texture *t):
185 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
186 DataFile::ObjectLoader<TextureSlot>(s)
188 add("name", &TextureSlot::name);