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),
37 tex_names(other.tex_names)
40 RenderPass::~RenderPass()
46 void RenderPass::set_material(const Material *mat)
52 void RenderPass::set_texture(unsigned index, const Texture *tex)
55 texturing = new Texturing;
57 texturing->attach(index, *tex);
60 int RenderPass::get_texture_index(const string &n) const
62 map<string, unsigned>::const_iterator i = tex_names.find(n);
63 if(i==tex_names.end())
69 RenderPass::Loader::Loader(RenderPass &p):
70 DataFile::CollectionObjectLoader<RenderPass>(p, 0)
75 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
76 DataFile::CollectionObjectLoader<RenderPass>(p, &c)
81 void RenderPass::Loader::init()
83 allow_pointer_reload = false;
85 add("shader", &RenderPass::shprog);
86 add("material", &Loader::material_inline);
87 add("material", &Loader::material);
88 add("texunit", &Loader::texunit);
89 add("texunit", &Loader::texunit_named);
90 add("uniforms", &Loader::uniforms);
93 void RenderPass::Loader::material_inline()
95 RefPtr<Material> mat = new Material;
100 void RenderPass::Loader::material(const string &name)
102 obj.material = get_collection().get<Material>(name);
106 void RenderPass::Loader::texunit(unsigned i)
109 obj.texturing = new Texturing;
110 TextureLoader ldr(*obj.texturing, i, coll);
114 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
117 obj.tex_names[n] = i;
120 void RenderPass::Loader::uniforms()
123 throw InvalidState("Can't load uniforms without a shader program");
125 obj.shdata = new ProgramData(*obj.shprog);
126 load_sub(*obj.shdata);
130 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
131 DataFile::CollectionObjectLoader<Texturing>(t, c),
134 add("texture", &TextureLoader::texture);
135 add("texture2d", &TextureLoader::texture2d);
138 void RenderPass::TextureLoader::finish()
143 obj.attach(index, *tex, *env);
145 obj.attach(index, *tex);
151 void RenderPass::TextureLoader::texenv()
153 throw Exception("TexEnvs can't be loaded yet");
158 void RenderPass::TextureLoader::texture(const string &name)
160 tex = get_collection().get<Texture>(name);
164 void RenderPass::TextureLoader::texture2d()
167 load_sub(static_cast<Texture2D &>(*tex));