MeshResource.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "Texture.h"
  28. #include "Allocator.h"
  29. #include "Bundle.h"
  30. namespace crown
  31. {
  32. // Bump the version whenever a change in the format is made.
  33. const uint32_t MESH_VERSION = 1;
  34. struct MeshHeader
  35. {
  36. uint32_t version;
  37. uint32_t mesh_count;
  38. uint32_t joint_count;
  39. uint32_t padding[16];
  40. };
  41. class MeshResource
  42. {
  43. public:
  44. //-----------------------------------------------------------------------------
  45. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  46. {
  47. File* file = bundle.open(id);
  48. MeshResource* resource = (MeshResource*)allocator.allocate(sizeof(MeshResource));
  49. file->read(&resource->m_header, sizeof(MeshHeader));
  50. // Read vertices
  51. file->read(&resource->m_vertex_count, sizeof(uint32_t));
  52. resource->m_vertices = (float*) allocator.allocate(sizeof(float) * resource->m_vertex_count);
  53. file->read(resource->m_vertices, sizeof(float) * resource->m_vertex_count);
  54. // Read triangles
  55. file->read(&resource->m_index_count, sizeof(uint32_t));
  56. resource->m_indices = (uint16_t*) allocator.allocate(sizeof(uint16_t) * resource->m_index_count);
  57. file->read(resource->m_indices, sizeof(uint16_t) * resource->m_index_count);
  58. bundle.close(file);
  59. return resource;
  60. }
  61. //-----------------------------------------------------------------------------
  62. static void online(void* )
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. static void unload(Allocator& , void* )
  67. {
  68. }
  69. //-----------------------------------------------------------------------------
  70. static void offline(void* /*resource*/)
  71. {
  72. }
  73. public:
  74. MeshHeader m_header;
  75. size_t m_vertex_count;
  76. float* m_vertices;
  77. size_t m_index_count;
  78. uint16_t* m_indices;
  79. };
  80. } // namespace crown