support for floating point images => needed for skybox
[demo] / src / shader_manager.cc
1 #include <map>
2
3 #include "gfxapi.h"
4
5 #include "shader_manager.h"
6 #include "opengl/shader-gl.h"
7
8 ShaderManager::ShaderManager() {}
9 ShaderManager::~ShaderManager() {}
10
11 void ShaderManager::add_shader(Shader *sdr, const char *name)
12 {
13         shaders[name] = sdr;
14 }
15
16 Shader *ShaderManager::load_shader(const char *name, SType type)
17 {
18         std::map<std::string, Shader *>::const_iterator it;
19         it = shaders.find(name);
20
21         if(it != shaders.end()) {
22                 return it->second;
23         }
24
25         Shader *sdr = gfx_create_shader();
26
27         std::string fname = path.empty() ? std::string(name) : path + "/" + std::string(name);
28
29         if(!sdr->load(fname.c_str(), type)) {
30                 fprintf(stderr, "Failed to load shader: %s.\n", fname.c_str());
31                 delete sdr;
32                 return 0;
33         }
34
35         add_shader(sdr, name);
36         return sdr;
37 }
38
39 void ShaderManager::delete_shaders()
40 {
41         std::map<std::string, Shader *>::iterator it;
42         for(it=shaders.begin(); it != shaders.end(); it++) {
43                 delete it->second;
44         }
45 }
46
47 ShaderProgram *ShaderManager::create_shader_program(const char *vname, const char *fname)
48 {
49         path = std::string(gfx_get_shader_path());
50
51         Shader *vsdr = load_shader(vname, SDR_VERTEX);
52         if(!vsdr)
53                 return 0;
54
55         Shader *fsdr = load_shader(fname, SDR_FRAGMENT);
56         if(!fsdr)
57                 return 0;
58
59         ShaderProgram *sprog = gfx_create_shader_program();
60
61         if(!sprog->create()) {
62                 return 0;
63         }
64
65         sprog->attach_shader(vsdr);
66         sprog->attach_shader(fsdr);
67
68         if(!sprog->link()) {
69                 delete sprog;
70                 return 0;
71         }
72
73         return sprog;
74 }
75
76 void ShaderManager::set_path(const char *path)
77 {
78         if(!path)
79                 this->path.clear();
80         else
81                 this->path = std::string(path);
82 }