Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Program.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "graphics/Program.h"
  19. Program::Program() : handle_(0)
  20. {}
  21. Program::~Program()
  22. {
  23. destroy();
  24. }
  25. void Program::create()
  26. {
  27. destroy();
  28. handle_ = glCreateProgram();
  29. }
  30. void Program::attach(GLenum type, const GLchar* source)
  31. {
  32. GLint length = static_cast<GLint>(strlen(source));
  33. GLuint shader = glCreateShader(type);
  34. glShaderSource(shader, 1, &source, &length);
  35. glCompileShader(shader);
  36. GLint success = GL_FALSE;
  37. glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  38. if(!success)
  39. {
  40. GLint logLength;
  41. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  42. vector<GLchar> log(logLength);
  43. glGetShaderInfoLog(shader, logLength, &logLength, log.data());
  44. string err("Failed to compile shader: ");
  45. err.append(log.data(), logLength);
  46. throw runtime_error(err);
  47. }
  48. glAttachShader(handle_, shader);
  49. glDeleteShader(shader);
  50. }
  51. void Program::link()
  52. {
  53. glLinkProgram(handle_);
  54. GLint success = GL_FALSE;
  55. glGetProgramiv(handle_, GL_LINK_STATUS, &success);
  56. if(!success)
  57. {
  58. GLint logLength;
  59. glGetProgramiv(handle_, GL_INFO_LOG_LENGTH, &logLength);
  60. vector<GLchar> log(logLength);
  61. glGetProgramInfoLog(handle_, logLength, &logLength, log.data());
  62. string err("Failed to link program: ");
  63. err.append(log.data(), logLength);
  64. throw runtime_error(err);
  65. }
  66. }
  67. void Program::use()
  68. {
  69. assert(handle_ != 0);
  70. glUseProgram(handle_);
  71. }
  72. GLint Program::getUniform(const GLchar* name)
  73. {
  74. assert(handle_ != 0);
  75. auto it = uniforms_.find(name);
  76. if(it == uniforms_.end())
  77. {
  78. it = uniforms_.insert({name, glGetUniformLocation(handle_, name)}).first;
  79. }
  80. if(it->second < 0)
  81. {
  82. //string err("Uniform does not exist: ");
  83. //err.append(name);
  84. //throw runtime_error(err);
  85. //puts(err.c_str());
  86. }
  87. return it->second;
  88. }
  89. void Program::destroy()
  90. {
  91. if(handle_ != 0)
  92. {
  93. glDeleteProgram(handle_);
  94. handle_ = 0;
  95. }
  96. }