2013-02-24 13:52:23 -08:00
|
|
|
#include "bulletnifloader.hpp"
|
2013-03-08 23:46:25 +01:00
|
|
|
|
2021-10-31 20:34:45 +01:00
|
|
|
#include <cassert>
|
2022-06-27 21:32:46 +02:00
|
|
|
#include <sstream>
|
2022-07-06 13:13:35 +02:00
|
|
|
#include <tuple>
|
2021-10-31 14:59:35 +01:00
|
|
|
#include <variant>
|
2015-05-12 03:02:15 +02:00
|
|
|
#include <vector>
|
2011-01-13 17:51:50 +01:00
|
|
|
|
2015-05-27 22:32:11 +02:00
|
|
|
#include <BulletCollision/CollisionShapes/btBoxShape.h>
|
|
|
|
#include <BulletCollision/CollisionShapes/btTriangleMesh.h>
|
|
|
|
|
2018-08-14 19:42:41 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2020-11-07 03:33:04 +03:00
|
|
|
#include <components/misc/convert.hpp>
|
2022-08-03 00:00:54 +02:00
|
|
|
|
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2013-03-08 23:46:25 +01:00
|
|
|
|
2015-11-27 21:01:28 -05:00
|
|
|
#include <components/nif/data.hpp>
|
|
|
|
#include <components/nif/extra.hpp>
|
|
|
|
#include <components/nif/node.hpp>
|
2022-01-22 00:15:56 +01:00
|
|
|
#include <components/nif/parent.hpp>
|
2011-01-13 17:51:50 +01:00
|
|
|
|
2022-07-03 00:02:29 +02:00
|
|
|
#include <components/files/conversion.hpp>
|
2021-04-11 10:28:56 +02:00
|
|
|
|
2015-05-07 21:17:15 +02:00
|
|
|
namespace
|
2015-01-12 11:29:56 +01:00
|
|
|
{
|
|
|
|
|
2018-07-11 01:05:19 +03:00
|
|
|
bool pathFileNameStartsWithX(const std::string& path)
|
|
|
|
{
|
2020-11-07 03:33:04 +03:00
|
|
|
const std::size_t slashpos = path.find_last_of("/\\");
|
|
|
|
const std::size_t letterPos = slashpos == std::string::npos ? 0 : slashpos + 1;
|
|
|
|
return letterPos < path.size() && (path[letterPos] == 'x' || path[letterPos] == 'X');
|
2018-07-11 01:05:19 +03:00
|
|
|
}
|
2015-01-12 11:29:56 +01:00
|
|
|
|
2023-03-08 04:24:52 +03:00
|
|
|
void prepareTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriBasedGeomData& data)
|
2019-08-08 22:33:56 +03:00
|
|
|
{
|
2023-03-08 04:24:52 +03:00
|
|
|
// FIXME: copying vertices/indices individually is unreasonable
|
2023-09-05 00:56:20 +03:00
|
|
|
const std::vector<osg::Vec3f>& vertices = data.mVertices;
|
2019-08-08 22:33:56 +03:00
|
|
|
mesh.preallocateVertices(static_cast<int>(vertices.size()));
|
2023-03-08 04:24:52 +03:00
|
|
|
for (const osg::Vec3f& vertex : vertices)
|
|
|
|
mesh.findOrAddVertex(Misc::Convert::toBullet(vertex), false);
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2023-09-03 16:52:10 +03:00
|
|
|
mesh.preallocateIndices(static_cast<int>(data.mNumTriangles) * 3);
|
2023-03-08 04:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriShapeData& data)
|
|
|
|
{
|
|
|
|
prepareTriangleMesh(mesh, data);
|
2023-09-05 00:08:28 +03:00
|
|
|
const std::vector<unsigned short>& triangles = data.mTriangles;
|
2019-08-08 22:33:56 +03:00
|
|
|
for (std::size_t i = 0; i < triangles.size(); i += 3)
|
2023-03-08 04:24:52 +03:00
|
|
|
mesh.addTriangleIndices(triangles[i + 0], triangles[i + 1], triangles[i + 2]);
|
2019-08-08 22:33:56 +03:00
|
|
|
}
|
2019-08-08 17:03:11 +03:00
|
|
|
|
2023-02-25 07:06:49 +03:00
|
|
|
void fillTriangleMesh(btTriangleMesh& mesh, const Nif::NiTriStripsData& data)
|
2019-08-08 17:03:11 +03:00
|
|
|
{
|
2023-03-08 04:24:52 +03:00
|
|
|
prepareTriangleMesh(mesh, data);
|
2023-09-03 17:41:52 +03:00
|
|
|
for (const std::vector<unsigned short>& strip : data.mStrips)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2019-08-08 22:33:56 +03:00
|
|
|
if (strip.size() < 3)
|
|
|
|
continue;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-07-06 12:58:12 +02:00
|
|
|
unsigned short a;
|
2019-08-08 17:03:11 +03:00
|
|
|
unsigned short b = strip[0];
|
|
|
|
unsigned short c = strip[1];
|
|
|
|
for (size_t i = 2; i < strip.size(); i++)
|
|
|
|
{
|
2022-09-22 21:26:05 +03:00
|
|
|
a = b;
|
|
|
|
b = c;
|
|
|
|
c = strip[i];
|
2023-02-09 20:24:05 +03:00
|
|
|
if (a == b || b == c || a == c)
|
|
|
|
continue;
|
|
|
|
if (i % 2 == 0)
|
2023-03-08 04:24:52 +03:00
|
|
|
mesh.addTriangleIndices(a, b, c);
|
2023-02-09 20:24:05 +03:00
|
|
|
else
|
2023-03-08 04:24:52 +03:00
|
|
|
mesh.addTriangleIndices(a, c, b);
|
2019-08-08 17:03:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 14:59:35 +01:00
|
|
|
template <class Function>
|
|
|
|
auto handleNiGeometry(const Nif::NiGeometry& geometry, Function&& function)
|
2023-08-03 19:55:06 +00:00
|
|
|
-> decltype(function(static_cast<const Nif::NiTriShapeData&>(geometry.data.get())))
|
2021-10-31 14:55:04 +01:00
|
|
|
{
|
2021-10-31 14:23:44 +01:00
|
|
|
if (geometry.recType == Nif::RC_NiTriShape || geometry.recType == Nif::RC_BSLODTriShape)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2023-08-03 19:55:06 +00:00
|
|
|
if (geometry.data->recType != Nif::RC_NiTriShapeData)
|
2021-10-31 14:55:04 +01:00
|
|
|
return {};
|
|
|
|
|
2023-08-03 19:55:06 +00:00
|
|
|
auto data = static_cast<const Nif::NiTriShapeData*>(geometry.data.getPtr());
|
2023-09-05 00:08:28 +03:00
|
|
|
if (data->mTriangles.empty())
|
2021-10-31 14:55:04 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return function(static_cast<const Nif::NiTriShapeData&>(*data));
|
|
|
|
}
|
2021-10-31 14:59:35 +01:00
|
|
|
|
|
|
|
if (geometry.recType == Nif::RC_NiTriStrips)
|
2021-10-31 14:55:04 +01:00
|
|
|
{
|
2023-08-03 19:55:06 +00:00
|
|
|
if (geometry.data->recType != Nif::RC_NiTriStripsData)
|
2021-10-31 14:55:04 +01:00
|
|
|
return {};
|
|
|
|
|
2023-08-03 19:55:06 +00:00
|
|
|
auto data = static_cast<const Nif::NiTriStripsData*>(geometry.data.getPtr());
|
2023-09-03 17:41:52 +03:00
|
|
|
if (data->mStrips.empty())
|
2021-10-31 14:55:04 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return function(static_cast<const Nif::NiTriStripsData&>(*data));
|
|
|
|
}
|
2021-10-31 14:59:35 +01:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<btTriangleMesh> makeChildMesh(const Nif::NiGeometry& geometry)
|
|
|
|
{
|
|
|
|
return handleNiGeometry(geometry, [&](const auto& data) {
|
2022-05-29 13:24:32 +02:00
|
|
|
auto mesh = std::make_unique<btTriangleMesh>();
|
2023-02-25 07:06:49 +03:00
|
|
|
fillTriangleMesh(*mesh, data);
|
2021-10-31 14:59:35 +01:00
|
|
|
return mesh;
|
|
|
|
});
|
2019-08-08 17:03:11 +03:00
|
|
|
}
|
|
|
|
|
2012-03-25 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 01:05:19 +03:00
|
|
|
namespace NifBullet
|
2011-01-13 17:51:50 +01:00
|
|
|
{
|
|
|
|
|
2022-09-18 14:12:49 +02:00
|
|
|
osg::ref_ptr<Resource::BulletShape> BulletNifLoader::load(Nif::FileView nif)
|
2011-01-13 17:51:50 +01:00
|
|
|
{
|
2015-11-16 23:30:10 +01:00
|
|
|
mShape = new Resource::BulletShape;
|
2011-01-13 17:51:50 +01:00
|
|
|
|
2021-11-10 20:24:17 +01:00
|
|
|
mCompoundShape.reset();
|
2023-02-25 07:06:49 +03:00
|
|
|
mAvoidCompoundShape.reset();
|
2021-11-10 20:24:17 +01:00
|
|
|
|
|
|
|
mShape->mFileHash = nif.getHash();
|
2011-01-13 17:51:50 +01:00
|
|
|
|
2020-11-28 13:12:30 +03:00
|
|
|
const size_t numRoots = nif.numRoots();
|
2023-09-10 05:23:25 +03:00
|
|
|
std::vector<const Nif::NiAVObject*> roots;
|
2020-11-28 13:12:30 +03:00
|
|
|
for (size_t i = 0; i < numRoots; ++i)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2020-11-28 13:12:30 +03:00
|
|
|
const Nif::Record* r = nif.getRoot(i);
|
2022-09-22 21:26:05 +03:00
|
|
|
if (!r)
|
2021-05-05 02:26:32 +03:00
|
|
|
continue;
|
2023-09-10 05:23:25 +03:00
|
|
|
const Nif::NiAVObject* node = dynamic_cast<const Nif::NiAVObject*>(r);
|
2021-10-31 14:23:44 +01:00
|
|
|
if (node)
|
2020-11-28 13:12:30 +03:00
|
|
|
roots.emplace_back(node);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2021-10-31 14:23:44 +01:00
|
|
|
const std::string filename = Files::pathToUnicodeString(nif.getFilename());
|
2022-07-03 00:02:29 +02:00
|
|
|
mShape->mFileName = filename;
|
2020-11-28 13:12:30 +03:00
|
|
|
if (roots.empty())
|
|
|
|
{
|
2022-05-29 13:24:32 +02:00
|
|
|
warn("Found no root nodes in NIF file " + filename);
|
2020-11-28 13:12:30 +03:00
|
|
|
return mShape;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a valid bounding box first. If one's found for any root node, use that.
|
2023-09-10 05:23:25 +03:00
|
|
|
for (const Nif::NiAVObject* node : roots)
|
2013-03-08 23:46:25 +01:00
|
|
|
{
|
2022-07-03 02:14:32 +02:00
|
|
|
if (findBoundingBox(*node, filename))
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-08-25 00:14:17 +02:00
|
|
|
const btVector3 extents = Misc::Convert::toBullet(mShape->mCollisionBox.mExtents);
|
2022-07-03 02:14:32 +02:00
|
|
|
const btVector3 center = Misc::Convert::toBullet(mShape->mCollisionBox.mCenter);
|
2022-05-29 13:24:32 +02:00
|
|
|
auto compound = std::make_unique<btCompoundShape>();
|
|
|
|
auto boxShape = std::make_unique<btBoxShape>(extents);
|
2020-11-28 13:12:30 +03:00
|
|
|
btTransform transform = btTransform::getIdentity();
|
2020-11-16 19:37:30 +03:00
|
|
|
transform.setOrigin(center);
|
2022-07-03 02:14:32 +02:00
|
|
|
compound->addChildShape(transform, boxShape.get());
|
|
|
|
std::ignore = boxShape.release();
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-08-25 00:14:17 +02:00
|
|
|
mShape->mCollisionShape.reset(compound.release());
|
2020-11-28 13:12:30 +03:00
|
|
|
return mShape;
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 00:14:17 +02:00
|
|
|
// files with the name convention xmodel.nif usually have keyframes stored in a separate file xmodel.kf (see
|
|
|
|
// Animation::addAnimSource). assume all nodes in the file will be animated
|
2023-01-14 04:36:06 +03:00
|
|
|
// TODO: investigate whether this should and could be optimized.
|
2022-08-25 00:14:17 +02:00
|
|
|
const bool isAnimated = pathFileNameStartsWithX(filename);
|
2015-05-12 03:02:15 +02:00
|
|
|
|
2022-07-03 02:14:32 +02:00
|
|
|
// If there's no bounding box, we'll have to generate a Bullet collision shape
|
2020-11-28 13:12:30 +03:00
|
|
|
// from the collision data present in every root node.
|
2023-09-10 05:23:25 +03:00
|
|
|
for (const Nif::NiAVObject* node : roots)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
const Nif::NiNode* colNode = findRootCollisionNode(*node);
|
|
|
|
bool hasCollisionShape = false;
|
|
|
|
if (colNode != nullptr)
|
|
|
|
{
|
|
|
|
if (colNode->mBounds.type == Nif::NiBoundingVolume::Type::BASE_BV && !colNode->mChildren.empty())
|
|
|
|
hasCollisionShape = true;
|
|
|
|
else
|
|
|
|
mShape->mVisualCollisionType = Resource::VisualCollisionType::Camera;
|
|
|
|
}
|
2023-07-10 20:14:53 +03:00
|
|
|
HandleNodeArgs args;
|
2023-09-10 05:23:25 +03:00
|
|
|
args.mAutogenerated = args.mIsCollisionNode = !hasCollisionShape;
|
2023-07-10 20:14:53 +03:00
|
|
|
args.mAnimated = isAnimated;
|
|
|
|
handleNode(filename, *node, nullptr, args, mShape->mVisualCollisionType);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
|
2020-11-28 13:12:30 +03:00
|
|
|
if (mCompoundShape)
|
2021-10-30 03:46:29 +02:00
|
|
|
mShape->mCollisionShape = std::move(mCompoundShape);
|
2015-05-12 03:02:15 +02:00
|
|
|
|
2023-02-25 07:06:49 +03:00
|
|
|
if (mAvoidCompoundShape)
|
|
|
|
mShape->mAvoidCollisionShape = std::move(mAvoidCompoundShape);
|
2020-11-28 13:12:30 +03:00
|
|
|
|
|
|
|
return mShape;
|
2011-03-23 13:42:28 +01:00
|
|
|
}
|
|
|
|
|
2015-05-12 03:02:15 +02:00
|
|
|
// Find a boundingBox in the node hierarchy.
|
|
|
|
// Return: use bounding box for collision?
|
2023-09-10 05:23:25 +03:00
|
|
|
bool BulletNifLoader::findBoundingBox(const Nif::NiAVObject& node, const std::string& filename)
|
2015-05-12 03:02:15 +02:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
unsigned int type = node.mBounds.type;
|
|
|
|
switch (type)
|
2020-11-14 03:42:15 +03:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
case Nif::NiBoundingVolume::Type::BASE_BV:
|
|
|
|
break;
|
|
|
|
case Nif::NiBoundingVolume::Type::BOX_BV:
|
|
|
|
mShape->mCollisionBox.mExtents = node.mBounds.box.extents;
|
|
|
|
mShape->mCollisionBox.mCenter = node.mBounds.box.center;
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-14 03:42:15 +03:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
std::stringstream warning;
|
|
|
|
warning << "Unsupported NiBoundingVolume type " << type << " in node " << node.recIndex;
|
|
|
|
warning << " in file " << filename;
|
|
|
|
warn(warning.str());
|
2020-11-14 03:42:15 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-12 03:02:15 +02:00
|
|
|
|
2023-09-10 05:23:25 +03:00
|
|
|
if (type != Nif::NiBoundingVolume::Type::BASE_BV && node.hasBBoxCollision())
|
|
|
|
return true;
|
|
|
|
|
2022-06-22 00:43:16 +03:00
|
|
|
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&node))
|
2015-05-12 03:02:15 +02:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
for (const auto& child : ninode->mChildren)
|
2022-09-18 15:59:42 +02:00
|
|
|
if (!child.empty() && findBoundingBox(child.get(), filename))
|
|
|
|
return true;
|
2015-05-12 03:02:15 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-10 05:23:25 +03:00
|
|
|
const Nif::NiNode* BulletNifLoader::findRootCollisionNode(const Nif::NiAVObject& rootNode) const
|
2011-04-01 23:35:56 +02:00
|
|
|
{
|
2013-02-20 05:35:52 +01:00
|
|
|
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&rootNode))
|
2018-07-30 08:24:30 +04:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
for (const auto& child : ninode->mChildren)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
if (!child.empty() && child.getPtr()->recType == Nif::RC_RootCollisionNode)
|
|
|
|
return static_cast<const Nif::NiNode*>(child.getPtr());
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-07-03 02:14:32 +02:00
|
|
|
}
|
2023-09-10 05:23:25 +03:00
|
|
|
return nullptr;
|
2022-07-03 02:14:32 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 05:23:25 +03:00
|
|
|
void BulletNifLoader::handleNode(const std::string& fileName, const Nif::NiAVObject& node,
|
|
|
|
const Nif::Parent* parent, HandleNodeArgs args, Resource::VisualCollisionType& visualCollisionType)
|
2022-07-03 02:14:32 +02:00
|
|
|
{
|
|
|
|
// TODO: allow on-the fly collision switching via toggling this flag
|
2022-06-22 00:43:16 +03:00
|
|
|
if (node.recType == Nif::RC_NiCollisionSwitch && !node.collisionActive())
|
2020-02-19 12:14:21 +04:00
|
|
|
return;
|
|
|
|
|
2023-09-14 02:46:20 +03:00
|
|
|
for (Nif::NiTimeControllerPtr ctrl = node.mController; !ctrl.empty(); ctrl = ctrl->mNext)
|
2023-09-09 21:32:42 +03:00
|
|
|
{
|
|
|
|
if (args.mAnimated)
|
|
|
|
break;
|
|
|
|
if (!ctrl->isActive())
|
|
|
|
continue;
|
|
|
|
switch (ctrl->recType)
|
|
|
|
{
|
|
|
|
case Nif::RC_NiKeyframeController:
|
|
|
|
case Nif::RC_NiPathController:
|
|
|
|
case Nif::RC_NiRollController:
|
|
|
|
args.mAnimated = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 20:43:24 +02:00
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
if (node.recType == Nif::RC_RootCollisionNode)
|
|
|
|
{
|
|
|
|
args.mIsCollisionNode = true;
|
|
|
|
if (args.mAutogenerated)
|
|
|
|
{
|
|
|
|
// Encountered a RootCollisionNode inside an autogenerated mesh.
|
2013-02-25 10:29:48 -08:00
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
// We treat empty RootCollisionNodes as NCC flag (set collisionType to `Camera`)
|
|
|
|
// and generate the camera collision shape based on rendered geometry.
|
|
|
|
if (visualCollisionType == Resource::VisualCollisionType::Camera)
|
|
|
|
return;
|
2013-04-28 11:12:55 -07:00
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
// Otherwise we'll want to notify the user.
|
|
|
|
Log(Debug::Info) << "RootCollisionNode is not attached to the root node in " << fileName
|
|
|
|
<< ". Treating it as a common NiTriShape.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't collide with AvoidNode shapes
|
|
|
|
if (node.recType == Nif::RC_AvoidNode)
|
|
|
|
args.mAvoid = true;
|
2018-10-26 13:24:05 +04:00
|
|
|
|
2011-04-01 23:35:56 +02:00
|
|
|
// Check for extra data
|
2023-09-09 21:32:42 +03:00
|
|
|
for (const auto& e : node.getExtraList())
|
2011-04-01 23:35:56 +02:00
|
|
|
{
|
2019-09-13 19:54:19 +03:00
|
|
|
if (e->recType == Nif::RC_NiStringExtraData)
|
2015-05-12 03:02:15 +02:00
|
|
|
{
|
|
|
|
// String markers may contain important information
|
|
|
|
// affecting the entire subtree of this node
|
2023-09-01 00:58:23 +03:00
|
|
|
auto sd = static_cast<const Nif::NiStringExtraData*>(e.getPtr());
|
2018-10-26 12:51:04 +04:00
|
|
|
|
2023-09-01 00:58:23 +03:00
|
|
|
if (Misc::StringUtils::ciStartsWith(sd->mData, "NC"))
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2021-04-11 10:28:56 +02:00
|
|
|
// NCC flag in vanilla is partly case sensitive: prefix NC is case insensitive but second C needs be
|
|
|
|
// uppercase
|
2023-09-01 00:58:23 +03:00
|
|
|
if (sd->mData.length() > 2 && sd->mData[2] == 'C')
|
2021-04-11 10:28:56 +02:00
|
|
|
// Collide only with camera.
|
2022-08-25 00:14:17 +02:00
|
|
|
visualCollisionType = Resource::VisualCollisionType::Camera;
|
2022-09-22 21:26:05 +03:00
|
|
|
else
|
2021-04-11 10:28:56 +02:00
|
|
|
// No collision.
|
2022-08-25 00:14:17 +02:00
|
|
|
visualCollisionType = Resource::VisualCollisionType::Default;
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2023-09-01 00:58:23 +03:00
|
|
|
// Don't autogenerate collision if MRK is set.
|
|
|
|
// FIXME: verify if this covers the entire subtree
|
|
|
|
else if (sd->mData == "MRK" && args.mAutogenerated)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 03:02:42 +02:00
|
|
|
else if (e->recType == Nif::RC_BSXFlags)
|
|
|
|
{
|
|
|
|
auto bsxFlags = static_cast<const Nif::NiIntegerExtraData*>(e.getPtr());
|
2023-09-01 00:58:23 +03:00
|
|
|
if (bsxFlags->mData & 32) // Editor marker flag
|
2023-07-10 20:14:53 +03:00
|
|
|
args.mHasMarkers = true;
|
2023-05-24 03:02:42 +02:00
|
|
|
}
|
2011-04-01 23:35:56 +02:00
|
|
|
}
|
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
if (args.mIsCollisionNode)
|
2013-03-09 10:28:10 +01:00
|
|
|
{
|
2023-09-10 05:23:25 +03:00
|
|
|
// NOTE: a trishape with bounds, but no BBoxCollision flag should NOT go through handleNiTriShape!
|
2021-04-11 10:28:56 +02:00
|
|
|
// It must be ignored completely.
|
|
|
|
// (occurs in tr_ex_imp_wall_arch_04.nif)
|
2023-09-10 05:23:25 +03:00
|
|
|
if (node.mBounds.type == Nif::NiBoundingVolume::Type::BASE_BV
|
2021-10-31 14:23:44 +01:00
|
|
|
&& (node.recType == Nif::RC_NiTriShape || node.recType == Nif::RC_NiTriStrips
|
|
|
|
|| node.recType == Nif::RC_BSLODTriShape))
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2023-07-10 20:14:53 +03:00
|
|
|
handleNiTriShape(static_cast<const Nif::NiGeometry&>(node), parent, args);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2019-08-08 13:11:24 +03:00
|
|
|
}
|
2013-02-20 05:35:52 +01:00
|
|
|
|
|
|
|
// For NiNodes, loop through children
|
2021-10-31 14:23:44 +01:00
|
|
|
if (const Nif::NiNode* ninode = dynamic_cast<const Nif::NiNode*>(&node))
|
2011-04-01 23:35:56 +02:00
|
|
|
{
|
2022-01-22 00:15:56 +01:00
|
|
|
const Nif::Parent currentParent{ *ninode, parent };
|
2023-09-10 05:23:25 +03:00
|
|
|
for (const auto& child : ninode->mChildren)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-09-18 15:59:42 +02:00
|
|
|
if (child.empty())
|
2021-10-31 20:34:45 +01:00
|
|
|
continue;
|
|
|
|
|
2023-09-10 05:23:25 +03:00
|
|
|
assert(std::find(child->mParents.begin(), child->mParents.end(), ninode) != child->mParents.end());
|
2023-07-10 20:14:53 +03:00
|
|
|
handleNode(fileName, child.get(), ¤tParent, args, visualCollisionType);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2011-04-01 23:35:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-08 13:11:24 +03:00
|
|
|
|
2023-03-08 14:17:45 +03:00
|
|
|
void BulletNifLoader::handleNiTriShape(
|
2023-07-10 20:14:53 +03:00
|
|
|
const Nif::NiGeometry& niGeometry, const Nif::Parent* nodeParent, HandleNodeArgs args)
|
2019-08-08 17:03:11 +03:00
|
|
|
{
|
2023-07-10 20:14:53 +03:00
|
|
|
// mHasMarkers is specifically BSXFlags editor marker flag.
|
|
|
|
// If this changes, the check must be corrected.
|
2023-09-09 21:32:42 +03:00
|
|
|
if (args.mHasMarkers && Misc::StringUtils::ciStartsWith(niGeometry.mName, "EditorMarker"))
|
2023-07-10 20:14:53 +03:00
|
|
|
return;
|
|
|
|
|
2023-09-05 00:56:20 +03:00
|
|
|
if (niGeometry.data.empty() || niGeometry.data->mVertices.empty())
|
2021-10-31 14:59:35 +01:00
|
|
|
return;
|
|
|
|
|
2023-08-03 19:55:06 +00:00
|
|
|
if (!niGeometry.skin.empty())
|
2023-07-10 20:14:53 +03:00
|
|
|
args.mAnimated = false;
|
2023-07-16 03:06:00 +03:00
|
|
|
// TODO: handle NiSkinPartition
|
2019-08-08 17:03:11 +03:00
|
|
|
|
2023-02-25 07:06:49 +03:00
|
|
|
std::unique_ptr<btTriangleMesh> childMesh = makeChildMesh(niGeometry);
|
|
|
|
if (childMesh == nullptr || childMesh->getNumTriangles() == 0)
|
|
|
|
return;
|
2019-08-08 17:03:11 +03:00
|
|
|
|
2023-02-25 07:06:49 +03:00
|
|
|
auto childShape = std::make_unique<Resource::TriangleMeshShape>(childMesh.get(), true);
|
|
|
|
std::ignore = childMesh.release();
|
2019-08-08 17:03:11 +03:00
|
|
|
|
2023-09-10 05:23:25 +03:00
|
|
|
osg::Matrixf transform = niGeometry.mTransform.toMatrix();
|
2023-02-25 07:06:49 +03:00
|
|
|
for (const Nif::Parent* parent = nodeParent; parent != nullptr; parent = parent->mParent)
|
2023-09-10 05:23:25 +03:00
|
|
|
transform *= parent->mNiNode.mTransform.toMatrix();
|
2023-03-08 14:17:45 +03:00
|
|
|
childShape->setLocalScaling(Misc::Convert::toBullet(transform.getScale()));
|
|
|
|
transform.orthoNormalize(transform);
|
|
|
|
|
|
|
|
btTransform trans;
|
|
|
|
trans.setOrigin(Misc::Convert::toBullet(transform.getTrans()));
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
trans.getBasis()[i][j] = transform(j, i);
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
if (!args.mAvoid)
|
2023-02-25 07:06:49 +03:00
|
|
|
{
|
|
|
|
if (!mCompoundShape)
|
|
|
|
mCompoundShape.reset(new btCompoundShape);
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2023-07-10 20:14:53 +03:00
|
|
|
if (args.mAnimated)
|
2023-02-25 07:06:49 +03:00
|
|
|
mShape->mAnimatedShapes.emplace(niGeometry.recIndex, mCompoundShape->getNumChildShapes());
|
2019-08-08 17:03:11 +03:00
|
|
|
mCompoundShape->addChildShape(trans, childShape.get());
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
else
|
2023-02-25 07:06:49 +03:00
|
|
|
{
|
|
|
|
if (!mAvoidCompoundShape)
|
|
|
|
mAvoidCompoundShape.reset(new btCompoundShape);
|
|
|
|
mAvoidCompoundShape->addChildShape(trans, childShape.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ignore = childShape.release();
|
2019-08-08 17:03:11 +03:00
|
|
|
}
|
2019-08-08 13:11:24 +03:00
|
|
|
|
2013-02-20 05:35:52 +01:00
|
|
|
} // namespace NifBullet
|