💿🐜 Antkeeper source code https://antkeeper.com
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.

114 lines
3.1 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_GL_SHADER_OBJECT_HPP
  20. #define ANTKEEPER_GL_SHADER_OBJECT_HPP
  21. #include "gl/shader-stage.hpp"
  22. #include <cstdlib>
  23. #include <string>
  24. namespace gl {
  25. class shader_program;
  26. /**
  27. * Shader object which can be compiled and linked to a shader program.
  28. *
  29. * @see gl::shader_program
  30. * @see gl::shader_stage
  31. */
  32. class shader_object
  33. {
  34. public:
  35. /**
  36. * Creates an empty shader object for the specified shader stage.
  37. *
  38. * @param stage Shader stage in which this shader object will be used.
  39. *
  40. * @exception std::runtime_error An error occurred while creating an OpenGL shader object.
  41. */
  42. shader_object(shader_stage stage);
  43. /**
  44. * Destroys a shader object.
  45. */
  46. ~shader_object();
  47. /**
  48. * Replaces the source code of the shader object.
  49. *
  50. * @param source_code String containing shader object source code.
  51. *
  52. * @exception std::runtime_error Shader object handle is not a value generated by OpenGL.
  53. * @exception std::runtime_error Shader object handle is not a shader object.
  54. */
  55. void source(const std::string& source_code);
  56. /**
  57. * Compiles the shader object.
  58. *
  59. * @return `true` if the shader object was compiled successfully, `false` otherwise. If compilation fails, check the info log via shader_object::get_info_log() for more information.
  60. *
  61. * @exception std::runtime_error Shader object handle is not a value generated by OpenGL.
  62. * @exception std::runtime_error Shader object handle is not a shader object.
  63. *
  64. * @see shader_object::get_info_log()
  65. */
  66. bool compile();
  67. /// Returns the shader stage of this shader object.
  68. shader_stage get_stage() const;
  69. /// Returns the shader object info log, which is updated when the shader object is compiled.
  70. const std::string& get_info_log() const;
  71. /// Returns `true` if the shader object has been successfully compiled, `false` otherwise.
  72. bool was_compiled() const;
  73. shader_object(const shader_object&) = delete;
  74. shader_object& operator=(const shader_object&) = delete;
  75. private:
  76. friend class shader_program;
  77. unsigned int gl_shader_id;
  78. shader_stage stage;
  79. std::string info_log;
  80. bool compiled;
  81. };
  82. inline shader_stage shader_object::get_stage() const
  83. {
  84. return stage;
  85. }
  86. inline const std::string& shader_object::get_info_log() const
  87. {
  88. return info_log;
  89. }
  90. inline bool shader_object::was_compiled() const
  91. {
  92. return compiled;
  93. }
  94. } // namespace gl
  95. #endif // ANTKEEPER_GL_SHADER_OBJECT_HPP