create_shader_program.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if(geom_source != "")
  36. {
  37. // load vertex shader
  38. GLuint g = igl::opengl::load_shader(geom_source.c_str(),GL_GEOMETRY_SHADER_EXT);
  39. if(g == 0)
  40. {
  41. cerr<<"geometry shader failed to compile."<<endl;
  42. return false;
  43. }
  44. glAttachShader(id,g);
  45. }
  46. if(vert_source != "")
  47. {
  48. // load vertex shader
  49. GLuint v = igl::opengl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  50. if(v == 0)
  51. {
  52. cerr<<"vertex shader failed to compile."<<endl;
  53. return false;
  54. }
  55. glAttachShader(id,v);
  56. }
  57. if(frag_source != "")
  58. {
  59. // load fragment shader
  60. GLuint f = igl::opengl::load_shader(frag_source.c_str(),GL_FRAGMENT_SHADER);
  61. if(f == 0)
  62. {
  63. cerr<<"fragment shader failed to compile."<<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. // print log if any
  82. igl::opengl::print_program_info_log(id);
  83. return true;
  84. }
  85. IGL_INLINE bool igl::opengl::create_shader_program(
  86. const std::string & vert_source,
  87. const std::string & frag_source,
  88. const std::map<std::string,GLuint> & attrib,
  89. GLuint & prog_id)
  90. {
  91. return create_shader_program("",vert_source,frag_source,attrib,prog_id);
  92. }
  93. IGL_INLINE GLuint igl::opengl::create_shader_program(
  94. const std::string & geom_source,
  95. const std::string & vert_source,
  96. const std::string & frag_source,
  97. const std::map<std::string,GLuint> & attrib)
  98. {
  99. GLuint prog_id = 0;
  100. create_shader_program(geom_source,vert_source,frag_source,attrib,prog_id);
  101. return prog_id;
  102. }
  103. IGL_INLINE GLuint igl::opengl::create_shader_program(
  104. const std::string & vert_source,
  105. const std::string & frag_source,
  106. const std::map<std::string,GLuint> & attrib)
  107. {
  108. GLuint prog_id = 0;
  109. create_shader_program(vert_source,frag_source,attrib,prog_id);
  110. return prog_id;
  111. }
  112. #ifdef IGL_STATIC_LIBRARY
  113. // Explicit template specialization
  114. #endif