]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
b95bd4bcfe1977c9ce327c0e5189883efd97800a
[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         tex_names(other.tex_names)
38 { }
39
40 RenderPass::~RenderPass()
41 {
42         delete shdata;
43         delete texturing;
44 }
45
46 void RenderPass::set_material(const Material *mat)
47 {
48         material = mat;
49         material.keep();
50 }
51
52 void RenderPass::set_texture(unsigned index, const Texture *tex)
53 {
54         if(!texturing)
55                 texturing = new Texturing;
56
57         texturing->attach(index, *tex);
58 }
59
60 int RenderPass::get_texture_index(const string &n) const
61 {
62         map<string, unsigned>::const_iterator i = tex_names.find(n);
63         if(i==tex_names.end())
64                 return -1;
65         return i->second;
66 }
67
68
69 RenderPass::Loader::Loader(RenderPass &p):
70         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
71 {
72         init();
73 }
74
75 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
76         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
77 {
78         init();
79 }
80
81 void RenderPass::Loader::init()
82 {
83         allow_pointer_reload = false;
84
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);
91 }
92
93 void RenderPass::Loader::material_inline()
94 {
95         RefPtr<Material> mat = new Material;
96         load_sub(*mat);
97         obj.material = mat;
98 }
99
100 void RenderPass::Loader::material(const string &name)
101 {
102         obj.material = get_collection().get<Material>(name);
103         obj.material.keep();
104 }
105
106 void RenderPass::Loader::texunit(unsigned i)
107 {
108         if(!obj.texturing)
109                 obj.texturing = new Texturing;
110         TextureLoader ldr(*obj.texturing, i, coll);
111         load_sub_with(ldr);
112 }
113
114 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
115 {
116         texunit(i);
117         obj.tex_names[n] = i;
118 }
119
120 void RenderPass::Loader::uniforms()
121 {
122         if(!obj.shprog)
123                 throw InvalidState("Can't load uniforms without a shader program");
124         if(!obj.shdata)
125                 obj.shdata = new ProgramData(*obj.shprog);
126         load_sub(*obj.shdata);
127 }
128
129
130 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
131         DataFile::CollectionObjectLoader<Texturing>(t, c),
132         index(i)
133 {
134         add("texture",   &TextureLoader::texture);
135         add("texture2d", &TextureLoader::texture2d);
136 }
137
138 void RenderPass::TextureLoader::finish()
139 {
140         if(tex)
141         {
142                 if(env)
143                         obj.attach(index, *tex, *env);
144                 else
145                         obj.attach(index, *tex);
146                 tex.release();
147                 env.release();
148         }
149 }
150
151 void RenderPass::TextureLoader::texenv()
152 {
153         throw Exception("TexEnvs can't be loaded yet");
154         /*env = new TexEnv;
155         load_sub(*env);*/
156 }
157
158 void RenderPass::TextureLoader::texture(const string &name)
159 {
160         tex = get_collection().get<Texture>(name);
161         tex.keep();
162 }
163
164 void RenderPass::TextureLoader::texture2d()
165 {
166         tex = new Texture2D;
167         load_sub(static_cast<Texture2D &>(*tex));
168 }
169
170 } // namespace GL
171 } // namespace Msp