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