1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 19:49:54 +00:00
openmw-tes3mp/components/nifbullet/bulletnifloader.hpp

133 lines
3.5 KiB
C++
Raw Normal View History

2013-02-24 21:52:23 +00:00
#ifndef OPENMW_COMPONENTS_NIFBULLET_BULLETNIFLOADER_HPP
#define OPENMW_COMPONENTS_NIFBULLET_BULLETNIFLOADER_HPP
2011-01-13 16:51:50 +00:00
#include <cassert>
2011-01-13 16:51:50 +00:00
#include <string>
#include <set>
#include <iostream>
#include <map>
2011-02-26 15:37:47 +00:00
#include <BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h>
#include <BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h>
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
2011-01-13 16:51:50 +00:00
#include <btBulletDynamicsCommon.h>
2015-05-07 19:17:15 +00:00
#include <osg/Matrixf>
#include <osg/BoundingBox>
#include <osg/ref_ptr>
#include <osg/Referenced>
2015-05-07 19:17:15 +00:00
#include <components/nif/niffile.hpp>
2011-01-13 16:51:50 +00:00
namespace Nif
{
class Node;
struct Transformation;
struct NiTriShape;
2011-01-13 16:51:50 +00:00
}
namespace NifBullet
{
2011-01-13 16:51:50 +00:00
class BulletShapeInstance;
class BulletShape : public osg::Referenced
{
public:
BulletShape();
virtual ~BulletShape();
btCollisionShape* mCollisionShape;
// Used for actors. Note, ideally actors would use a separate loader - as it is
// we have to keep a redundant copy of the actor model around in mCollisionShape, which isn't used.
// For now, use one file <-> one resource for simplicity.
osg::Vec3f mCollisionBoxHalfExtents;
osg::Vec3f mCollisionBoxTranslate;
// Stores animated collision shapes. If any collision nodes in the NIF are animated, then mCollisionShape
// will be a btCompoundShape (which consists of one or more child shapes).
// In this map, for each animated collision shape,
// we store the node's record index mapped to the child index of the shape in the btCompoundShape.
std::map<int, int> mAnimatedShapes;
osg::ref_ptr<BulletShapeInstance> makeInstance();
btCollisionShape* duplicateCollisionShape(btCollisionShape* shape) const;
btCollisionShape* getCollisionShape();
private:
void deleteShape(btCollisionShape* shape);
};
// An instance of a BulletShape that may have its own unique scaling set on the mCollisionShape.
// Vertex data is shallow-copied where possible. A ref_ptr to the original shape needs to be held to keep vertex pointers intact.
class BulletShapeInstance : public BulletShape
{
public:
BulletShapeInstance(osg::ref_ptr<BulletShape> source);
private:
osg::ref_ptr<BulletShape> mSource;
};
// Subclass btBhvTriangleMeshShape to auto-delete the meshInterface
struct TriangleMeshShape : public btBvhTriangleMeshShape
{
TriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression)
: btBvhTriangleMeshShape(meshInterface, useQuantizedAabbCompression)
{
}
virtual ~TriangleMeshShape()
{
delete getTriangleInfoMap();
delete m_meshInterface;
}
};
2011-01-13 16:51:50 +00:00
/**
*Load bulletShape from NIF files.
*/
class BulletNifLoader
2011-01-13 16:51:50 +00:00
{
public:
BulletNifLoader();
2013-07-31 16:46:32 +00:00
virtual ~BulletNifLoader();
2011-01-13 16:51:50 +00:00
void warn(const std::string &msg)
{
std::cerr << "NIFLoader: Warn:" << msg << "\n";
}
2011-01-13 16:51:50 +00:00
void fail(const std::string &msg)
{
std::cerr << "NIFLoader: Fail: "<< msg << std::endl;
abort();
}
2011-01-13 16:51:50 +00:00
osg::ref_ptr<BulletShape> load(const Nif::NIFFilePtr file);
2011-01-13 16:51:50 +00:00
private:
bool findBoundingBox(const Nif::Node* node, int flags = 0);
2011-01-13 16:51:50 +00:00
void handleNode(Nif::Node const *node, int flags, bool isCollisionNode, bool isAnimated=false, bool autogenerated=false);
bool hasAutoGeneratedCollision(const Nif::Node *rootNode);
2011-01-13 16:51:50 +00:00
2015-05-07 19:17:15 +00:00
void handleNiTriShape(const Nif::NiTriShape *shape, int flags, const osg::Matrixf& transform, bool isAnimated);
2011-01-13 16:51:50 +00:00
btCompoundShape* mCompoundShape;
btTriangleMesh* mStaticMesh;
std::set<std::string> mControlledNodes;
osg::ref_ptr<BulletShape> mShape;
2011-01-13 16:51:50 +00:00
};
}
2011-01-13 16:51:50 +00:00
#endif