]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Use samplers in effects and postprocessors
[libs/gl.git] / source / resources.cpp
1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
3 #include "animation.h"
4 #include "armature.h"
5 #include "camera.h"
6 #include "font.h"
7 #include "keyframe.h"
8 #include "lighting.h"
9 #include "material.h"
10 #include "mesh.h"
11 #include "object.h"
12 #include "pipelinetemplate.h"
13 #include "pose.h"
14 #include "program.h"
15 #include "programcompiler.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
18 #include "sampler.h"
19 #include "technique.h"
20 #include "texture1d.h"
21 #include "texture2d.h"
22 #include "texture2darray.h"
23 #include "texturecube.h"
24
25 using namespace std;
26
27 namespace Msp {
28 namespace GL {
29
30 void init_shaderlib(DataFile::BuiltinSource &);
31
32 Resources::Resources():
33         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
34         default_tex_anisotropy(1.0f),
35         srgb_conversion(false),
36         resource_manager(0)
37 {
38         add_type<Animation>().suffix(".anim").keyword("animation");
39         add_type<Armature>().suffix(".arma").keyword("armature");
40         add_type<Camera>().keyword("camera");
41         add_type<Font>().keyword("font");
42         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
43         add_type<Lighting>().suffix(".lightn").keyword("lighting");
44         add_type<Material>().suffix(".mat").creator(&Resources::create_material);
45         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
46         add_type<Object>().keyword("object");
47         add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
48         add_type<Pose>().keyword("pose");
49         add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
50         add_type<Sampler>().suffix(".samp").keyword("sampler");
51         add_type<Technique>().suffix(".tech").keyword("technique");
52         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
53         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
54         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
55         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
56         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
57
58         add_source(get_builtins());
59 }
60
61 const DataFile::CollectionSource &Resources::get_builtins()
62 {
63         static DataFile::BuiltinSource builtins;
64         bool init_done = false;
65
66         if(!init_done)
67         {
68                 init_shaderlib(builtins);
69                 init_done = true;
70         }
71
72         return builtins;
73 }
74
75 void Resources::set_default_texture_filter(TextureFilter tf)
76 {
77         default_tex_filter = tf;
78 }
79
80 void Resources::set_default_texture_anisotropy(float a)
81 {
82         default_tex_anisotropy = a;
83 }
84
85 void Resources::set_srgb_conversion(bool c)
86 {
87         srgb_conversion = c;
88 }
89
90 void Resources::set_resource_manager(ResourceManager *m)
91 {
92         resource_manager = m;
93 }
94
95 Material *Resources::create_material(const string &name)
96 {
97         if(RefPtr<IO::Seekable> io = open_raw(name))
98         {
99                 DataFile::Parser parser(*io, name);
100                 Material::GenericLoader ldr(this);
101                 ldr.load(parser);
102                 return ldr.get_material();
103         }
104
105         return 0;
106 }
107
108 Mesh *Resources::create_mesh(const string &name)
109 {
110         if(!resource_manager)
111                 return 0;
112
113         if(RefPtr<IO::Seekable> io = open_raw(name))
114         {
115                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
116                 resource_manager->set_resource_location(*mesh, *this, name);
117                 return mesh.release();
118         }
119
120         return 0;
121 }
122
123 Texture2D *Resources::create_texture2d(const string &name)
124 {
125         string ext = FS::extpart(name);
126         if(ext==".tex2d" && !resource_manager)
127                 return 0;
128
129         if(RefPtr<IO::Seekable> io = open_raw(name))
130         {
131                 Graphics::Image image;
132                 if(!resource_manager)
133                         image.load_io(*io);
134
135                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
136
137                 if(ext==".tex2d")
138                 {
139                         DataFile::Parser parser(*io, name);
140                         Texture2D::Loader ldr(*tex, *this);
141                         ldr.load(parser);
142                 }
143                 else
144                 {
145                         Sampler &samp = tex->get_default_sampler();
146                         if(is_mipmapped(default_tex_filter))
147                         {
148                                 tex->set_auto_generate_mipmap(true);
149                                 samp.set_mag_filter(LINEAR);
150                         }
151                         else
152                                 samp.set_mag_filter(default_tex_filter);
153                         samp.set_min_filter(default_tex_filter);
154                         samp.set_max_anisotropy(default_tex_anisotropy);
155                 }
156
157                 if(resource_manager)
158                         resource_manager->set_resource_location(*tex, *this, name);
159                 else
160                         tex->image(image);
161                 return tex.release();
162         }
163
164         return 0;
165 }
166
167 Program *Resources::create_program(const string &name)
168 {
169         string ext = FS::extpart(name);
170         if(ext==".shader")
171                 return 0;
172
173         if(RefPtr<IO::Seekable> io = open_raw(name))
174         {
175                 ProgramCompiler compiler;
176                 compiler.compile(*io, this, name);
177                 RefPtr<Program> program = new Program;
178                 compiler.add_shaders(*program);
179                 program->link();
180                 return program.release();
181         }
182
183         return 0;
184 }
185
186 } // namespace GL
187 } // namespace Msp