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