destroy_shader_program.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef IGL_DESTROY_SHADER_PROGRAM_H
  2. #define IGL_DESTROY_SHADER_PROGRAM_H
  3. #ifdef __APPLE__
  4. # include <OpenGL/gl.h>
  5. #else
  6. # ifdef _WIN32
  7. # define NOMINMAX
  8. # include <Windows.h>
  9. # undef NOMINMAX
  10. # endif
  11. # include <GL/gl.h>
  12. #endif
  13. namespace igl
  14. {
  15. // Properly destroy a shader program. Detach and delete each of its shaders
  16. // and delete it
  17. // Inputs:
  18. // id index id of created shader, set to 0 on error
  19. // Returns true on success, false on error
  20. //
  21. // Note: caller is responsible for making sure he doesn't foolishly continue
  22. // to use id as if it still contains a program
  23. //
  24. // See also: create_shader_program
  25. inline bool destroy_shader_program(const GLuint id);
  26. }
  27. // Implementation
  28. inline bool igl::destroy_shader_program(const GLuint id)
  29. {
  30. // Don't try to destroy id == 0 (no shader program)
  31. if(id == 0)
  32. {
  33. fprintf(stderr,"Error: destroy_shader_program() id = %d"
  34. " but must should be positive\n",id);
  35. return false;
  36. }
  37. // Get each attached shader one by one and detach and delete it
  38. GLsizei count;
  39. // shader id
  40. GLuint s;
  41. do
  42. {
  43. // Try to get at most *1* attached shader
  44. glGetAttachedShaders(id,1,&count,&s);
  45. // Check that we actually got *1*
  46. if(count == 1)
  47. {
  48. // Detach and delete this shader
  49. glDetachShader(id,s);
  50. glDeleteShader(s);
  51. }
  52. }while(count > 0);
  53. // Now that all of the shaders are gone we can just delete the program
  54. glDeleteProgram(id);
  55. return true;
  56. }
  57. #endif