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").keyword("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 Mesh *Resources::create_mesh(const string &name)
98 if(RefPtr<IO::Seekable> io = open_raw(name))
100 RefPtr<Mesh> mesh = new Mesh(resource_manager);
101 resource_manager->set_resource_location(*mesh, *this, name);
102 return mesh.release();
108 Texture2D *Resources::create_texture2d(const string &name)
110 string ext = FS::extpart(name);
114 if(RefPtr<IO::Seekable> io = open_raw(name))
116 Graphics::Image image;
117 if(!resource_manager)
120 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
122 if(is_mipmapped(default_tex_filter))
124 tex->set_auto_generate_mipmap(true);
125 tex->set_mag_filter(LINEAR);
128 tex->set_mag_filter(default_tex_filter);
129 tex->set_min_filter(default_tex_filter);
130 tex->set_max_anisotropy(default_tex_anisotropy);
133 resource_manager->set_resource_location(*tex, *this, name);
135 tex->image(image, srgb_conversion);
136 return tex.release();
142 Program *Resources::create_program(const string &name)
144 string ext = FS::extpart(name);
148 if(RefPtr<IO::Seekable> io = open_raw(name))
150 ProgramCompiler compiler;
151 compiler.compile(*io, this, name);
152 RefPtr<Program> program = new Program;
153 compiler.add_shaders(*program);
155 return program.release();