Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Model.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "resource/Model.h"
  19. #include "resource/BSP.h"
  20. #include "resource/ImgColor.h"
  21. #include "resource/ImgTex.h"
  22. #include "resource/Surface.h"
  23. #include "BinReader.h"
  24. #include "Core.h"
  25. #include "ResourceCache.h"
  26. #include "util.h"
  27. enum ModelFlags
  28. {
  29. kHasPhysicsBSP = 0x1,
  30. kHasDrawingBSP = 0x2,
  31. kHasDegrade = 0x8
  32. };
  33. static vector<TriangleFan> readTriangleFans(BinReader& reader)
  34. {
  35. uint16_t numTriangleFans = reader.readPackedShort();
  36. vector<TriangleFan> triangleFans(numTriangleFans);
  37. for(uint16_t i = 0; i < numTriangleFans; i++)
  38. {
  39. uint16_t triangleFanNum = reader.readShort();
  40. assert(triangleFanNum == i);
  41. UNUSED(triangleFanNum);
  42. read(reader, triangleFans[i]);
  43. }
  44. return triangleFans;
  45. }
  46. Model::Model(uint32_t id, const void* data, size_t size) : ResourceImpl{id}, needsDepthSort{false}
  47. {
  48. BinReader reader(data, size);
  49. uint32_t resourceId = reader.readInt();
  50. assert(resourceId == id);
  51. UNUSED(resourceId);
  52. uint32_t flags = reader.readInt();
  53. assert(flags == 0x2 || flags == 0x3 || flags == 0xA || flags == 0xB);
  54. uint8_t numSurfaces = reader.readByte();
  55. surfaces.resize(numSurfaces);
  56. for(ResourcePtr& surface : surfaces)
  57. {
  58. uint32_t surfaceId = reader.readInt();
  59. surface = Core::get().resourceCache().get(surfaceId);
  60. bool hasAlpha = surface->cast<Surface>()
  61. .imgTex->cast<ImgTex>()
  62. .imgColor->cast<ImgColor>()
  63. .image.hasAlpha();
  64. needsDepthSort = needsDepthSort || hasAlpha;
  65. }
  66. uint32_t one = reader.readInt();
  67. assert(one == 1);
  68. UNUSED(one);
  69. uint16_t numVertices = reader.readShort();
  70. vertices.resize(numVertices);
  71. uint16_t flags2 = reader.readShort();
  72. assert(flags2 == 0x0000 || flags2 == 0x8000);
  73. UNUSED(flags2);
  74. for(uint16_t i = 0; i < numVertices; i++)
  75. {
  76. uint16_t vertexNum = reader.readShort();
  77. assert(vertexNum == i);
  78. UNUSED(vertexNum);
  79. read(reader, vertices[i]);
  80. }
  81. if(flags & kHasPhysicsBSP)
  82. {
  83. hitTriangleFans = readTriangleFans(reader);
  84. read(reader, hitTree, BSPTreeType::kPhysics);
  85. }
  86. glm::vec3 sortCenter;
  87. read(reader, sortCenter);
  88. if(flags & kHasDrawingBSP)
  89. {
  90. triangleFans = readTriangleFans(reader);
  91. unique_ptr<BSPNode> drawingBSP;
  92. read(reader, drawingBSP, BSPTreeType::kDrawing);
  93. }
  94. if(flags & kHasDegrade)
  95. {
  96. /*degradeId*/ reader.readInt();
  97. }
  98. assert(reader.remaining() == 0);
  99. }
  100. Model::~Model()
  101. {}