shader_program_uniforms_map.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef IGL_SHADER_PROGRAM_UNIFORMS_MAP_H
  2. #define IGL_SHADER_PROGRAM_UNIFORMS_MAP_H
  3. #include <string>
  4. #include <map>
  5. #ifdef __APPLE__
  6. # include <OpenGL/gl.h>
  7. #else
  8. # include <GL/gl.h>
  9. #endif
  10. namespace igl
  11. {
  12. // Builds a map of *active* uniform names as strings to their respective
  13. // locations as GLuint.
  14. // Input:
  15. // id index id of the program to query
  16. // Output:
  17. // uniforms map of names to locations
  18. // Returns true on success, false on errors
  19. void shader_program_uniforms_map(
  20. const GLuint id,
  21. std::map<std::string,GLint> & uniforms);
  22. }
  23. // Implementation
  24. #include "verbose.h"
  25. #include "report_gl_error.h"
  26. void igl::shader_program_uniforms_map(
  27. const GLuint id,
  28. std::map<std::string,GLint> & uniforms)
  29. {
  30. // empty the map of previous contents
  31. uniforms.clear();
  32. // get number of active uniforms
  33. GLint n = 200;
  34. glGetProgramiv(id,GL_ACTIVE_UNIFORMS,&n);
  35. // get max uniform name length
  36. GLint max_name_length;
  37. glGetProgramiv(id,GL_ACTIVE_UNIFORM_MAX_LENGTH,&max_name_length);
  38. // buffer for name
  39. GLchar * name = new GLchar[max_name_length];
  40. // buffer for length
  41. GLsizei length = 100;
  42. GLenum type;
  43. GLint size;
  44. // loop over active uniforms getting each's name
  45. for(GLuint u = 0;u < n;u++)
  46. {
  47. // I have no idea why glGetActiveUniformName doesn't work but
  48. // glGetActiveUniform does...
  49. //glGetActiveUniformName(id,u,max_name_length,&length,name);
  50. glGetActiveUniform(id,u,max_name_length,&length,&size,&type,name);
  51. // insert into map
  52. uniforms[string(name)] = u;
  53. }
  54. delete[] name;
  55. }
  56. #endif