create_shader_program.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <iostream>
  13. #include <cstdio>
  14. IGL_INLINE bool igl::create_shader_program(
  15. const std::string & geom_source,
  16. const std::string & vert_source,
  17. const std::string & frag_source,
  18. const std::map<std::string,GLuint> & attrib,
  19. GLuint & id)
  20. {
  21. using namespace std;
  22. if(vert_source == "" && frag_source == "")
  23. {
  24. cerr<<
  25. "create_shader_program() could not create shader program,"
  26. " both .vert and .frag source given were empty"<<endl;
  27. return false;
  28. }
  29. // create program
  30. id = glCreateProgram();
  31. if(id == 0)
  32. {
  33. cerr<<"create_shader_program() could not create shader program."<<endl;
  34. return false;
  35. }
  36. if(geom_source != "")
  37. {
  38. // load vertex shader
  39. GLuint g = igl::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. GLuint v = igl::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. GLuint f = igl::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. // print log if any
  83. igl::print_program_info_log(id);
  84. return true;
  85. }
  86. IGL_INLINE bool igl::create_shader_program(
  87. const std::string & vert_source,
  88. const std::string & frag_source,
  89. const std::map<std::string,GLuint> & attrib,
  90. GLuint & prog_id)
  91. {
  92. return create_shader_program("",vert_source,frag_source,attrib,prog_id);
  93. }
  94. IGL_INLINE GLuint igl::create_shader_program(
  95. const std::string & geom_source,
  96. const std::string & vert_source,
  97. const std::string & frag_source,
  98. const std::map<std::string,GLuint> & attrib)
  99. {
  100. GLuint prog_id = 0;
  101. create_shader_program(geom_source,vert_source,frag_source,attrib,prog_id);
  102. return prog_id;
  103. }
  104. IGL_INLINE GLuint igl::create_shader_program(
  105. const std::string & vert_source,
  106. const std::string & frag_source,
  107. const std::map<std::string,GLuint> & attrib)
  108. {
  109. GLuint prog_id = 0;
  110. create_shader_program(vert_source,frag_source,attrib,prog_id);
  111. return prog_id;
  112. }
  113. #endif