]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Rename some Loader methods to avoid having to static_cast the pointers
[libs/gl.git] / source / renderpass.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2008, 2010-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/collection.h>
10 #include <msp/strings/formatter.h>
11 #include "material.h"
12 #include "renderpass.h"
13 #include "program.h"
14 #include "programdata.h"
15 #include "texenv.h"
16 #include "texture.h"
17 #include "texture2d.h"
18 #include "texturing.h"
19
20 using namespace std;
21
22 namespace Msp {
23 namespace GL {
24
25 RenderPass::RenderPass():
26         shprog(0),
27         shdata(0),
28         material(0),
29         texturing(0)
30 { }
31
32 RenderPass::RenderPass(const RenderPass &other):
33         shprog(other.shprog),
34         shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
35         material(other.material),
36         texturing(other.texturing ? new Texturing(*other.texturing) : 0)
37 { }
38
39 RenderPass::~RenderPass()
40 {
41         delete shdata;
42         delete texturing;
43 }
44
45 void RenderPass::set_material(const Material *mat)
46 {
47         material = mat;
48         material.keep();
49 }
50
51 void RenderPass::set_texture(unsigned index, const Texture *tex)
52 {
53         if(!texturing)
54                 texturing = new Texturing;
55
56         texturing->attach(index, *tex);
57 }
58
59
60 RenderPass::Loader::Loader(RenderPass &p):
61         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
62 {
63         init();
64 }
65
66 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
67         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
68 {
69         init();
70 }
71
72 void RenderPass::Loader::init()
73 {
74         allow_pointer_reload = false;
75
76         add("shader",   &RenderPass::shprog);
77         add("material", &Loader::material_inline);
78         add("material", &Loader::material);
79         add("texunit",  &Loader::texunit);
80         add("uniforms", &Loader::uniforms);
81 }
82
83 void RenderPass::Loader::finish()
84 {
85         // XXX Make shdata optional
86         if(obj.shprog && !obj.shdata)
87                 obj.shdata = new ProgramData;
88 }
89
90 void RenderPass::Loader::material_inline()
91 {
92         RefPtr<Material> mat = new Material;
93         load_sub(*mat);
94         obj.material = mat;
95 }
96
97 void RenderPass::Loader::material(const string &name)
98 {
99         obj.material = get_collection().get<Material>(name);
100         obj.material.keep();
101 }
102
103 void RenderPass::Loader::texunit(unsigned i)
104 {
105         if(!obj.texturing)
106                 obj.texturing = new Texturing;
107         TextureLoader ldr(*obj.texturing, i, coll);
108         load_sub_with(ldr);
109 }
110
111 void RenderPass::Loader::uniforms()
112 {
113         if(!obj.shprog)
114                 throw InvalidState("Can't load uniforms without a shader program");
115         if(!obj.shdata)
116                 obj.shdata = new ProgramData;
117         load_sub(*obj.shdata, *obj.shprog);
118 }
119
120
121 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
122         DataFile::CollectionObjectLoader<Texturing>(t, c),
123         index(i)
124 {
125         add("texture",   &TextureLoader::texture);
126         add("texture2d", &TextureLoader::texture2d);
127 }
128
129 void RenderPass::TextureLoader::finish()
130 {
131         if(tex)
132         {
133                 if(env)
134                         obj.attach(index, *tex, *env);
135                 else
136                         obj.attach(index, *tex);
137                 tex.release();
138                 env.release();
139         }
140 }
141
142 void RenderPass::TextureLoader::texenv()
143 {
144         throw Exception("TexEnvs can't be loaded yet");
145         /*env = new TexEnv;
146         load_sub(*env);*/
147 }
148
149 void RenderPass::TextureLoader::texture(const string &name)
150 {
151         tex = get_collection().get<Texture>(name);
152         tex.keep();
153 }
154
155 void RenderPass::TextureLoader::texture2d()
156 {
157         tex = new Texture2D;
158         load_sub(static_cast<Texture2D &>(*tex));
159 }
160
161 } // namespace GL
162 } // namespace Msp