gltf_buffer_view.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**************************************************************************/
  2. /* gltf_buffer_view.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gltf_buffer_view.h"
  31. #include "gltf_buffer_view.compat.inc"
  32. #include "../gltf_state.h"
  33. void GLTFBufferView::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("load_buffer_view_data", "state"), &GLTFBufferView::load_buffer_view_data);
  35. ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer);
  36. ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer);
  37. ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFBufferView::get_byte_offset);
  38. ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFBufferView::set_byte_offset);
  39. ClassDB::bind_method(D_METHOD("get_byte_length"), &GLTFBufferView::get_byte_length);
  40. ClassDB::bind_method(D_METHOD("set_byte_length", "byte_length"), &GLTFBufferView::set_byte_length);
  41. ClassDB::bind_method(D_METHOD("get_byte_stride"), &GLTFBufferView::get_byte_stride);
  42. ClassDB::bind_method(D_METHOD("set_byte_stride", "byte_stride"), &GLTFBufferView::set_byte_stride);
  43. ClassDB::bind_method(D_METHOD("get_indices"), &GLTFBufferView::get_indices);
  44. ClassDB::bind_method(D_METHOD("set_indices", "indices"), &GLTFBufferView::set_indices);
  45. ClassDB::bind_method(D_METHOD("get_vertex_attributes"), &GLTFBufferView::get_vertex_attributes);
  46. ClassDB::bind_method(D_METHOD("set_vertex_attributes", "is_attributes"), &GLTFBufferView::set_vertex_attributes);
  47. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer"), "set_buffer", "get_buffer"); // GLTFBufferIndex
  48. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int
  49. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_length"), "set_byte_length", "get_byte_length"); // int
  50. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_stride"), "set_byte_stride", "get_byte_stride"); // int
  51. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indices"), "set_indices", "get_indices"); // bool
  52. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertex_attributes"), "set_vertex_attributes", "get_vertex_attributes"); // bool
  53. }
  54. GLTFBufferIndex GLTFBufferView::get_buffer() const {
  55. return buffer;
  56. }
  57. void GLTFBufferView::set_buffer(GLTFBufferIndex p_buffer) {
  58. buffer = p_buffer;
  59. }
  60. int GLTFBufferView::get_byte_offset() const {
  61. return byte_offset;
  62. }
  63. void GLTFBufferView::set_byte_offset(int p_byte_offset) {
  64. byte_offset = p_byte_offset;
  65. }
  66. int GLTFBufferView::get_byte_length() const {
  67. return byte_length;
  68. }
  69. void GLTFBufferView::set_byte_length(int p_byte_length) {
  70. byte_length = p_byte_length;
  71. }
  72. int GLTFBufferView::get_byte_stride() const {
  73. return byte_stride;
  74. }
  75. void GLTFBufferView::set_byte_stride(int p_byte_stride) {
  76. byte_stride = p_byte_stride;
  77. }
  78. bool GLTFBufferView::get_indices() const {
  79. return indices;
  80. }
  81. void GLTFBufferView::set_indices(bool p_indices) {
  82. indices = p_indices;
  83. }
  84. bool GLTFBufferView::get_vertex_attributes() const {
  85. return vertex_attributes;
  86. }
  87. void GLTFBufferView::set_vertex_attributes(bool p_attributes) {
  88. vertex_attributes = p_attributes;
  89. }
  90. Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_state) const {
  91. ERR_FAIL_COND_V(p_state.is_null(), Vector<uint8_t>());
  92. ERR_FAIL_COND_V_MSG(byte_stride > 0, Vector<uint8_t>(), "Buffer views with byte stride are not yet supported by this method.");
  93. const TypedArray<Vector<uint8_t>> &buffers = p_state->get_buffers();
  94. ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>());
  95. const PackedByteArray &buffer_data = buffers[buffer];
  96. const int64_t byte_end = byte_offset + byte_length;
  97. return buffer_data.slice(byte_offset, byte_end);
  98. }