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

163 lines
4.2 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. #include "rasterizer/rasterizer.hpp"
  20. #include "rasterizer/framebuffer.hpp"
  21. #include "rasterizer/shader-program.hpp"
  22. #include "rasterizer/vertex-array.hpp"
  23. #include <glad/glad.h>
  24. static constexpr GLenum drawing_mode_lut[] =
  25. {
  26. GL_POINTS,
  27. GL_LINE_STRIP,
  28. GL_LINE_LOOP,
  29. GL_LINES,
  30. GL_LINE_STRIP_ADJACENCY,
  31. GL_LINES_ADJACENCY,
  32. GL_TRIANGLE_STRIP,
  33. GL_TRIANGLE_FAN,
  34. GL_TRIANGLES,
  35. GL_TRIANGLE_STRIP_ADJACENCY,
  36. GL_TRIANGLES_ADJACENCY
  37. };
  38. static constexpr GLenum element_array_type_lut[] =
  39. {
  40. GL_UNSIGNED_BYTE,
  41. GL_UNSIGNED_SHORT,
  42. GL_UNSIGNED_INT
  43. };
  44. rasterizer::rasterizer():
  45. bound_vao(nullptr),
  46. bound_shader_program(nullptr)
  47. {
  48. // Determine dimensions of default framebuffer
  49. GLint scissor_box[4] = {0, 0, 0, 0};
  50. glGetIntegerv(GL_SCISSOR_BOX, scissor_box);
  51. // Setup default framebuffer
  52. default_framebuffer = new framebuffer();
  53. default_framebuffer->gl_framebuffer_id = 0;
  54. default_framebuffer->dimensions = {scissor_box[2], scissor_box[3]};
  55. // Bind default framebuffer
  56. bound_framebuffer = default_framebuffer;
  57. }
  58. rasterizer::~rasterizer()
  59. {
  60. delete default_framebuffer;
  61. }
  62. void rasterizer::context_resized(int width, int height)
  63. {
  64. default_framebuffer->dimensions = {width, height};
  65. }
  66. void rasterizer::use_framebuffer(const ::framebuffer& framebuffer)
  67. {
  68. if (bound_framebuffer != &framebuffer)
  69. {
  70. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.gl_framebuffer_id);
  71. bound_framebuffer = &framebuffer;
  72. }
  73. }
  74. void rasterizer::set_clear_color(float r, float g, float b, float a)
  75. {
  76. glClearColor(r, g, b, a);
  77. }
  78. void rasterizer::set_clear_depth(float depth)
  79. {
  80. glClearDepth(depth);
  81. }
  82. void rasterizer::set_clear_stencil(int s)
  83. {
  84. glClearStencil(s);
  85. }
  86. void rasterizer::clear_framebuffer(bool color, bool depth, bool stencil)
  87. {
  88. GLbitfield mask = 0;
  89. if (color) mask |= GL_COLOR_BUFFER_BIT;
  90. if (depth) mask |= GL_DEPTH_BUFFER_BIT;
  91. if (stencil) mask |= GL_STENCIL_BUFFER_BIT;
  92. glClear(mask);
  93. }
  94. void rasterizer::set_viewport(int x, int y, int width, int height)
  95. {
  96. glViewport(x, y, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
  97. }
  98. void rasterizer::use_program(const shader_program& program)
  99. {
  100. if (bound_shader_program != &program)
  101. {
  102. glUseProgram(program.gl_program_id);
  103. bound_shader_program = &program;
  104. }
  105. }
  106. void rasterizer::draw_arrays(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count)
  107. {
  108. GLenum gl_mode = drawing_mode_lut[static_cast<std::size_t>(mode)];
  109. if (bound_vao != &vao)
  110. {
  111. glBindVertexArray(vao.gl_array_id);
  112. bound_vao = &vao;
  113. }
  114. glDrawArrays(gl_mode, static_cast<GLint>(offset), static_cast<GLsizei>(count));
  115. }
  116. void rasterizer::draw_arrays_instanced(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count, std::size_t instance_count)
  117. {
  118. GLenum gl_mode = drawing_mode_lut[static_cast<std::size_t>(mode)];
  119. if (bound_vao != &vao)
  120. {
  121. glBindVertexArray(vao.gl_array_id);
  122. bound_vao = &vao;
  123. }
  124. glDrawArraysInstanced(gl_mode, static_cast<GLint>(offset), static_cast<GLsizei>(count), static_cast<GLsizei>(instance_count));
  125. }
  126. void rasterizer::draw_elements(const vertex_array& vao, drawing_mode mode, std::size_t offset, std::size_t count, element_array_type type)
  127. {
  128. GLenum gl_mode = drawing_mode_lut[static_cast<std::size_t>(mode)];
  129. GLenum gl_type = element_array_type_lut[static_cast<std::size_t>(type)];
  130. if (bound_vao != &vao)
  131. {
  132. glBindVertexArray(vao.gl_array_id);
  133. bound_vao = &vao;
  134. }
  135. glDrawElements(gl_mode, static_cast<GLsizei>(count), gl_type, (const GLvoid*)offset);
  136. }