MeshResource.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. VertexBufferId vbuffer;
  39. IndexBufferId ibuffer;
  40. uint32_t version;
  41. uint32_t num_meshes;
  42. uint32_t num_joints;
  43. uint32_t padding[16];
  44. };
  45. struct VertexData
  46. {
  47. uint32_t num_vertices;
  48. VertexFormat::Enum format;
  49. uint32_t offset;
  50. };
  51. struct IndexData
  52. {
  53. uint32_t num_indices;
  54. uint32_t offset;
  55. };
  56. struct MeshData
  57. {
  58. VertexData vertices;
  59. IndexData indices;
  60. };
  61. struct MeshResource
  62. {
  63. public:
  64. //-----------------------------------------------------------------------------
  65. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  66. {
  67. File* file = bundle.open(id);
  68. const size_t file_size = file->size();
  69. void* res = allocator.allocate(file_size);
  70. file->read(res, file_size);
  71. bundle.close(file);
  72. return res;
  73. }
  74. //-----------------------------------------------------------------------------
  75. static void online(void* resource)
  76. {
  77. MeshResource* m = (MeshResource*) resource;
  78. MeshHeader* h = (MeshHeader*) m;
  79. h->vbuffer = device()->renderer()->create_vertex_buffer(m->num_vertices() * Vertex::bytes_per_vertex(m->vertex_format()), m->vertices(), m->vertex_format());
  80. h->ibuffer = device()->renderer()->create_index_buffer(m->num_indices() * sizeof(uint16_t), m->indices());
  81. }
  82. //-----------------------------------------------------------------------------
  83. static void unload(Allocator& a, void* res)
  84. {
  85. CE_ASSERT_NOT_NULL(res);
  86. a.deallocate(res);
  87. }
  88. //-----------------------------------------------------------------------------
  89. static void offline(void* resource)
  90. {
  91. MeshResource* m = (MeshResource*) resource;
  92. MeshHeader* h = (MeshHeader*) m;
  93. device()->renderer()->destroy_index_buffer(h->ibuffer);
  94. device()->renderer()->destroy_vertex_buffer(h->vbuffer);
  95. }
  96. //-----------------------------------------------------------------------------
  97. uint32_t num_vertices() const
  98. {
  99. MeshData* data = (MeshData*) ((char*) this) + sizeof(MeshHeader);
  100. return data->vertices.num_vertices;
  101. }
  102. //-----------------------------------------------------------------------------
  103. VertexFormat::Enum vertex_format() const
  104. {
  105. MeshData* data = (MeshData*) ((char*) this) + sizeof(MeshHeader);
  106. return data->vertices.format;
  107. }
  108. //-----------------------------------------------------------------------------
  109. float* vertices() const
  110. {
  111. MeshData* data = (MeshData*) ((char*) this) + sizeof(MeshHeader);
  112. return (float*) (((char*)this) + data->vertices.offset);
  113. }
  114. //-----------------------------------------------------------------------------
  115. uint32_t num_indices() const
  116. {
  117. MeshData* data = (MeshData*) ((char*) this) + sizeof(MeshHeader);
  118. return data->indices.num_indices;
  119. }
  120. //-----------------------------------------------------------------------------
  121. uint16_t* indices() const
  122. {
  123. MeshData* data = (MeshData*) ((char*) this) + sizeof(MeshHeader);
  124. return (uint16_t*) (((char*)this) + data->indices.offset);
  125. }
  126. //-----------------------------------------------------------------------------
  127. VertexBufferId vertex_buffer() const
  128. {
  129. MeshHeader* h = (MeshHeader*) this;
  130. return h->vbuffer;
  131. }
  132. //-----------------------------------------------------------------------------
  133. IndexBufferId index_buffer() const
  134. {
  135. MeshHeader* h = (MeshHeader*) this;
  136. return h->ibuffer;
  137. }
  138. private:
  139. // Disable construction
  140. MeshResource();
  141. };
  142. } // namespace crown