MeshResource.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "Resource.h"
  26. #include "PixelFormat.h"
  27. #include "Allocator.h"
  28. #include "Bundle.h"
  29. #include "File.h"
  30. #include "Device.h"
  31. #include "Renderer.h"
  32. namespace crown
  33. {
  34. // Bump the version whenever a change in the format is made.
  35. const uint32_t MESH_VERSION = 1;
  36. struct MeshHeader
  37. {
  38. uint32_t version;
  39. uint32_t num_meshes;
  40. uint32_t num_joints;
  41. uint32_t padding[16];
  42. };
  43. struct VertexData
  44. {
  45. uint32_t num_vertices;
  46. VertexFormat::Enum format;
  47. uint32_t offset;
  48. };
  49. struct IndexData
  50. {
  51. uint32_t num_indices;
  52. uint32_t offset;
  53. };
  54. struct MeshData
  55. {
  56. VertexData vertices;
  57. IndexData indices;
  58. };
  59. class MeshResource
  60. {
  61. public:
  62. //-----------------------------------------------------------------------------
  63. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  64. {
  65. File* file = bundle.open(id);
  66. const size_t file_size = file->size() - 12;
  67. MeshResource* res = (MeshResource*) allocator.allocate(sizeof(MeshResource));
  68. res->m_data = (uint8_t*) allocator.allocate(file_size);
  69. file->read(res->m_data, file_size);
  70. bundle.close(file);
  71. return res;
  72. }
  73. //-----------------------------------------------------------------------------
  74. static void online(void* resource)
  75. {
  76. MeshResource* m = (MeshResource*) resource;
  77. m->m_vbuffer = device()->renderer()->create_vertex_buffer(m->num_vertices(), m->vertex_format(), m->vertices());
  78. m->m_ibuffer = device()->renderer()->create_index_buffer(m->num_indices(), m->indices());
  79. }
  80. //-----------------------------------------------------------------------------
  81. static void unload(Allocator& a, void* res)
  82. {
  83. MeshResource* resource = (MeshResource*)res;
  84. a.deallocate(resource->m_data);
  85. a.deallocate(resource);
  86. }
  87. //-----------------------------------------------------------------------------
  88. static void offline(void* resource)
  89. {
  90. MeshResource* m = (MeshResource*) resource;
  91. device()->renderer()->destroy_index_buffer(m->m_ibuffer);
  92. device()->renderer()->destroy_vertex_buffer(m->m_vbuffer);
  93. }
  94. //-----------------------------------------------------------------------------
  95. uint32_t num_vertices()
  96. {
  97. MeshData* data = (MeshData*) (m_data + sizeof(MeshHeader));
  98. return data->vertices.num_vertices;
  99. }
  100. //-----------------------------------------------------------------------------
  101. VertexFormat::Enum vertex_format()
  102. {
  103. MeshData* data = (MeshData*) (m_data + sizeof(MeshHeader));
  104. return data->vertices.format;
  105. }
  106. //-----------------------------------------------------------------------------
  107. float* vertices()
  108. {
  109. MeshData* data = (MeshData*) (m_data + sizeof(MeshHeader));
  110. return (float*) (m_data + data->vertices.offset);
  111. }
  112. //-----------------------------------------------------------------------------
  113. uint32_t num_indices()
  114. {
  115. MeshData* data = (MeshData*) (m_data + sizeof(MeshHeader));
  116. return data->indices.num_indices;
  117. }
  118. //-----------------------------------------------------------------------------
  119. uint16_t* indices()
  120. {
  121. MeshData* data = (MeshData*) (m_data + sizeof(MeshHeader));
  122. return (uint16_t*) (m_data + data->indices.offset);
  123. }
  124. public:
  125. uint8_t* m_data;
  126. VertexBufferId m_vbuffer;
  127. IndexBufferId m_ibuffer;
  128. };
  129. } // namespace crown