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

66 lines
1.8 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-array.hpp"
  20. #include "gl/vertex-buffer.hpp"
  21. #include <glad/glad.h>
  22. namespace gl {
  23. static constexpr GLenum vertex_attribute_type_lut[] =
  24. {
  25. GL_BYTE,
  26. GL_UNSIGNED_BYTE,
  27. GL_SHORT,
  28. GL_UNSIGNED_SHORT,
  29. GL_INT,
  30. GL_UNSIGNED_INT,
  31. GL_HALF_FLOAT,
  32. GL_FLOAT,
  33. GL_DOUBLE
  34. };
  35. vertex_array::vertex_array():
  36. gl_array_id(0)
  37. {
  38. glGenVertexArrays(1, &gl_array_id);
  39. }
  40. vertex_array::~vertex_array()
  41. {
  42. glDeleteVertexArrays(1, &gl_array_id);
  43. }
  44. void vertex_array::bind_attribute(unsigned int index, const vertex_buffer& buffer, int size, vertex_attribute_type type, int stride, std::size_t offset)
  45. {
  46. GLenum gl_type = vertex_attribute_type_lut[static_cast<std::size_t>(type)];
  47. glBindVertexArray(gl_array_id);
  48. glBindBuffer(GL_ARRAY_BUFFER, buffer.gl_buffer_id);
  49. glVertexAttribPointer(index, size, gl_type, GL_FALSE, stride, (const GLvoid*)offset);
  50. glEnableVertexAttribArray(index);
  51. }
  52. void vertex_array::bind_elements(const vertex_buffer& buffer)
  53. {
  54. glBindVertexArray(gl_array_id);
  55. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer.gl_buffer_id);
  56. }
  57. } // namespace gl