]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Redesign the material system
[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 "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 "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"
23
24 using namespace std;
25
26 namespace Msp {
27 namespace GL {
28
29 void init_shaderlib(DataFile::BuiltinSource &);
30
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),
35         resource_manager(0)
36 {
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").creator(&Resources::create_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");
55
56         add_source(get_builtins());
57 }
58
59 const DataFile::CollectionSource &Resources::get_builtins()
60 {
61         static DataFile::BuiltinSource builtins;
62         bool init_done = false;
63
64         if(!init_done)
65         {
66                 init_shaderlib(builtins);
67                 init_done = true;
68         }
69
70         return builtins;
71 }
72
73 void Resources::set_default_texture_filter(TextureFilter tf)
74 {
75         default_tex_filter = tf;
76 }
77
78 void Resources::set_default_texture_anisotropy(float a)
79 {
80         default_tex_anisotropy = a;
81 }
82
83 void Resources::set_srgb_conversion(bool c)
84 {
85         srgb_conversion = c;
86 }
87
88 void Resources::set_resource_manager(ResourceManager *m)
89 {
90         resource_manager = m;
91 }
92
93 Material *Resources::create_material(const string &name)
94 {
95         if(RefPtr<IO::Seekable> io = open_raw(name))
96         {
97                 DataFile::Parser parser(*io, name);
98                 Material::GenericLoader ldr(this);
99                 ldr.load(parser);
100                 return ldr.get_material();
101         }
102
103         return 0;
104 }
105
106 Mesh *Resources::create_mesh(const string &name)
107 {
108         if(!resource_manager)
109                 return 0;
110
111         if(RefPtr<IO::Seekable> io = open_raw(name))
112         {
113                 RefPtr<Mesh> mesh = new Mesh(resource_manager);
114                 resource_manager->set_resource_location(*mesh, *this, name);
115                 return mesh.release();
116         }
117
118         return 0;
119 }
120
121 Texture2D *Resources::create_texture2d(const string &name)
122 {
123         string ext = FS::extpart(name);
124         if(ext==".tex2d")
125                 return 0;
126
127         if(RefPtr<IO::Seekable> io = open_raw(name))
128         {
129                 Graphics::Image image;
130                 if(!resource_manager)
131                         image.load_io(*io);
132
133                 RefPtr<Texture2D> tex = new Texture2D(resource_manager);
134
135                 if(is_mipmapped(default_tex_filter))
136                 {
137                         tex->set_auto_generate_mipmap(true);
138                         tex->set_mag_filter(LINEAR);
139                 }
140                 else
141                         tex->set_mag_filter(default_tex_filter);
142                 tex->set_min_filter(default_tex_filter);
143                 tex->set_max_anisotropy(default_tex_anisotropy);
144
145                 if(resource_manager)
146                         resource_manager->set_resource_location(*tex, *this, name);
147                 else
148                         tex->image(image, srgb_conversion);
149                 return tex.release();
150         }
151
152         return 0;
153 }
154
155 Program *Resources::create_program(const string &name)
156 {
157         string ext = FS::extpart(name);
158         if(ext==".shader")
159                 return 0;
160
161         if(RefPtr<IO::Seekable> io = open_raw(name))
162         {
163                 ProgramCompiler compiler;
164                 compiler.compile(*io, this, name);
165                 RefPtr<Program> program = new Program;
166                 compiler.add_shaders(*program);
167                 program->link();
168                 return program.release();
169         }
170
171         return 0;
172 }
173
174 } // namespace GL
175 } // namespace Msp