diff --git a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp index 6b5b163d..329359ef 100644 --- a/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp +++ b/src/Components/Modules/AssetInterfaces/IclipMap_t.cpp @@ -1,6 +1,7 @@ #include "StdInclude.hpp" #define IW4X_CLIPMAP_VERSION 1 +#define SMODEL_QUADTREE_DEPTH 6 #ifdef ENABLE_EXPERIMENTAL_MAP_CODE @@ -883,15 +884,35 @@ namespace Assets clipMap->mapEnts = Components::AssetHandler::FindAssetForZone(Game::XAssetType::ASSET_TYPE_MAP_ENTS, Utils::String::VA("maps/mp/%s.d3dbsp", name.data()), builder).mapEnts; - // This mustn't be null and has to have at least 1 'valid' entry. - if (!clipMap->smodelNodeCount || !clipMap->smodelNodes) - { - clipMap->smodelNodeCount = 1; - clipMap->smodelNodes = builder->getAllocator()->allocateArray(clipMap->smodelNodeCount); + //SModelQuadtree* quadtree = new SModelQuadtree(clipMap->staticModelList, clipMap->numStaticModels); - clipMap->smodelNodes[0].bounds.halfSize[0] = -131072.000f; - clipMap->smodelNodes[0].bounds.halfSize[1] = -131072.000f; - clipMap->smodelNodes[0].bounds.halfSize[2] = -131072.000f; + clipMap->smodelNodeCount = 1; + clipMap->smodelNodes = builder->getAllocator()->allocateArray(clipMap->smodelNodeCount); + + clipMap->smodelNodes[0].childCount = clipMap->numStaticModels; + clipMap->smodelNodes[0].firstChild = 0; + + for (int i = 0; i < clipMap->numStaticModels; ++i) + { + clipMap->smodelNodes[0].bounds.midPoint[0] += clipMap->staticModelList[i].origin[0]; + clipMap->smodelNodes[0].bounds.midPoint[1] += clipMap->staticModelList[i].origin[1]; + clipMap->smodelNodes[0].bounds.midPoint[2] += clipMap->staticModelList[i].origin[2]; + } + + clipMap->smodelNodes[0].bounds.midPoint[0] /= clipMap->numStaticModels; + clipMap->smodelNodes[0].bounds.midPoint[1] /= clipMap->numStaticModels; + clipMap->smodelNodes[0].bounds.midPoint[2] /= clipMap->numStaticModels; + + float* nodePos = clipMap->smodelNodes[0].bounds.midPoint; + + for (int i = 0; i < clipMap->numStaticModels; ++i) + { + if (fabs(nodePos[0] - clipMap->staticModelList[i].origin[0]) > clipMap->smodelNodes[0].bounds.halfSize[0]) + clipMap->smodelNodes[0].bounds.halfSize[0] = fabs(nodePos[0] - clipMap->staticModelList[i].origin[0]); + if (fabs(nodePos[1] - clipMap->staticModelList[i].origin[1]) > clipMap->smodelNodes[0].bounds.halfSize[1]) + clipMap->smodelNodes[0].bounds.halfSize[1] = fabs(nodePos[1] - clipMap->staticModelList[i].origin[1]); + if (fabs(nodePos[1] - clipMap->staticModelList[i].origin[2]) > clipMap->smodelNodes[0].bounds.halfSize[2]) + clipMap->smodelNodes[0].bounds.halfSize[2] = fabs(nodePos[2] - clipMap->staticModelList[i].origin[2]); } // These mustn't be null, but they don't need to be valid. @@ -902,7 +923,7 @@ namespace Assets Utils::Stream::ClearPointer(&clipMap->dynEntCollList[i]); } - if(!reader.end()) + if (!reader.end()) { Components::Logger::Error("Clipmap data left!"); } diff --git a/src/Components/Modules/AssetInterfaces/IclipMap_t.hpp b/src/Components/Modules/AssetInterfaces/IclipMap_t.hpp index b3aba110..363b5f48 100644 --- a/src/Components/Modules/AssetInterfaces/IclipMap_t.hpp +++ b/src/Components/Modules/AssetInterfaces/IclipMap_t.hpp @@ -15,13 +15,17 @@ namespace Assets class SModelQuadtree { public: - SModelQuadtree(Game::cStaticModel_t* /*modelList*/, int numModels) + SModelQuadtree() + { + } + + SModelQuadtree(Game::cStaticModel_t* modelList, int numModels) { numValues = 0; for (int i = 0; i < numModels; ++i) { - + insert(&modelList[i]); } } @@ -35,8 +39,13 @@ namespace Assets { if (numValues == 4) // split { - numValues++; + // create children objects for (int i = 0; i < 4; ++i) + { + children[i] = new SModelQuadtree(); + } + + for (int i = 0; i < numValues; ++i) { if (item->origin[0] > x && values[i]->origin[1] > y) children[0]->insert(values[i]); @@ -54,8 +63,12 @@ namespace Assets { children[i]->halfX = halfX / 2; children[i]->halfY = halfY / 2; - children[i]->halfZ = halfZ / 2; + children[i]->halfZ = halfZ; } + + // update origins here + + numValues++; } if (item->origin[0] > x && item->origin[1] > y)