3dphotoshoot
diff src/shader.cc @ 21:4ca4e3c5a754
port to C++ completed, shader programs now use the SdrProg class
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 11 Jun 2015 04:56:33 +0300 |
parents | c14613d27a3a |
children | d7fe157c402d |
line diff
1.1 --- a/src/shader.cc Thu Jun 11 02:53:43 2015 +0300 1.2 +++ b/src/shader.cc Thu Jun 11 04:56:33 2015 +0300 1.3 @@ -7,6 +7,8 @@ 1.4 1.5 std::map<std::string, unsigned int> sdrdb; 1.6 1.7 +const SdrProg *SdrProg::active; 1.8 + 1.9 SdrProg::SdrProg() 1.10 { 1.11 prog = 0; 1.12 @@ -59,10 +61,8 @@ 1.13 return false; 1.14 } 1.15 1.16 - if(!link()) { 1.17 - return false; 1.18 - } 1.19 - return true; 1.20 + bind_default_attribs(); 1.21 + return link(); 1.22 } 1.23 1.24 bool SdrProg::create(const char *vsrc, const char *psrc) 1.25 @@ -91,29 +91,32 @@ 1.26 1.27 bool SdrProg::load(const char *vfname, const char *pfname) 1.28 { 1.29 - unsigned int vs = load_vertex_shader(vfname); 1.30 + unsigned int vs = get_shader(vfname, GL_VERTEX_SHADER); 1.31 if(!vs) { 1.32 return false; 1.33 } 1.34 - sdrdb[vfname] = vs; 1.35 1.36 - unsigned int ps = load_pixel_shader(pfname); 1.37 + unsigned int ps = get_shader(pfname, GL_FRAGMENT_SHADER); 1.38 if(!ps) { 1.39 return false; 1.40 } 1.41 - sdrdb[pfname] = ps; 1.42 1.43 printf("creating shader program (%s, %s)\n", vfname, pfname); 1.44 if(!(prog = create_program_link(vs, ps, 0))) { 1.45 return false; 1.46 } 1.47 - valid = true; 1.48 - return true; 1.49 + 1.50 + bind_default_attribs(); 1.51 + return link(); 1.52 } 1.53 1.54 bool SdrProg::link() const 1.55 { 1.56 - return link_program(prog) != -1; 1.57 + if(link_program(prog) == -1) { 1.58 + return false; 1.59 + } 1.60 + valid = true; 1.61 + return true; 1.62 } 1.63 1.64 int SdrProg::get_uniform(const char *name) const 1.65 @@ -149,6 +152,24 @@ 1.66 return true; 1.67 } 1.68 1.69 +void SdrProg::bind_default_attribs() const 1.70 +{ 1.71 + // XXX must be in sync with SdrDefaultAttrib enums in shader.h 1.72 + static const char *def_names[] = { 1.73 + "attr_vertex", 1.74 + "attr_normal", 1.75 + "attr_texcoord", 1.76 + "attr_color", 1.77 + "attr_tangent", 1.78 + 0 1.79 + }; 1.80 + 1.81 + for(int i=0; def_names[i]; i++) { 1.82 + bind_attrib(def_names[i], i); 1.83 + } 1.84 +} 1.85 + 1.86 + 1.87 bool SdrProg::bind() const 1.88 { 1.89 if(!prog || (!valid && !link())) { 1.90 @@ -161,10 +182,11 @@ 1.91 fprintf(stderr, "failed to bind program %d\n", prog); 1.92 return false; 1.93 } 1.94 + 1.95 + active = this; 1.96 return true; 1.97 } 1.98 1.99 - 1.100 unsigned int get_shader(const char *name, unsigned int type) 1.101 { 1.102 std::map<std::string, unsigned int>::const_iterator it = sdrdb.find(name); 1.103 @@ -180,3 +202,13 @@ 1.104 sdrdb[name] = sdr; 1.105 return sdr; 1.106 } 1.107 + 1.108 +SdrProg *get_sdrprog(const char *vfile, const char *pfile) 1.109 +{ 1.110 + SdrProg *sp = new SdrProg; 1.111 + if(!sp->load(vfile, pfile)) { 1.112 + delete sp; 1.113 + return 0; 1.114 + } 1.115 + return sp; 1.116 +}