+++ /dev/null
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/core/refptr.h>
-#include <msp/datafile/collection.h>
-#include <msp/strings/formatter.h>
-#include "material.h"
-#include "renderpass.h"
-#include "program.h"
-#include "programdata.h"
-#include "texture.h"
-#include "texture2d.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-RenderPass::RenderPass():
- shprog(0),
- shdata(0),
- material(0)
-{ }
-
-RenderPass::RenderPass(const RenderPass &other):
- Bindable<RenderPass>(other),
- shprog(other.shprog),
- shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
- material(other.material),
- textures(other.textures)
-{ }
-
-RenderPass::~RenderPass()
-{
- delete shdata;
-}
-
-void RenderPass::set_material(const Material *mat)
-{
- material = mat;
- material.keep();
-}
-
-void RenderPass::set_texture(unsigned index, const Texture *tex)
-{
- for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
- if(i->index==index)
- {
- i->texture = tex;
- i->texture.keep();
- return;
- }
-
- textures.push_back(TextureSlot(index));
- textures.back().texture = tex;
- textures.back().texture.keep();
-}
-
-void RenderPass::bind() const
-{
- const RenderPass *old = current();
- if(!set_current(this))
- return;
-
- if(shprog)
- {
- shprog->bind();
- shdata->apply();
- }
- else if(old && old->shprog)
- GL::Program::unbind();
-
- if(material)
- material->bind();
- else if(old && old->material)
- GL::Material::unbind();
-
- unsigned used_tex_units = 0;
- for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
- {
- i->texture->bind_to(i->index);
- used_tex_units |= 1<<i->index;
- }
- if(old)
- {
- for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
- if(!used_tex_units&(1<<i->index))
- Texture::unbind_from(i->index);
- }
-}
-
-void RenderPass::unbind()
-{
- const RenderPass *old = current();
- if(!set_current(0))
- return;
-
- if(old->shprog)
- GL::Program::unbind();
-
- if(old->material)
- GL::Material::unbind();
-
- for(unsigned i=old->textures.size(); i--; )
- GL::Texture::unbind_from(i);
-}
-
-
-RenderPass::TextureSlot::TextureSlot(unsigned i):
- index(i),
- texture(0)
-{ }
-
-
-RenderPass::Loader::Loader(RenderPass &p):
- DataFile::CollectionObjectLoader<RenderPass>(p, 0)
-{
- init();
-}
-
-RenderPass::Loader::Loader(RenderPass &p, Collection &c):
- DataFile::CollectionObjectLoader<RenderPass>(p, &c)
-{
- init();
-}
-
-void RenderPass::Loader::init()
-{
- allow_pointer_reload = false;
-
- add("shader", &RenderPass::shprog);
- add("material", static_cast<void (Loader::*)()>(&Loader::material));
- add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
- add("texunit", &Loader::texunit);
- add("uniforms", &Loader::uniforms);
-}
-
-void RenderPass::Loader::finish()
-{
- // XXX Make shdata optional
- if(obj.shprog && !obj.shdata)
- obj.shdata = new ProgramData;
-}
-
-void RenderPass::Loader::material()
-{
- RefPtr<Material> mat = new Material;
- load_sub(*mat);
- obj.material = mat;
-}
-
-void RenderPass::Loader::material(const string &name)
-{
- obj.material = get_collection().get<Material>(name);
- obj.material.keep();
-}
-
-void RenderPass::Loader::texunit(unsigned i)
-{
- TextureSlot slot(i);
- load_sub(slot);
- obj.textures.push_back(slot);
-}
-
-void RenderPass::Loader::uniforms()
-{
- if(!obj.shprog)
- throw InvalidState("Can't load uniforms without a shader program");
- if(!obj.shdata)
- obj.shdata = new ProgramData;
- load_sub(*obj.shdata, *obj.shprog);
-}
-
-
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
- DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
-{
- init();
-}
-
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
- DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
-{
- init();
-}
-
-void RenderPass::TextureSlot::Loader::init()
-{
- add("texture", &Loader::texture);
- add("texture2d", &Loader::texture2d);
-}
-
-void RenderPass::TextureSlot::Loader::texture(const string &name)
-{
- obj.texture = get_collection().get<Texture>(name);
- obj.texture.keep();
-}
-
-void RenderPass::TextureSlot::Loader::texture2d()
-{
- RefPtr<Texture2D> tex = new Texture2D;
- load_sub(*tex);
- obj.texture = tex;
-}
-
-} // namespace GL
-} // namespace Msp