You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loadShader.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: loadShader.cpp
  5. *
  6. * Description:
  7. *
  8. * Version: 1.0
  9. * Created: 03/13/2014 03:28:10 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "loadShader.h"
  19. #include <GL/glew.h>
  20. #include <GL/gl.h>
  21. #include <string>
  22. #include <fstream>
  23. #include <vector>
  24. #include <algorithm>
  25. GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path){
  26. // Create the shaders
  27. GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  28. GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  29. // Read the Vertex Shader code from the file
  30. std::string VertexShaderCode;
  31. std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  32. if(VertexShaderStream.is_open())
  33. {
  34. std::string Line = "";
  35. while(getline(VertexShaderStream, Line))
  36. VertexShaderCode += "\n" + Line;
  37. VertexShaderStream.close();
  38. }
  39. // Read the Fragment Shader code from the file
  40. std::string FragmentShaderCode;
  41. std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  42. if(FragmentShaderStream.is_open()){
  43. std::string Line = "";
  44. while(getline(FragmentShaderStream, Line))
  45. FragmentShaderCode += "\n" + Line;
  46. FragmentShaderStream.close();
  47. }
  48. GLint Result = GL_FALSE;
  49. int InfoLogLength;
  50. // Compile Vertex Shader
  51. printf("Compiling shader : %s\n", vertex_file_path);
  52. char const * VertexSourcePointer = VertexShaderCode.c_str();
  53. glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
  54. glCompileShader(VertexShaderID);
  55. // Check Vertex Shader
  56. glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  57. glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  58. std::vector<char> VertexShaderErrorMessage(InfoLogLength);
  59. glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  60. fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
  61. // Compile Fragment Shader
  62. printf("Compiling shader : %s\n", fragment_file_path);
  63. char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  64. glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  65. glCompileShader(FragmentShaderID);
  66. // Check Fragment Shader
  67. glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  68. glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  69. std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
  70. glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  71. fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
  72. // Link the program
  73. fprintf(stdout, "Linking program\n");
  74. GLuint ProgramID = glCreateProgram();
  75. glAttachShader(ProgramID, VertexShaderID);
  76. glAttachShader(ProgramID, FragmentShaderID);
  77. glLinkProgram(ProgramID);
  78. // Check the program
  79. glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  80. glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  81. std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) );
  82. glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  83. fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
  84. glDeleteShader(VertexShaderID);
  85. glDeleteShader(FragmentShaderID);
  86. return ProgramID;
  87. }