]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Check uniforms as a hint for texunit to use for material textures
[libs/gl.git] / source / material.cpp
1 #include "basicmaterial.h"
2 #include "gl.h"
3 #include "resources.h"
4 #include "texturing.h"
5 #include "uniform.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
13 {
14         if(!tex)
15                 return;
16
17         int unit = -1;
18
19         if(const Uniform *uni = tex_shdata.find_uniform(name))
20                 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
21                         unit = uni_int->get();
22
23         if(unit<0)
24                 unit = texturing.find_free_unit(name);
25         if(unit<0)
26                 throw runtime_error("no free texunit");
27
28         texturing.attach(unit, *tex);
29         tex_shdata.uniform(name, unit);
30 }
31
32 Material::MaterialRegistry &Material::get_material_registry()
33 {
34         static MaterialRegistry registry;
35         static bool initialized = false;
36         if(!initialized)
37         {
38                 registry.register_type<BasicMaterial>("basic");
39         }
40         return registry;
41 }
42
43
44 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
45
46 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
47         coll(c)
48 {
49         set_actions(shared_actions);
50 }
51
52 void Material::GenericLoader::init_actions()
53 {
54         get_material_registry().add_all(*this);
55 }
56
57 } // namespace GL
58 } // namespace Msp