]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Add a scene type property to the Blender exporter
[libs/gl.git] / source / resources / 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 "module.h"
12 #include "object.h"
13 #include "pipelinetemplate.h"
14 #include "pose.h"
15 #include "program.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
18 #include "sampler.h"
19 #include "scene.h"
20 #include "technique.h"
21 #include "texture1d.h"
22 #include "texture2d.h"
23 #include "texture2darray.h"
24 #include "texturecube.h"
25 #include "glsl/compiler.h"
26
27 using namespace std;
28
29 namespace Msp {
30 namespace GL {
31
32 void init_shaderlib(DataFile::BuiltinSource &);
33 void init_builtin_data(DataFile::BuiltinSource &);
34
35 Resources::Resources():
36         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
37         default_tex_anisotropy(1.0f),
38         srgb_conversion(false),
39         resource_manager(0)
40 {
41         add_type<Animation>().suffix(".anim").keyword("animation");
42         add_type<Armature>().suffix(".arma").keyword("armature");
43         add_type<Camera>().keyword("camera");
44         add_type<Font>().keyword("font");
45         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
46         add_type<Lighting>().suffix(".lightn").keyword("lighting");
47         add_type<Material>().suffix(".mat").creator(&Resources::create_material);
48         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
49         add_type<Module>().suffix(".glsl").suffix(".spv").creator(&Resources::create_module);
50         add_type<Object>().keyword("object");
51         add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
52         add_type<Pose>().keyword("pose");
53         add_type<Program>().keyword("shader").creator(&Resources::create_program);
54         add_type<Sampler>().suffix(".samp").keyword("sampler");
55         add_type<Scene>().suffix(".scene").creator(&Resources::create_scene);
56         add_type<Technique>().suffix(".tech").keyword("technique");
57         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
58         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
59         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
60         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
61         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
62
63         add_source(get_builtins());
64 }
65
66 const DataFile::CollectionSource &Resources::get_builtins()
67 {
68         static DataFile::BuiltinSource builtins;
69         bool init_done = false;
70
71         if(!init_done)
72         {
73                 init_builtin_data(builtins);
74                 init_shaderlib(builtins);
75                 init_done = true;
76         }
77
78         return builtins;
79 }
80
81 void Resources::set_default_texture_filter(TextureFilter tf)
82 {
83         default_tex_filter = tf;
84 }
85
86 void Resources::set_default_texture_anisotropy(float a)
87 {
88         default_tex_anisotropy = a;
89 }
90
91 void Resources::set_srgb_conversion(bool c)
92 {
93         srgb_conversion = c;
94 }
95
96 void Resources::set_resource_manager(ResourceManager *m)
97 {
98         resource_manager = m;
99 }
100
101 Material *Resources::create_material(const string &name)
102 {
103         if(RefPtr<IO::Seekable> io = open_raw(name))
104         {
105                 DataFile::Parser parser(*io, name);
106                 Material::GenericLoader ldr(this);
107                 ldr.load(parser);
108                 return ldr.get_material();
109         }
110
111         return 0;
112 }
113
114 Mesh *Resources::create_mesh(const string &name)
115 {
116         if(!resource_manager)
117                 return 0;
118
119         if(RefPtr<IO::Seekable> io = open_raw(name))
120         {
121                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
122                 resource_manager->set_resource_location(*mesh, *this, name);
123                 return mesh.release();
124         }
125
126         return 0;
127 }
128
129 Scene *Resources::create_scene(const string &name)
130 {
131         if(RefPtr<IO::Seekable> io = open_raw(name))
132         {
133                 DataFile::Parser parser(*io, name);
134                 Scene::GenericLoader ldr(*this);
135                 ldr.load(parser);
136                 return ldr.get_scene();
137         }
138
139         return 0;
140 }
141
142 Texture2D *Resources::create_texture2d(const string &name)
143 {
144         string ext = FS::extpart(name);
145         if(ext==".tex2d" && !resource_manager)
146                 return 0;
147
148         if(RefPtr<IO::Seekable> io = open_raw(name))
149         {
150                 RefPtr<Texture2D> tex;
151
152                 if(ext==".tex2d")
153                 {
154                         tex = new Texture2D(resource_manager);
155                         DataFile::Parser parser(*io, name);
156                         Texture2D::Loader ldr(*tex, *this);
157                         ldr.load(parser);
158                 }
159                 else
160                 {
161                         // Verify that the image is loadable
162                         Graphics::Image image;
163                         if(!resource_manager)
164                                 image.load_io(*io);
165
166                         tex = new Texture2D(resource_manager);
167                         Sampler &samp = tex->get_default_sampler();
168                         if(is_mipmapped(default_tex_filter))
169                         {
170                                 tex->set_auto_generate_mipmap(true);
171                                 samp.set_mag_filter(LINEAR);
172                         }
173                         else
174                                 samp.set_mag_filter(default_tex_filter);
175                         samp.set_min_filter(default_tex_filter);
176                         samp.set_max_anisotropy(default_tex_anisotropy);
177
178                         if(resource_manager)
179                                 resource_manager->set_resource_location(*tex, *this, name);
180                         else
181                                 tex->image(image);
182                 }
183
184                 return tex.release();
185         }
186
187         return 0;
188 }
189
190 Module *Resources::create_module(const string &name)
191 {
192         string ext = FS::extpart(name);
193         if(ext!=".glsl" && ext!=".spv")
194                 return 0;
195
196         if(RefPtr<IO::Seekable> io = open_raw(name))
197         {
198                 if(ext==".glsl")
199                 {
200                         RefPtr<GlslModule> module = new GlslModule;
201                         module->load_source(*io, this, name);
202                         return module.release();
203                 }
204                 else if(ext==".spv")
205                 {
206                         RefPtr<SpirVModule> module = new SpirVModule;
207                         module->load_code(*io);
208                         return module.release();
209                 }
210         }
211
212         return 0;
213 }
214
215 Program *Resources::create_program(const string &name)
216 {
217         string ext = FS::extpart(name);
218         string base = FS::basepart(name);
219         string ext2 = FS::extpart(base);
220         if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
221         {
222                 Module &module = get<Module>(base);
223                 RefPtr<Program> shprog = new Program;
224                 shprog->add_stages(module);
225                 shprog->link();
226                 return shprog.release();
227         }
228
229         return 0;
230 }
231
232 } // namespace GL
233 } // namespace Msp