create_shader_program.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. #include "load_shader.h"
  10. #include "print_program_info_log.h"
  11. #include <iostream>
  12. #include <cstdio>
  13. IGL_INLINE bool igl::opengl::create_shader_program(
  14. const std::string & geom_source,
  15. const std::string & vert_source,
  16. const std::string & frag_source,
  17. const std::map<std::string,GLuint> & attrib,
  18. GLuint & id)
  19. {
  20. if(vert_source == "" && frag_source == "")
  21. {
  22. std::cerr<<
  23. "create_shader_program() could not create shader program,"
  24. " both .vert and .frag source given were empty"<<std::endl;
  25. return false;
  26. }
  27. // create program
  28. id = glCreateProgram();
  29. if(id == 0)
  30. {
  31. std::cerr<<"create_shader_program() could not create shader program."<<std::endl;
  32. return false;
  33. }
  34. GLuint g = 0,f = 0,v = 0;
  35. if(geom_source != "")
  36. {
  37. // load vertex shader
  38. g = igl::opengl::load_shader(geom_source.c_str(),GL_GEOMETRY_SHADER);
  39. if(g == 0)
  40. {
  41. std::cerr<<"geometry shader failed to compile."<<std::endl;
  42. return false;
  43. }
  44. glAttachShader(id,g);
  45. }
  46. if(vert_source != "")
  47. {
  48. // load vertex shader
  49. v = igl::opengl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  50. if(v == 0)
  51. {
  52. std::cerr<<"vertex shader failed to compile."<<std::endl;
  53. return false;
  54. }
  55. glAttachShader(id,v);
  56. }
  57. if(frag_source != "")
  58. {
  59. // load fragment shader
  60. f = igl::opengl::load_shader(frag_source.c_str(),GL_FRAGMENT_SHADER);
  61. if(f == 0)
  62. {
  63. std::cerr<<"fragment shader failed to compile."<<std::endl;
  64. return false;
  65. }
  66. glAttachShader(id,f);
  67. }
  68. // loop over attributes
  69. for(
  70. std::map<std::string,GLuint>::const_iterator ait = attrib.begin();
  71. ait != attrib.end();
  72. ait++)
  73. {
  74. glBindAttribLocation(
  75. id,
  76. (*ait).second,
  77. (*ait).first.c_str());
  78. }
  79. // Link program
  80. glLinkProgram(id);
  81. const auto & detach = [&id](const GLuint shader)
  82. {
  83. if(shader)
  84. {
  85. glDetachShader(id,shader);
  86. glDeleteShader(shader);
  87. }
  88. };
  89. detach(g);
  90. detach(f);
  91. detach(v);
  92. // print log if any
  93. igl::opengl::print_program_info_log(id);
  94. return true;
  95. }
  96. IGL_INLINE bool igl::opengl::create_shader_program(
  97. const std::string & vert_source,
  98. const std::string & frag_source,
  99. const std::map<std::string,GLuint> & attrib,
  100. GLuint & prog_id)
  101. {
  102. return create_shader_program("",vert_source,frag_source,attrib,prog_id);
  103. }
  104. IGL_INLINE GLuint igl::opengl::create_shader_program(
  105. const std::string & geom_source,
  106. const std::string & vert_source,
  107. const std::string & frag_source,
  108. const std::map<std::string,GLuint> & attrib)
  109. {
  110. GLuint prog_id = 0;
  111. create_shader_program(geom_source,vert_source,frag_source,attrib,prog_id);
  112. return prog_id;
  113. }
  114. IGL_INLINE GLuint igl::opengl::create_shader_program(
  115. const std::string & vert_source,
  116. const std::string & frag_source,
  117. const std::map<std::string,GLuint> & attrib)
  118. {
  119. GLuint prog_id = 0;
  120. create_shader_program(vert_source,frag_source,attrib,prog_id);
  121. return prog_id;
  122. }
  123. #ifdef IGL_STATIC_LIBRARY
  124. // Explicit template instantiation
  125. #endif