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