1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
12 #include "pipelinetemplate.h"
15 #include "resourcemanager.h"
16 #include "resources.h"
18 #include "technique.h"
19 #include "texture1d.h"
20 #include "texture2d.h"
21 #include "texture2darray.h"
22 #include "texturecube.h"
23 #include "glsl/compiler.h"
30 void init_shaderlib(DataFile::BuiltinSource &);
31 void init_builtin_data(DataFile::BuiltinSource &);
33 Resources::Resources():
34 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
35 default_tex_anisotropy(1.0f),
36 srgb_conversion(false),
39 add_type<Animation>().suffix(".anim").keyword("animation");
40 add_type<Armature>().suffix(".arma").keyword("armature");
41 add_type<Camera>().keyword("camera");
42 add_type<Font>().keyword("font");
43 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
44 add_type<Lighting>().suffix(".lightn").keyword("lighting");
45 add_type<Material>().suffix(".mat").creator(&Resources::create_material);
46 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
47 add_type<Object>().keyword("object");
48 add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
49 add_type<Pose>().keyword("pose");
50 add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
51 add_type<Sampler>().suffix(".samp").keyword("sampler");
52 add_type<Technique>().suffix(".tech").keyword("technique");
53 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
54 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
55 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
56 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
57 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
59 add_source(get_builtins());
62 const DataFile::CollectionSource &Resources::get_builtins()
64 static DataFile::BuiltinSource builtins;
65 bool init_done = false;
69 init_builtin_data(builtins);
70 init_shaderlib(builtins);
77 void Resources::set_default_texture_filter(TextureFilter tf)
79 default_tex_filter = tf;
82 void Resources::set_default_texture_anisotropy(float a)
84 default_tex_anisotropy = a;
87 void Resources::set_srgb_conversion(bool c)
92 void Resources::set_resource_manager(ResourceManager *m)
97 Material *Resources::create_material(const string &name)
99 if(RefPtr<IO::Seekable> io = open_raw(name))
101 DataFile::Parser parser(*io, name);
102 Material::GenericLoader ldr(this);
104 return ldr.get_material();
110 Mesh *Resources::create_mesh(const string &name)
112 if(!resource_manager)
115 if(RefPtr<IO::Seekable> io = open_raw(name))
117 RefPtr<Mesh> mesh = new Mesh(resource_manager);
118 resource_manager->set_resource_location(*mesh, *this, name);
119 return mesh.release();
125 Texture2D *Resources::create_texture2d(const string &name)
127 string ext = FS::extpart(name);
128 if(ext==".tex2d" && !resource_manager)
131 if(RefPtr<IO::Seekable> io = open_raw(name))
133 Graphics::Image image;
134 if(!resource_manager)
137 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
141 DataFile::Parser parser(*io, name);
142 Texture2D::Loader ldr(*tex, *this);
147 Sampler &samp = tex->get_default_sampler();
148 if(is_mipmapped(default_tex_filter))
150 tex->set_auto_generate_mipmap(true);
151 samp.set_mag_filter(LINEAR);
154 samp.set_mag_filter(default_tex_filter);
155 samp.set_min_filter(default_tex_filter);
156 samp.set_max_anisotropy(default_tex_anisotropy);
160 resource_manager->set_resource_location(*tex, *this, name);
163 return tex.release();
169 Program *Resources::create_program(const string &name)
171 string ext = FS::extpart(name);
175 if(RefPtr<IO::Seekable> io = open_raw(name))
177 SL::Compiler compiler;
178 compiler.load_source(*io, this, name);
180 RefPtr<Program> program = new Program;
181 compiler.add_shaders(*program);
183 return program.release();