3D ski maps. WIP.

Program.cc 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Associated header file
  2. #include "Program.h"
  3. // System headers
  4. #include <fstream>
  5. #include <iostream>
  6. #include <sstream>
  7. Program::Program(
  8. std::string vertex_shader_source_file,
  9. std::string fragment_shader_source_file) {
  10. // Load vertex shader from file
  11. std::ifstream ifs(vertex_shader_source_file, std::ios::in);
  12. std::string vertex_shader_source(
  13. (std::istreambuf_iterator<char>(ifs)),
  14. std::istreambuf_iterator<char>());
  15. ifs.close();
  16. // Load fragment shader from file
  17. ifs.open(fragment_shader_source_file, std::ios::in);
  18. std::string fragment_shader_source(
  19. (std::istreambuf_iterator<char>(ifs)),
  20. std::istreambuf_iterator<char>());
  21. ifs.close();
  22. // Build the program
  23. build(
  24. vertex_shader_source.c_str(),
  25. fragment_shader_source.c_str());
  26. }
  27. GLuint Program::get_program_id(void) const {
  28. return program_id_;
  29. }
  30. GLuint Program::compile_shader(const GLenum type, const GLchar* source) {
  31. if (NULL == source) {
  32. // TODO[Error]
  33. throw 1;
  34. }
  35. // Create a shader object of the correct type
  36. GLuint shader_object_id = glCreateShader(type);
  37. if (0 == shader_object_id) {
  38. // TODO[Error]
  39. }
  40. // Compile the shader
  41. glShaderSource(shader_object_id, 1, &source, NULL);
  42. glCompileShader(shader_object_id);
  43. // Check the compilation status
  44. GLint compile_status;
  45. glGetShaderiv(shader_object_id, GL_COMPILE_STATUS, &compile_status);
  46. if (GL_FALSE == compile_status) {
  47. // TODO[Error]
  48. const int max_length = 1000;
  49. GLchar infoLog[max_length] = "";
  50. glGetShaderInfoLog(shader_object_id, max_length, NULL, infoLog);
  51. std::cerr << infoLog << std::endl;
  52. throw 1;
  53. }
  54. // Return the id of the compiled shader
  55. return shader_object_id;
  56. }
  57. GLuint Program::link(const GLuint vertex_shader, const GLuint fragment_shader) {
  58. // Create a new program
  59. GLuint program_object_id = glCreateProgram();
  60. if (0 == program_object_id) {
  61. // TODO[Error]
  62. }
  63. // Attach shaders to the program object
  64. glAttachShader(program_object_id, vertex_shader);
  65. glAttachShader(program_object_id, fragment_shader);
  66. glLinkProgram(program_object_id);
  67. // Check link status
  68. GLint link_status;
  69. glGetProgramiv(program_object_id, GL_LINK_STATUS, &link_status);
  70. if (GL_FALSE == link_status) {
  71. // TODO[Error]
  72. const int max_length = 1000;
  73. GLchar infoLog[max_length] = "";
  74. glGetProgramInfoLog(program_object_id, max_length, NULL, infoLog);
  75. std::cerr << infoLog << std::endl;
  76. throw 1;
  77. }
  78. // Return the id of the linked program
  79. return program_object_id;
  80. }
  81. void Program::build(
  82. const GLchar* vertex_shader_source,
  83. const GLchar* fragment_shader_source) {
  84. if ((NULL == vertex_shader_source) || (NULL == fragment_shader_source)) {
  85. // TODO[Error]
  86. throw 1;
  87. }
  88. // Compile vertex shader
  89. GLuint vertex_shader = compile_shader(
  90. GL_VERTEX_SHADER,
  91. vertex_shader_source);
  92. // Compile fragment shader
  93. GLuint fragment_shader = compile_shader(
  94. GL_FRAGMENT_SHADER,
  95. fragment_shader_source);
  96. // Link both of them and set the program id
  97. program_id_ = link(vertex_shader, fragment_shader);
  98. }