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"
19 #include "technique.h"
20 #include "texture1d.h"
21 #include "texture2d.h"
22 #include "texture2darray.h"
23 #include "texturecube.h"
30 void init_shaderlib(DataFile::BuiltinSource &);
32 Resources::Resources():
33 default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
34 default_tex_anisotropy(1.0f),
35 srgb_conversion(false),
38 add_type<Animation>().suffix(".anim").keyword("animation");
39 add_type<Armature>().suffix(".arma").keyword("armature");
40 add_type<Camera>().keyword("camera");
41 add_type<Font>().keyword("font");
42 add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
43 add_type<Lighting>().suffix(".lightn").keyword("lighting");
44 add_type<Material>().suffix(".mat").creator(&Resources::create_material);
45 add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
46 add_type<Object>().keyword("object");
47 add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
48 add_type<Pose>().keyword("pose");
49 add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
50 add_type<Sampler>().suffix(".samp").keyword("sampler");
51 add_type<Technique>().suffix(".tech").keyword("technique");
52 add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
53 add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
54 add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
55 add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
56 add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
58 add_source(get_builtins());
61 const DataFile::CollectionSource &Resources::get_builtins()
63 static DataFile::BuiltinSource builtins;
64 bool init_done = false;
68 init_shaderlib(builtins);
75 void Resources::set_default_texture_filter(TextureFilter tf)
77 default_tex_filter = tf;
80 void Resources::set_default_texture_anisotropy(float a)
82 default_tex_anisotropy = a;
85 void Resources::set_srgb_conversion(bool c)
90 void Resources::set_resource_manager(ResourceManager *m)
95 Material *Resources::create_material(const string &name)
97 if(RefPtr<IO::Seekable> io = open_raw(name))
99 DataFile::Parser parser(*io, name);
100 Material::GenericLoader ldr(this);
102 return ldr.get_material();
108 Mesh *Resources::create_mesh(const string &name)
110 if(!resource_manager)
113 if(RefPtr<IO::Seekable> io = open_raw(name))
115 RefPtr<Mesh> mesh = new Mesh(resource_manager);
116 resource_manager->set_resource_location(*mesh, *this, name);
117 return mesh.release();
123 Texture2D *Resources::create_texture2d(const string &name)
125 string ext = FS::extpart(name);
126 if(ext==".tex2d" && !resource_manager)
129 if(RefPtr<IO::Seekable> io = open_raw(name))
131 Graphics::Image image;
132 if(!resource_manager)
135 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
139 DataFile::Parser parser(*io, name);
140 Texture2D::Loader ldr(*tex, *this);
145 Sampler &samp = tex->get_default_sampler();
146 if(is_mipmapped(default_tex_filter))
148 tex->set_auto_generate_mipmap(true);
149 samp.set_mag_filter(LINEAR);
152 samp.set_mag_filter(default_tex_filter);
153 samp.set_min_filter(default_tex_filter);
154 samp.set_max_anisotropy(default_tex_anisotropy);
158 resource_manager->set_resource_location(*tex, *this, name);
161 return tex.release();
167 Program *Resources::create_program(const string &name)
169 string ext = FS::extpart(name);
173 if(RefPtr<IO::Seekable> io = open_raw(name))
175 ProgramCompiler compiler;
176 compiler.compile(*io, this, name);
177 RefPtr<Program> program = new Program;
178 compiler.add_shaders(*program);
180 return program.release();