create_shader_program.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "create_shader_program.h"
  9. #ifndef IGL_NO_OPENGL
  10. #include "load_shader.h"
  11. #include "print_program_info_log.h"
  12. #include <cstdio>
  13. IGL_INLINE bool igl::create_shader_program(
  14. const std::string & vert_source,
  15. const std::string & frag_source,
  16. const std::map<std::string,GLuint> & attrib,
  17. GLuint & id)
  18. {
  19. if(vert_source == "" && frag_source == "")
  20. {
  21. fprintf(
  22. stderr,
  23. "Error: create_shader_program() could not create shader program,"
  24. " both .vert and .frag source given were empty\n");
  25. return false;
  26. }
  27. // create program
  28. id = glCreateProgram();
  29. if(id == 0)
  30. {
  31. fprintf(
  32. stderr,
  33. "Error: create_shader_program() could not create shader program.\n");
  34. return false;
  35. }
  36. if(vert_source != "")
  37. {
  38. // load vertex shader
  39. GLuint v = igl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  40. if(v == 0)
  41. {
  42. return false;
  43. }
  44. glAttachShader(id,v);
  45. }
  46. if(frag_source != "")
  47. {
  48. // load fragment shader
  49. GLuint f = igl::load_shader(frag_source.c_str(),GL_FRAGMENT_SHADER);
  50. if(f == 0)
  51. {
  52. return false;
  53. }
  54. glAttachShader(id,f);
  55. }
  56. // loop over attributes
  57. for(
  58. std::map<std::string,GLuint>::const_iterator ait = attrib.begin();
  59. ait != attrib.end();
  60. ait++)
  61. {
  62. glBindAttribLocation(
  63. id,
  64. (*ait).second,
  65. (*ait).first.c_str());
  66. }
  67. // Link program
  68. glLinkProgram(id);
  69. // print log if any
  70. igl::print_program_info_log(id);
  71. return true;
  72. }
  73. IGL_INLINE GLuint igl::create_shader_program(
  74. const std::string & vert_source,
  75. const std::string & frag_source,
  76. const std::map<std::string,GLuint> & attrib)
  77. {
  78. GLuint prog_id = 0;
  79. create_shader_program(vert_source,frag_source,attrib,prog_id);
  80. return prog_id;
  81. }
  82. #endif