]> git.tdb.fi Git - libs/gl.git/blob - source/resources/resources.cpp
Load various built-in things through Resources
[libs/gl.git] / source / resources / resources.cpp
1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
3 #include "animation.h"
4 #include "armature.h"
5 #include "camera.h"
6 #include "font.h"
7 #include "keyframe.h"
8 #include "lighting.h"
9 #include "material.h"
10 #include "mesh.h"
11 #include "object.h"
12 #include "pipelinetemplate.h"
13 #include "pose.h"
14 #include "program.h"
15 #include "resourcemanager.h"
16 #include "resources.h"
17 #include "sampler.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"
24
25 using namespace std;
26
27 namespace Msp {
28 namespace GL {
29
30 void init_shaderlib(DataFile::BuiltinSource &);
31 void init_builtin_data(DataFile::BuiltinSource &);
32
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),
37         resource_manager(0)
38 {
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");
58
59         add_source(get_builtins());
60 }
61
62 const DataFile::CollectionSource &Resources::get_builtins()
63 {
64         static DataFile::BuiltinSource builtins;
65         bool init_done = false;
66
67         if(!init_done)
68         {
69                 init_builtin_data(builtins);
70                 init_shaderlib(builtins);
71                 init_done = true;
72         }
73
74         return builtins;
75 }
76
77 void Resources::set_default_texture_filter(TextureFilter tf)
78 {
79         default_tex_filter = tf;
80 }
81
82 void Resources::set_default_texture_anisotropy(float a)
83 {
84         default_tex_anisotropy = a;
85 }
86
87 void Resources::set_srgb_conversion(bool c)
88 {
89         srgb_conversion = c;
90 }
91
92 void Resources::set_resource_manager(ResourceManager *m)
93 {
94         resource_manager = m;
95 }
96
97 Material *Resources::create_material(const string &name)
98 {
99         if(RefPtr<IO::Seekable> io = open_raw(name))
100         {
101                 DataFile::Parser parser(*io, name);
102                 Material::GenericLoader ldr(this);
103                 ldr.load(parser);
104                 return ldr.get_material();
105         }
106
107         return 0;
108 }
109
110 Mesh *Resources::create_mesh(const string &name)
111 {
112         if(!resource_manager)
113                 return 0;
114
115         if(RefPtr<IO::Seekable> io = open_raw(name))
116         {
117                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
118                 resource_manager->set_resource_location(*mesh, *this, name);
119                 return mesh.release();
120         }
121
122         return 0;
123 }
124
125 Texture2D *Resources::create_texture2d(const string &name)
126 {
127         string ext = FS::extpart(name);
128         if(ext==".tex2d" && !resource_manager)
129                 return 0;
130
131         if(RefPtr<IO::Seekable> io = open_raw(name))
132         {
133                 Graphics::Image image;
134                 if(!resource_manager)
135                         image.load_io(*io);
136
137                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
138
139                 if(ext==".tex2d")
140                 {
141                         DataFile::Parser parser(*io, name);
142                         Texture2D::Loader ldr(*tex, *this);
143                         ldr.load(parser);
144                 }
145                 else
146                 {
147                         Sampler &samp = tex->get_default_sampler();
148                         if(is_mipmapped(default_tex_filter))
149                         {
150                                 tex->set_auto_generate_mipmap(true);
151                                 samp.set_mag_filter(LINEAR);
152                         }
153                         else
154                                 samp.set_mag_filter(default_tex_filter);
155                         samp.set_min_filter(default_tex_filter);
156                         samp.set_max_anisotropy(default_tex_anisotropy);
157                 }
158
159                 if(resource_manager)
160                         resource_manager->set_resource_location(*tex, *this, name);
161                 else
162                         tex->image(image);
163                 return tex.release();
164         }
165
166         return 0;
167 }
168
169 Program *Resources::create_program(const string &name)
170 {
171         string ext = FS::extpart(name);
172         if(ext==".shader")
173                 return 0;
174
175         if(RefPtr<IO::Seekable> io = open_raw(name))
176         {
177                 SL::Compiler compiler;
178                 compiler.load_source(*io, this, name);
179                 compiler.compile();
180                 RefPtr<Program> program = new Program;
181                 compiler.add_shaders(*program);
182                 program->link();
183                 return program.release();
184         }
185
186         return 0;
187 }
188
189 } // namespace GL
190 } // namespace Msp