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