]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Correct some #includes
[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, Collection &c):
115         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
116 {
117         allow_pointer_reload=false;
118
119         add("shader",   &RenderPass::shprog);
120         add("material", &Loader::material);
121         add("material", &RenderPass::material);
122         add("texture",  &Loader::texture);
123         add("uniforms", &Loader::uniforms);
124 }
125
126 void RenderPass::Loader::finish()
127 {
128         if(obj.shprog)
129         {
130                 if(!obj.shdata)
131                         obj.shdata=new ProgramData;
132
133                 for(unsigned i=0; i<obj.textures.size(); ++i)
134                 {
135                         unsigned loc=obj.shprog->get_uniform_location(obj.textures[i].name);
136                         obj.shdata->uniform(loc, static_cast<int>(i));
137                 }
138         }
139 }
140
141 void RenderPass::Loader::material()
142 {
143         if(obj.material)
144                 throw InvalidState("A material is already loaded");
145
146         RefPtr<Material> mat=new Material;
147         load_sub(*mat);
148         obj.material=mat.release();
149         obj.own_material=true;
150 }
151
152 void RenderPass::Loader::texture(const string &n)
153 {
154         const Texture *tex=(n.empty() ? 0 : get_collection().get<Texture>(n));
155         TextureSlot slot(tex);
156         slot.name=(obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
157         load_sub(slot);
158         obj.textures.push_back(slot);
159 }
160
161 void RenderPass::Loader::uniforms()
162 {
163         if(!obj.shprog)
164                 throw InvalidState("Can't load uniforms without a shader program");
165         if(!obj.shdata)
166                 obj.shdata=new ProgramData;
167         load_sub(*obj.shdata, *obj.shprog);
168 }
169
170
171 RenderPass::TextureSlot::TextureSlot(const Texture *t):
172         texture(t)
173 { }
174
175
176 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
177         DataFile::ObjectLoader<TextureSlot>(s)
178 {
179         add("name", &TextureSlot::name);
180 }
181
182 } // namespace GL
183 } // namespace Msp