💿🐜 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.

142 lines
4.1 KiB

  1. /*
  2. * Copyright (C) 2023 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_RASTERIZER_HPP
  20. #define ANTKEEPER_GL_RASTERIZER_HPP
  21. #include <engine/gl/drawing-mode.hpp>
  22. #include <engine/gl/element-array-type.hpp>
  23. #include <cstddef>
  24. #include <memory>
  25. namespace gl {
  26. class framebuffer;
  27. class vertex_array;
  28. class shader_program;
  29. /**
  30. * Interface to the OpenGL state and drawing functions.
  31. */
  32. class rasterizer
  33. {
  34. public:
  35. /**
  36. * Creates a rasterizer. Warning: This must be called after an OpenGL context has been created.
  37. */
  38. rasterizer();
  39. /// Destroys a rasterizer.
  40. ~rasterizer();
  41. /**
  42. * This should be called when the window associated with the OpenGL context is resized, and will effectively changed the reported dimensions of the default framebuffer.
  43. */
  44. void context_resized(int width, int height);
  45. /**
  46. * Sets the active framebuffer.
  47. *
  48. * @param framebuffer Framebuffer to use.
  49. */
  50. void use_framebuffer(const framebuffer& framebuffer);
  51. /**
  52. * Sets the color to be used when the color buffer of a framebuffer is cleared.
  53. *
  54. * @param r Red color component.
  55. * @param g Green color component.
  56. * @param b Blue color component.
  57. * @param a Alpha color component.
  58. */
  59. void set_clear_color(float r, float g, float b, float a);
  60. /**
  61. * Sets the depth value to be used when the depth buffer of a framebuffer is cleared.
  62. *
  63. * @param depth Depth value.
  64. */
  65. void set_clear_depth(float depth);
  66. /**
  67. * Sets the stencil value to be used when the stencil buffer of a framebuffer is cleared.
  68. *
  69. * @param s Stencil value.
  70. */
  71. void set_clear_stencil(int s);
  72. /**
  73. * Clears the buffers attached to a framebuffer.
  74. *
  75. * @param color Specifies whether the color buffer should be cleared.
  76. * @param depth Specifies whether the depth buffer should be cleared.
  77. * @param stencil Specifies whether the stencil buffer should be cleared.
  78. */
  79. void clear_framebuffer(bool color, bool depth, bool stencil);
  80. /**
  81. * Sets the active viewport.
  82. *
  83. * @param x X-coordinate of the viewport.
  84. * @param y Y-coordinate of the viewport.
  85. * @param width Width of the viewport.
  86. * @param height Height of the viewport.
  87. */
  88. void set_viewport(int x, int y, int width, int height);
  89. /**
  90. * Binds a shader program.
  91. *
  92. * @param program Shader program to bind.
  93. */
  94. void use_program(const shader_program& program);
  95. /**
  96. *
  97. */
  98. void draw_arrays(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count);
  99. void draw_arrays(drawing_mode mode, std::size_t offset, std::size_t count);
  100. void draw_arrays_instanced(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count, std::size_t instance_count);
  101. /**
  102. *
  103. */
  104. void draw_elements(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count, element_array_type type);
  105. void draw_elements(drawing_mode mode, std::size_t offset, std::size_t count, element_array_type type);
  106. /**
  107. * Returns the default framebuffer associated with the OpenGL context of a window.
  108. */
  109. [[nodiscard]] inline const framebuffer& get_default_framebuffer() const noexcept
  110. {
  111. return *default_framebuffer;
  112. }
  113. private:
  114. std::unique_ptr<framebuffer> default_framebuffer;
  115. std::unique_ptr<vertex_array> dummy_vao;
  116. const framebuffer* bound_framebuffer{nullptr};
  117. const vertex_array* bound_vao{nullptr};
  118. const shader_program* bound_shader_program{nullptr};
  119. };
  120. } // namespace gl
  121. #endif // ANTKEEPER_GL_RASTERIZER_HPP