create_shader_program.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "create_shader_program.h"
  2. #include "load_shader.h"
  3. #include "print_program_info_log.h"
  4. #include <cstdio>
  5. IGL_INLINE bool igl::create_shader_program(
  6. const std::string vert_source,
  7. const std::string frag_source,
  8. const std::map<std::string,GLuint> attrib,
  9. GLuint & id)
  10. {
  11. if(vert_source == "" && frag_source == "")
  12. {
  13. fprintf(
  14. stderr,
  15. "Error: create_shader_program() could not create shader program,"
  16. " both .vert and .frag source given were empty\n");
  17. return false;
  18. }
  19. // create program
  20. id = glCreateProgram();
  21. if(id == 0)
  22. {
  23. fprintf(
  24. stderr,
  25. "Error: create_shader_program() could not create shader program.\n");
  26. return false;
  27. }
  28. if(vert_source != "")
  29. {
  30. // load vertex shader
  31. GLuint v = igl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  32. if(v == 0)
  33. {
  34. return false;
  35. }
  36. glAttachShader(id,v);
  37. }
  38. if(frag_source != "")
  39. {
  40. // load fragment shader
  41. GLuint f = igl::load_shader(frag_source.c_str(),GL_FRAGMENT_SHADER);
  42. if(f == 0)
  43. {
  44. return false;
  45. }
  46. glAttachShader(id,f);
  47. }
  48. // loop over attributes
  49. for(
  50. std::map<std::string,GLuint>::const_iterator ait = attrib.begin();
  51. ait != attrib.end();
  52. ait++)
  53. {
  54. glBindAttribLocation(
  55. id,
  56. (*ait).second,
  57. (*ait).first.c_str());
  58. }
  59. // Link program
  60. glLinkProgram(id);
  61. // print log if any
  62. igl::print_program_info_log(id);
  63. return true;
  64. }