create_shader_program.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef IGL_CREATE_SHADER_PROGRAM_H
  2. #define IGL_CREATE_SHADER_PROGRAM_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. // Create a shader program with a vertex and fragments shader loading from
  13. // source strings and vertex attributes assigned from a map before linking the
  14. // shaders to the program, making it ready to use with glUseProgram(id)
  15. // Inputs:
  16. // vert_source string containing source code of vertex shader
  17. // frag_source string containing source code of fragment shader
  18. // attrib map containing table of vertex attribute strings add their
  19. // correspondingly ids (generated previously using glBindAttribLocation)
  20. // Outputs:
  21. // id index id of created shader, set to 0 on error
  22. // Returns true on success, false on error
  23. //
  24. // Note: Caller is responsible for making sure that current value of id is not
  25. // leaking a shader (since it will be overwritten)
  26. //
  27. // See also: destroy_shader_program
  28. inline bool create_shader_program(
  29. const std::string vert_source,
  30. const std::string frag_source,
  31. const std::map<std::string,GLuint> attrib,
  32. GLuint & id);
  33. }
  34. // Implementation
  35. #include "load_shader.h"
  36. #include "print_program_info_log.h"
  37. #include <cstdio>
  38. inline bool igl::create_shader_program(
  39. const std::string vert_source,
  40. const std::string frag_source,
  41. const std::map<std::string,GLuint> attrib,
  42. GLuint & id)
  43. {
  44. if(vert_source == "" && frag_source == "")
  45. {
  46. fprintf(
  47. stderr,
  48. "Error: create_shader_program() could not create shader program,"
  49. " both .vert and .frag source given were empty\n");
  50. return false;
  51. }
  52. // create program
  53. id = glCreateProgram();
  54. if(id == 0)
  55. {
  56. fprintf(
  57. stderr,
  58. "Error: create_shader_program() could not create shader program.\n");
  59. return false;
  60. }
  61. if(vert_source != "")
  62. {
  63. // load vertex shader
  64. GLuint v = igl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  65. if(v == 0)
  66. {
  67. return false;
  68. }
  69. glAttachShader(id,v);
  70. }
  71. if(frag_source != "")
  72. {
  73. // load fragment shader
  74. GLuint f = igl::load_shader(frag_source.c_str(),GL_FRAGMENT_SHADER);
  75. if(f == 0)
  76. {
  77. return false;
  78. }
  79. glAttachShader(id,f);
  80. }
  81. // loop over attributes
  82. for(
  83. std::map<std::string,GLuint>::const_iterator ait = attrib.begin();
  84. ait != attrib.end();
  85. ait++)
  86. {
  87. glBindAttribLocation(
  88. id,
  89. (*ait).second,
  90. (*ait).first.c_str());
  91. }
  92. // Link program
  93. glLinkProgram(id);
  94. // print log if any
  95. igl::print_program_info_log(id);
  96. return true;
  97. }
  98. #endif