]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Rework Bind and enable it to restore the old binding
[libs/gl.git] / source / renderpass.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  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 "texture.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 RenderPass::RenderPass():
23         shprog(0),
24         shdata(0),
25         own_material(false),
26         material(0)
27 { }
28
29 RenderPass::RenderPass(const RenderPass &other):
30         shprog(other.shprog),
31         shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
32         own_material(other.own_material),
33         material(own_material ? new Material(*other.material) : other.material),
34         textures(other.textures)
35 { }
36
37 RenderPass::~RenderPass()
38 {
39         delete shdata;
40         if(own_material)
41                 delete material;
42 }
43
44 void RenderPass::set_material(const Material *mat)
45 {
46         material = mat;
47 }
48
49 unsigned RenderPass::get_texture_index(const string &slot) const
50 {
51         for(unsigned i=0; i<textures.size(); ++i)
52                 if(textures[i].name==slot)
53                         return i;
54
55         throw KeyError("Unknown texture slot", slot);
56 }
57
58 void RenderPass::set_texture(const string &slot, const Texture *tex)
59 {
60         textures[get_texture_index(slot)] = tex;
61 }
62
63 void RenderPass::bind() const
64 {
65         const RenderPass *old = current();
66         if(!set_current(this))
67                 return;
68
69         if(shprog)
70         {
71                 shprog->bind();
72                 shdata->apply();
73         }
74         else if(old && old->shprog)
75                 GL::Program::unbind();
76
77         if(material)
78                 material->bind();
79         else if(old && old->material)
80                 GL::Material::unbind();
81
82         for(unsigned i=0; i<textures.size(); ++i)
83                 if(textures[i].texture)
84                         textures[i].texture->bind_to(i);
85         if(old)
86         {
87                 for(unsigned i=textures.size(); i<old->textures.size(); ++i)
88                         GL::Texture::unbind_from(i);
89         }
90 }
91
92 void RenderPass::unbind()
93 {
94         const RenderPass *old = current();
95         if(!set_current(0))
96                 return;
97
98         if(old->shprog)
99                 GL::Program::unbind();
100
101         if(old->material)
102                 GL::Material::unbind();
103
104         for(unsigned i=old->textures.size(); i--; )
105                 GL::Texture::unbind_from(i);
106 }
107
108
109 RenderPass::Loader::Loader(RenderPass &p):
110         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
111 {
112         init();
113 }
114
115 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
116         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
117 {
118         init();
119 }
120
121 void RenderPass::Loader::init()
122 {
123         allow_pointer_reload = false;
124
125         add("shader",   &RenderPass::shprog);
126         add("material", &Loader::material);
127         add("material", &RenderPass::material);
128         add("texture",  &Loader::texture);
129         add("uniforms", &Loader::uniforms);
130 }
131
132 void RenderPass::Loader::finish()
133 {
134         if(obj.shprog)
135         {
136                 if(!obj.shdata)
137                         obj.shdata = new ProgramData;
138
139                 for(unsigned i=0; i<obj.textures.size(); ++i)
140                 {
141                         unsigned loc = obj.shprog->get_uniform_location(obj.textures[i].name);
142                         obj.shdata->uniform(loc, static_cast<int>(i));
143                 }
144         }
145 }
146
147 void RenderPass::Loader::material()
148 {
149         if(obj.material)
150                 throw InvalidState("A material is already loaded");
151
152         RefPtr<Material> mat = new Material;
153         load_sub(*mat);
154         obj.material = mat.release();
155         obj.own_material = true;
156 }
157
158 void RenderPass::Loader::texture(const string &n)
159 {
160         const Texture *tex = (n.empty() ? 0 : get_collection().get<Texture>(n));
161         TextureSlot slot(tex);
162         slot.name = (obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
163         load_sub(slot);
164         obj.textures.push_back(slot);
165 }
166
167 void RenderPass::Loader::uniforms()
168 {
169         if(!obj.shprog)
170                 throw InvalidState("Can't load uniforms without a shader program");
171         if(!obj.shdata)
172                 obj.shdata = new ProgramData;
173         load_sub(*obj.shdata, *obj.shprog);
174 }
175
176
177 RenderPass::TextureSlot::TextureSlot(const Texture *t):
178         texture(t)
179 { }
180
181
182 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
183         DataFile::ObjectLoader<TextureSlot>(s)
184 {
185         add("name", &TextureSlot::name);
186 }
187
188 } // namespace GL
189 } // namespace Msp