create_shader_program.cpp 3.3 KB

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