destroy_shader_program.h 1.4 KB

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