3 This file is part of libmspgl
4 Copyright © 2007-2008, 2010-2011 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"
17 #include "texture2d.h"
18 #include "texturing.h"
25 RenderPass::RenderPass():
32 RenderPass::RenderPass(const RenderPass &other):
34 shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
35 material(other.material),
36 texturing(other.texturing ? new Texturing(*other.texturing) : 0)
39 RenderPass::~RenderPass()
45 void RenderPass::set_material(const Material *mat)
51 void RenderPass::set_texture(unsigned index, const Texture *tex)
54 texturing = new Texturing;
56 texturing->attach(index, *tex);
60 RenderPass::Loader::Loader(RenderPass &p):
61 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
66 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
67 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
72 void RenderPass::Loader::init()
74 allow_pointer_reload = false;
76 add("shader", &RenderPass::shprog);
77 add("material", static_cast<void (Loader::*)()>(&Loader::material));
78 add("material", static_cast<void (Loader::*)(const string &)>(&Loader::material));
79 add("texunit", &Loader::texunit);
80 add("uniforms", &Loader::uniforms);
83 void RenderPass::Loader::finish()
85 // XXX Make shdata optional
86 if(obj.shprog && !obj.shdata)
87 obj.shdata = new ProgramData;
90 void RenderPass::Loader::material()
92 RefPtr<Material> mat = new Material;
97 void RenderPass::Loader::material(const string &name)
99 obj.material = get_collection().get<Material>(name);
103 void RenderPass::Loader::texunit(unsigned i)
106 obj.texturing = new Texturing;
107 TextureLoader ldr(*obj.texturing, i, coll);
111 void RenderPass::Loader::uniforms()
114 throw InvalidState("Can't load uniforms without a shader program");
116 obj.shdata = new ProgramData;
117 load_sub(*obj.shdata, *obj.shprog);
121 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
122 DataFile::CollectionObjectLoader<Texturing>(t, c),
125 add("texture", &TextureLoader::texture);
126 add("texture2d", &TextureLoader::texture2d);
129 void RenderPass::TextureLoader::finish()
134 obj.attach(index, *tex, *env);
136 obj.attach(index, *tex);
142 void RenderPass::TextureLoader::texenv()
144 throw Exception("TexEnvs can't be loaded yet");
149 void RenderPass::TextureLoader::texture(const string &name)
151 tex = get_collection().get<Texture>(name);
155 void RenderPass::TextureLoader::texture2d()
158 load_sub(static_cast<Texture2D &>(*tex));