1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
12 #include "programcompiler.h"
13 #include "resourcemanager.h"
14 #include "resources.h"
15 #include "technique.h"
16 #include "texture1d.h"
17 #include "texture2d.h"
18 #include "texture2darray.h"
19 #include "texturecube.h"
26 void init_shaderlib(DataFile::BuiltinSource &);
28 Resources::Resources():
29 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
30 srgb_conversion(false),
33 add_type<Animation>().suffix(".anim").keyword("animation");
34 add_type<Armature>().suffix(".arma").keyword("armature");
35 add_type<Font>().keyword("font");
36 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
37 add_type<Material>().suffix(".mat").keyword("material");
38 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
39 add_type<Object>().keyword("object");
40 add_type<Pose>().keyword("pose");
41 add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
42 add_type<Technique>().suffix(".tech").keyword("technique");
43 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
44 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
45 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
46 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
47 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
49 add_source(get_builtins());
52 const DataFile::CollectionSource &Resources::get_builtins()
54 static DataFile::BuiltinSource builtins;
55 bool init_done = false;
59 init_shaderlib(builtins);
66 void Resources::set_default_texture_filter(TextureFilter tf)
68 default_tex_filter = tf;
71 void Resources::set_srgb_conversion(bool c)
76 void Resources::set_resource_manager(ResourceManager *m)
81 Mesh *Resources::create_mesh(const string &name)
86 if(RefPtr<IO::Seekable> io = open_from_sources(name))
88 RefPtr<GL::Mesh> mesh = new GL::Mesh(resource_manager);
89 resource_manager->set_resource_location(*mesh, *this, name);
90 return mesh.release();
96 Texture2D *Resources::create_texture2d(const string &name)
98 string ext = FS::extpart(name);
102 if(RefPtr<IO::Seekable> io = open_from_sources(name))
104 Graphics::Image image;
105 if(!resource_manager)
108 RefPtr<GL::Texture2D> tex = new GL::Texture2D(resource_manager);
110 if(is_mipmapped(default_tex_filter))
112 tex->set_generate_mipmap(true);
113 tex->set_mag_filter(LINEAR);
116 tex->set_mag_filter(default_tex_filter);
117 tex->set_min_filter(default_tex_filter);
120 resource_manager->set_resource_location(*tex, *this, name);
122 tex->image(image, srgb_conversion);
123 return tex.release();
129 Program *Resources::create_program(const string &name)
131 string ext = FS::extpart(name);
135 if(RefPtr<IO::Seekable> io = open_from_sources(name))
137 ProgramCompiler compiler;
138 compiler.compile(*io, this);
139 RefPtr<Program> program = new Program;
140 compiler.add_shaders(*program);
142 return program.release();