]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Add integer overload of ProgramData::uniform1_array
[libs/gl.git] / source / renderpass.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
3 #include "error.h"
4 #include "material.h"
5 #include "renderpass.h"
6 #include "program.h"
7 #include "programdata.h"
8 #include "renderer.h"
9 #include "texture.h"
10 #include "texture2d.h"
11 #include "texturing.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 RenderPass::RenderPass():
19         shprog(0),
20         shdata(0),
21         material(0),
22         texturing(0),
23         back_faces(false)
24 { }
25
26 RenderPass::RenderPass(const RenderPass &other):
27         shprog(other.shprog),
28         shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
29         material(other.material),
30         texturing(other.texturing ? new Texturing(*other.texturing) : 0),
31         tex_names(other.tex_names),
32         back_faces(other.back_faces)
33 { }
34
35 RenderPass::~RenderPass()
36 {
37         delete texturing;
38 }
39
40 void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
41 {
42         shprog = prog;
43         shdata = data;
44         shdata.keep();
45 }
46
47 void RenderPass::set_material(const Material *mat)
48 {
49         material = mat;
50         material.keep();
51 }
52
53 void RenderPass::set_texture(unsigned index, const Texture *tex)
54 {
55         if(!texturing)
56                 texturing = new Texturing;
57
58         texturing->attach(index, *tex);
59 }
60
61 int RenderPass::get_texture_index(const string &n) const
62 {
63         map<string, unsigned>::const_iterator i = tex_names.find(n);
64         if(i==tex_names.end())
65                 return -1;
66         return i->second;
67 }
68
69 void RenderPass::apply(Renderer &renderer) const
70 {
71         renderer.set_texturing(texturing);
72         renderer.set_material(material.get());
73         renderer.set_shader_program(shprog, shdata.get());
74         renderer.set_reverse_winding(back_faces);
75 }
76
77
78 RenderPass::Loader::Loader(RenderPass &p):
79         DataFile::CollectionObjectLoader<RenderPass>(p, 0)
80 {
81         init();
82 }
83
84 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
85         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
86 {
87         init();
88 }
89
90 void RenderPass::Loader::init()
91 {
92         add("shader",   &RenderPass::shprog);
93         add("material", &Loader::material_inline);
94         add("material", &Loader::material);
95         add("back_faces",&RenderPass::back_faces);
96         add("texunit",  &Loader::texunit);
97         add("texunit",  &Loader::texunit_named);
98         add("uniforms", &Loader::uniforms);
99 }
100
101 void RenderPass::Loader::material_inline()
102 {
103         RefPtr<Material> mat = new Material;
104         if(coll)
105                 load_sub(*mat, get_collection());
106         else
107                 load_sub(*mat);
108         obj.material = mat;
109 }
110
111 void RenderPass::Loader::material(const string &name)
112 {
113         obj.material = &get_collection().get<Material>(name);
114         obj.material.keep();
115 }
116
117 void RenderPass::Loader::texunit(unsigned i)
118 {
119         if(!obj.texturing)
120                 obj.texturing = new Texturing;
121         TextureLoader ldr(*obj.texturing, i, coll);
122         load_sub_with(ldr);
123 }
124
125 void RenderPass::Loader::texunit_named(unsigned i, const string &n)
126 {
127         texunit(i);
128         obj.tex_names[n] = i;
129 }
130
131 void RenderPass::Loader::uniforms()
132 {
133         if(!obj.shprog)
134                 throw invalid_operation("RenderPass::Loader::uniforms");
135         RefPtr<ProgramData> shd = new ProgramData;
136         load_sub(*shd);
137         obj.shdata = shd;
138 }
139
140
141 RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
142         DataFile::CollectionObjectLoader<Texturing>(t, c),
143         index(i)
144 {
145         add("texture",   &TextureLoader::texture);
146         add("texture2d", &TextureLoader::texture2d);
147 }
148
149 void RenderPass::TextureLoader::finish()
150 {
151         if(tex)
152         {
153                 obj.attach(index, *tex);
154                 tex.release();
155         }
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         if(coll)
168                 load_sub(static_cast<Texture2D &>(*tex), get_collection());
169         else
170                 load_sub(static_cast<Texture2D &>(*tex));
171 }
172
173 } // namespace GL
174 } // namespace Msp