1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
12 #include "pipelinetemplate.h"
15 #include "programcompiler.h"
16 #include "resourcemanager.h"
17 #include "resources.h"
18 #include "technique.h"
19 #include "texture1d.h"
20 #include "texture2d.h"
21 #include "texture2darray.h"
22 #include "texturecube.h"
29 void init_shaderlib(DataFile::BuiltinSource &);
31 Resources::Resources():
32 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
33 default_tex_anisotropy(1.0f),
34 srgb_conversion(false),
37 add_type<Animation>().suffix(".anim").keyword("animation");
38 add_type<Armature>().suffix(".arma").keyword("armature");
39 add_type<Camera>().keyword("camera");
40 add_type<Font>().keyword("font");
41 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
42 add_type<Lighting>().suffix(".lightn").keyword("lighting");
43 add_type<Material>().suffix(".mat").creator(&Resources::create_material);
44 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
45 add_type<Object>().keyword("object");
46 add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
47 add_type<Pose>().keyword("pose");
48 add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
49 add_type<Technique>().suffix(".tech").keyword("technique");
50 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
51 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
52 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
53 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
54 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
56 add_source(get_builtins());
59 const DataFile::CollectionSource &Resources::get_builtins()
61 static DataFile::BuiltinSource builtins;
62 bool init_done = false;
66 init_shaderlib(builtins);
73 void Resources::set_default_texture_filter(TextureFilter tf)
75 default_tex_filter = tf;
78 void Resources::set_default_texture_anisotropy(float a)
80 default_tex_anisotropy = a;
83 void Resources::set_srgb_conversion(bool c)
88 void Resources::set_resource_manager(ResourceManager *m)
93 Material *Resources::create_material(const string &name)
95 if(RefPtr<IO::Seekable> io = open_raw(name))
97 DataFile::Parser parser(*io, name);
98 Material::GenericLoader ldr(this);
100 return ldr.get_material();
106 Mesh *Resources::create_mesh(const string &name)
108 if(!resource_manager)
111 if(RefPtr<IO::Seekable> io = open_raw(name))
113 RefPtr<Mesh> mesh = new Mesh(resource_manager);
114 resource_manager->set_resource_location(*mesh, *this, name);
115 return mesh.release();
121 Texture2D *Resources::create_texture2d(const string &name)
123 string ext = FS::extpart(name);
127 if(RefPtr<IO::Seekable> io = open_raw(name))
129 Graphics::Image image;
130 if(!resource_manager)
133 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
135 if(is_mipmapped(default_tex_filter))
137 tex->set_auto_generate_mipmap(true);
138 tex->set_mag_filter(LINEAR);
141 tex->set_mag_filter(default_tex_filter);
142 tex->set_min_filter(default_tex_filter);
143 tex->set_max_anisotropy(default_tex_anisotropy);
146 resource_manager->set_resource_location(*tex, *this, name);
148 tex->image(image, srgb_conversion);
149 return tex.release();
155 Program *Resources::create_program(const string &name)
157 string ext = FS::extpart(name);
161 if(RefPtr<IO::Seekable> io = open_raw(name))
163 ProgramCompiler compiler;
164 compiler.compile(*io, this, name);
165 RefPtr<Program> program = new Program;
166 compiler.add_shaders(*program);
168 return program.release();