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

78 lines
2.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. #include "rasterizer/vertex-buffer.hpp"
  20. #include <glad/glad.h>
  21. static constexpr GLenum buffer_usage_lut[] =
  22. {
  23. GL_STREAM_DRAW,
  24. GL_STREAM_READ,
  25. GL_STREAM_COPY,
  26. GL_STATIC_DRAW,
  27. GL_STATIC_READ,
  28. GL_STATIC_COPY,
  29. GL_DYNAMIC_DRAW,
  30. GL_DYNAMIC_READ,
  31. GL_DYNAMIC_COPY
  32. };
  33. vertex_buffer::vertex_buffer(std::size_t size, const void* data, buffer_usage usage):
  34. gl_buffer_id(0),
  35. size(size),
  36. usage(usage)
  37. {
  38. GLenum gl_usage = buffer_usage_lut[static_cast<std::size_t>(usage)];
  39. glGenBuffers(1, &gl_buffer_id);
  40. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  41. glBufferData(GL_ARRAY_BUFFER, size, data, gl_usage);
  42. }
  43. vertex_buffer::vertex_buffer():
  44. vertex_buffer(0, nullptr, buffer_usage::static_draw)
  45. {}
  46. vertex_buffer::~vertex_buffer()
  47. {
  48. glDeleteBuffers(1, &gl_buffer_id);
  49. }
  50. void vertex_buffer::repurpose(std::size_t size, const void* data, buffer_usage usage)
  51. {
  52. this->size = size;
  53. this->usage = usage;
  54. GLenum gl_usage = buffer_usage_lut[static_cast<std::size_t>(usage)];
  55. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  56. glBufferData(GL_ARRAY_BUFFER, size, data, gl_usage);
  57. }
  58. void vertex_buffer::resize(std::size_t size, const void* data)
  59. {
  60. repurpose(size, data, usage);
  61. }
  62. void vertex_buffer::update(int offset, std::size_t size, const void* data)
  63. {
  64. glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id);
  65. glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
  66. }