]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Bind ProgramData to a Program upon construction
[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::material_inline()
84 {
85         RefPtr<Material> mat = new Material;
86         load_sub(*mat);
87         obj.material = mat;
88 }
89
90 void RenderPass::Loader::material(const string &name)
91 {
92         obj.material = get_collection().get<Material>(name);
93         obj.material.keep();
94 }
95
96 void RenderPass::Loader::texunit(unsigned i)
97 {
98         if(!obj.texturing)
99                 obj.texturing = new Texturing;
100         TextureLoader ldr(*obj.texturing, i, coll);
101         load_sub_with(ldr);
102 }
103
104 void RenderPass::Loader::uniforms()
105 {
106         if(!obj.shprog)
107                 throw InvalidState("Can't load uniforms without a shader program");
108         if(!obj.shdata)
109                 obj.shdata = new ProgramData(*obj.shprog);
110         load_sub(*obj.shdata);
111 }
112
113
114 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
115         DataFile::CollectionObjectLoader<Texturing>(t, c),
116         index(i)
117 {
118         add("texture",   &TextureLoader::texture);
119         add("texture2d", &TextureLoader::texture2d);
120 }
121
122 void RenderPass::TextureLoader::finish()
123 {
124         if(tex)
125         {
126                 if(env)
127                         obj.attach(index, *tex, *env);
128                 else
129                         obj.attach(index, *tex);
130                 tex.release();
131                 env.release();
132         }
133 }
134
135 void RenderPass::TextureLoader::texenv()
136 {
137         throw Exception("TexEnvs can't be loaded yet");
138         /*env = new TexEnv;
139         load_sub(*env);*/
140 }
141
142 void RenderPass::TextureLoader::texture(const string &name)
143 {
144         tex = get_collection().get<Texture>(name);
145         tex.keep();
146 }
147
148 void RenderPass::TextureLoader::texture2d()
149 {
150         tex = new Texture2D;
151         load_sub(static_cast<Texture2D &>(*tex));
152 }
153
154 } // namespace GL
155 } // namespace Msp