create_shader_program.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 & 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(vert_source != "")
  36. {
  37. // load vertex shader
  38. GLuint v = igl::load_shader(vert_source.c_str(),GL_VERTEX_SHADER);
  39. if(v == 0)
  40. {
  41. cerr<<"vertex shader failed to compile."<<endl;
  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. cerr<<"fragment shader failed to compile."<<endl;
  53. return false;
  54. }
  55. glAttachShader(id,f);
  56. }
  57. // loop over attributes
  58. for(
  59. std::map<std::string,GLuint>::const_iterator ait = attrib.begin();
  60. ait != attrib.end();
  61. ait++)
  62. {
  63. glBindAttribLocation(
  64. id,
  65. (*ait).second,
  66. (*ait).first.c_str());
  67. }
  68. // Link program
  69. glLinkProgram(id);
  70. // print log if any
  71. igl::print_program_info_log(id);
  72. return true;
  73. }
  74. IGL_INLINE GLuint igl::create_shader_program(
  75. const std::string & vert_source,
  76. const std::string & frag_source,
  77. const std::map<std::string,GLuint> & attrib)
  78. {
  79. GLuint prog_id = 0;
  80. create_shader_program(vert_source,frag_source,attrib,prog_id);
  81. return prog_id;
  82. }
  83. #endif