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

BSP.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/BSP.h"
  19. #include "physics/Sphere.h"
  20. #include "BinReader.h"
  21. BSPNode::BSPNode()
  22. {}
  23. BSPNode::BSPNode(BinReader& reader, BSPTreeType treeType, uint32_t nodeType)
  24. {
  25. read(reader, partition_);
  26. if(nodeType == 0x42506e6e || nodeType == 0x4250496e) // BPnn, BPIn
  27. {
  28. read(reader, frontChild_, treeType);
  29. }
  30. else if(nodeType == 0x4270494e || nodeType == 0x42706e4e) // BpIN, BpnN
  31. {
  32. read(reader, backChild_ , treeType);
  33. }
  34. else if(nodeType == 0x4250494e || nodeType == 0x42506e4e) // BPIN, BPnN
  35. {
  36. read(reader, frontChild_, treeType);
  37. read(reader, backChild_, treeType);
  38. }
  39. if(treeType == BSPTreeType::kDrawing || treeType == BSPTreeType::kPhysics)
  40. {
  41. read(reader, bounds_);
  42. }
  43. if(treeType == BSPTreeType::kDrawing)
  44. {
  45. uint32_t triCount = reader.readInt();
  46. triangleIndices_.resize(triCount);
  47. for(uint16_t& index : triangleIndices_)
  48. {
  49. index = reader.readShort();
  50. }
  51. }
  52. }
  53. BSPLeaf::BSPLeaf(BinReader& reader, BSPTreeType treeType)
  54. {
  55. index_ = reader.readInt();
  56. if(treeType == BSPTreeType::kPhysics)
  57. {
  58. // if 1, sphere parameters are valid and there are indices
  59. solid_ = reader.readInt();
  60. read(reader, bounds_);
  61. uint32_t triCount = reader.readInt();
  62. triangleIndices_.resize(triCount);
  63. for(uint16_t& index : triangleIndices_)
  64. {
  65. index = reader.readShort();
  66. }
  67. }
  68. }
  69. BSPPortal::BSPPortal(BinReader& reader, BSPTreeType treeType)
  70. {
  71. read(reader, partition_);
  72. read(reader, frontChild_, treeType);
  73. read(reader, backChild_, treeType);
  74. if(treeType == BSPTreeType::kDrawing)
  75. {
  76. read(reader, bounds_);
  77. uint32_t triCount = reader.readInt();
  78. triangleIndices_.resize(triCount);
  79. uint32_t polyCount = reader.readInt();
  80. portalPolys_.resize(polyCount);
  81. for(uint16_t& index : triangleIndices_)
  82. {
  83. index = reader.readShort();
  84. }
  85. for(PortalPoly& poly : portalPolys_)
  86. {
  87. poly.portalIndex = reader.readShort();
  88. poly.polygonIndex = reader.readShort();
  89. }
  90. }
  91. }
  92. void read(BinReader& reader, unique_ptr<BSPNode>& node, BSPTreeType treeType)
  93. {
  94. uint32_t nodeType = reader.readInt();
  95. if(nodeType == 0x4c454146) // LEAF
  96. {
  97. node.reset(new BSPLeaf{reader, treeType});
  98. }
  99. else if(nodeType == 0x504f5254) // PORT
  100. {
  101. node.reset(new BSPPortal{reader, treeType});
  102. }
  103. else
  104. {
  105. node.reset(new BSPNode{reader, treeType, nodeType});
  106. }
  107. }