create_shader_program.cpp 1.5 KB

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