]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Implement loading functionality for texture classes that were missing it
[libs/gl.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/gl/extensions/sgis_generate_mipmap.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 "resourcemanager.h"
13 #include "resources.h"
14 #include "technique.h"
15 #include "texture1d.h"
16 #include "texture2d.h"
17 #include "texturecube.h"
18
19 using namespace std;
20
21 namespace Msp {
22 namespace GL {
23
24 Resources::Resources():
25         default_tex_filter(SGIS_generate_mipmap ? LINEAR_MIPMAP_LINEAR : LINEAR),
26         srgb_conversion(false),
27         resource_manager(0)
28 {
29         add_type<Animation>().suffix(".anim").keyword("animation");
30         add_type<Armature>().suffix(".arma").keyword("armature");
31         add_type<Font>().keyword("font");
32         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
33         add_type<Material>().suffix(".mat").keyword("material");
34         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
35         add_type<Object>().keyword("object");
36         add_type<Pose>().keyword("pose");
37         add_type<Program>().keyword("shader");
38         add_type<Technique>().suffix(".tech").keyword("technique");
39         add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
40         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
41         add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
42         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
43 }
44
45 void Resources::set_default_texture_filter(TextureFilter tf)
46 {
47         default_tex_filter = tf;
48 }
49
50 void Resources::set_srgb_conversion(bool c)
51 {
52         srgb_conversion = c;
53 }
54
55 void Resources::set_resource_manager(ResourceManager *m)
56 {
57         resource_manager = m;
58 }
59
60 Mesh *Resources::create_mesh(const string &name)
61 {
62         if(!resource_manager)
63                 return 0;
64
65         if(RefPtr<IO::Seekable> io = open_from_sources(name))
66         {
67                 RefPtr<GL::Mesh> mesh = new GL::Mesh(resource_manager);
68                 resource_manager->set_resource_location(*mesh, *this, name);
69                 return mesh.release();
70         }
71
72         return 0;
73 }
74
75 Texture2D *Resources::create_texture2d(const string &name)
76 {
77         string ext = FS::extpart(name);
78         if(ext==".tex2d")
79                 return 0;
80
81         if(RefPtr<IO::Seekable> io = open_from_sources(name))
82         {
83                 Graphics::Image image;
84                 if(!resource_manager)
85                         image.load_io(*io);
86
87                 RefPtr<GL::Texture2D> tex = new GL::Texture2D(resource_manager);
88
89                 if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR ||
90                         default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR)
91                 {
92                         tex->set_generate_mipmap(true);
93                         tex->set_mag_filter(LINEAR);
94                 }
95                 else
96                         tex->set_mag_filter(default_tex_filter);
97                 tex->set_min_filter(default_tex_filter);
98
99                 if(resource_manager)
100                         resource_manager->set_resource_location(*tex, *this, name);
101                 else
102                         tex->image(image, srgb_conversion);
103                 return tex.release();
104         }
105
106         return 0;
107 }
108
109 } // namespace GL
110 } // namespace Msp