]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Add a system for creating Pipelines from loadable templates
[libs/gl.git] / source / resources.cpp
1 #include <msp/datafile/builtinsource.h>
2 #include <msp/fs/utils.h>
3 #include "animation.h"
4 #include "armature.h"
5 #include "font.h"
6 #include "keyframe.h"
7 #include "lighting.h"
8 #include "material.h"
9 #include "mesh.h"
10 #include "object.h"
11 #include "pipelinetemplate.h"
12 #include "pose.h"
13 #include "program.h"
14 #include "programcompiler.h"
15 #include "resourcemanager.h"
16 #include "resources.h"
17 #include "technique.h"
18 #include "texture1d.h"
19 #include "texture2d.h"
20 #include "texture2darray.h"
21 #include "texturecube.h"
22
23 using namespace std;
24
25 namespace Msp {
26 namespace GL {
27
28 void init_shaderlib(DataFile::BuiltinSource &);
29
30 Resources::Resources():
31         default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
32         srgb_conversion(false),
33         resource_manager(0)
34 {
35         add_type<Animation>().suffix(".anim").keyword("animation");
36         add_type<Armature>().suffix(".arma").keyword("armature");
37         add_type<Font>().keyword("font");
38         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
39         add_type<Lighting>().suffix(".lightn").keyword("lighting");
40         add_type<Material>().suffix(".mat").keyword("material");
41         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
42         add_type<Object>().keyword("object");
43         add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
44         add_type<Pose>().keyword("pose");
45         add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
46         add_type<Technique>().suffix(".tech").keyword("technique");
47         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
48         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
49         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
50         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
51         add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
52
53         add_source(get_builtins());
54 }
55
56 const DataFile::CollectionSource &Resources::get_builtins()
57 {
58         static DataFile::BuiltinSource builtins;
59         bool init_done = false;
60
61         if(!init_done)
62         {
63                 init_shaderlib(builtins);
64                 init_done = true;
65         }
66
67         return builtins;
68 }
69
70 void Resources::set_default_texture_filter(TextureFilter tf)
71 {
72         default_tex_filter = tf;
73 }
74
75 void Resources::set_srgb_conversion(bool c)
76 {
77         srgb_conversion = c;
78 }
79
80 void Resources::set_resource_manager(ResourceManager *m)
81 {
82         resource_manager = m;
83 }
84
85 Mesh *Resources::create_mesh(const string &name)
86 {
87         if(!resource_manager)
88                 return 0;
89
90         if(RefPtr<IO::Seekable> io = open_from_sources(name))
91         {
92                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
93                 resource_manager->set_resource_location(*mesh, *this, name);
94                 return mesh.release();
95         }
96
97         return 0;
98 }
99
100 Texture2D *Resources::create_texture2d(const string &name)
101 {
102         string ext = FS::extpart(name);
103         if(ext==".tex2d")
104                 return 0;
105
106         if(RefPtr<IO::Seekable> io = open_from_sources(name))
107         {
108                 Graphics::Image image;
109                 if(!resource_manager)
110                         image.load_io(*io);
111
112                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
113
114                 if(is_mipmapped(default_tex_filter))
115                 {
116                         tex->set_generate_mipmap(true);
117                         tex->set_mag_filter(LINEAR);
118                 }
119                 else
120                         tex->set_mag_filter(default_tex_filter);
121                 tex->set_min_filter(default_tex_filter);
122
123                 if(resource_manager)
124                         resource_manager->set_resource_location(*tex, *this, name);
125                 else
126                         tex->image(image, srgb_conversion);
127                 return tex.release();
128         }
129
130         return 0;
131 }
132
133 Program *Resources::create_program(const string &name)
134 {
135         string ext = FS::extpart(name);
136         if(ext==".shader")
137                 return 0;
138
139         if(RefPtr<IO::Seekable> io = open_from_sources(name))
140         {
141                 ProgramCompiler compiler;
142                 compiler.compile(*io, this);
143                 RefPtr<Program> program = new Program;
144                 compiler.add_shaders(*program);
145                 program->link();
146                 return program.release();
147         }
148
149         return 0;
150 }
151
152 } // namespace GL
153 } // namespace Msp