]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Support linear to sRGB conversion when loading materials and textures
[libs/gl.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include "animation.h"
3 #include "armature.h"
4 #include "font.h"
5 #include "keyframe.h"
6 #include "material.h"
7 #include "mesh.h"
8 #include "object.h"
9 #include "pose.h"
10 #include "program.h"
11 #include "resources.h"
12 #include "technique.h"
13 #include "texture2d.h"
14 #include "texturecube.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 Resources::Resources():
22         default_tex_filter(LINEAR_MIPMAP_LINEAR),
23         srgb_conversion(false)
24 {
25         add_type<Animation>().suffix(".anim").keyword("animation");
26         add_type<Armature>().suffix(".arma").keyword("armature");
27         add_type<Font>().keyword("font");
28         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
29         add_type<Material>().suffix(".mat").keyword("material");
30         add_type<Mesh>().keyword("mesh");
31         add_type<Object>().keyword("object");
32         add_type<Pose>().keyword("pose");
33         add_type<Program>().keyword("shader");
34         add_type<Technique>().suffix(".tech").keyword("technique");
35         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
36         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
37 }
38
39 void Resources::set_default_texture_filter(TextureFilter tf)
40 {
41         default_tex_filter = tf;
42 }
43
44 void Resources::set_srgb_conversion(bool c)
45 {
46         srgb_conversion = c;
47 }
48
49 Texture2D *Resources::create_texture2d(const string &name)
50 {
51         string ext = FS::extpart(name);
52         if(ext==".tex2d")
53                 return 0;
54
55         if(RefPtr<IO::Seekable> io = open_from_sources(name))
56         {
57                 Graphics::Image image;
58                 image.load_io(*io);
59
60                 RefPtr<GL::Texture2D> tex = new GL::Texture2D;
61
62                 if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR ||
63                         default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR)
64                 {
65                         tex->set_generate_mipmap(true);
66                         tex->set_mag_filter(LINEAR);
67                 }
68                 else
69                         tex->set_mag_filter(default_tex_filter);
70                 tex->set_min_filter(default_tex_filter);
71
72                 tex->image(image);
73                 return tex.release();
74         }
75
76         return 0;
77 }
78
79 } // namespace GL
80 } // namespace Msp