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

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