Avoid returning pointers from NIFFile

actorid
Chris Robinson 13 years ago
parent d3a31a24ce
commit 0143cacd2b

@ -97,7 +97,7 @@ class ShapeData : public Record
{ {
public: public:
std::vector<float> vertices, normals, colors, uvlist; std::vector<float> vertices, normals, colors, uvlist;
const Vector *center; Vector center;
float radius; float radius;
void read(NIFFile *nif) void read(NIFFile *nif)
@ -388,8 +388,8 @@ public:
struct BoneInfo struct BoneInfo
{ {
const BoneTrafo *trafo; BoneTrafo trafo;
const Vector4 *unknown; Vector4 unknown;
std::vector<VertWeight> weights; std::vector<VertWeight> weights;
}; };
struct BoneInfoCopy struct BoneInfoCopy
@ -406,7 +406,7 @@ public:
unsigned int boneinfocopyindex; unsigned int boneinfocopyindex;
}; };
const BoneTrafo *trafo; BoneTrafo trafo;
std::vector<BoneInfo> bones; std::vector<BoneInfo> bones;
void read(NIFFile *nif) void read(NIFFile *nif)
@ -414,7 +414,7 @@ public:
assert(sizeof(BoneTrafo) == 4*(9+3+1)); assert(sizeof(BoneTrafo) == 4*(9+3+1));
assert(sizeof(VertWeight) == 6); assert(sizeof(VertWeight) == 6);
trafo = nif->getPtr<BoneTrafo>(); trafo = nif->getType<BoneTrafo>();
int boneNum = nif->getInt(); int boneNum = nif->getInt();
nif->getInt(); // -1 nif->getInt(); // -1
@ -424,7 +424,7 @@ public:
{ {
BoneInfo &bi = bones[i]; BoneInfo &bi = bones[i];
bi.trafo = nif->getPtr<BoneTrafo>(); bi.trafo = nif->getType<BoneTrafo>();
bi.unknown = nif->getVector4(); bi.unknown = nif->getVector4();
// Number of vertex weights // Number of vertex weights

@ -42,7 +42,7 @@ struct NiLight : Effect
Vector diffuse; Vector diffuse;
Vector specular; Vector specular;
}; };
const SLight *light; SLight light;
void read(NIFFile *nif) void read(NIFFile *nif)
{ {
@ -50,7 +50,7 @@ struct NiLight : Effect
nif->getInt(); // 1 nif->getInt(); // 1
nif->getInt(); // 1? nif->getInt(); // 1?
light = nif->getPtr<SLight>(); light = nif->getType<SLight>();
} }
}; };

@ -201,7 +201,7 @@ void NiSkinInstance::post(NIFFile *nif)
if(bnum != data->bones.size()) if(bnum != data->bones.size())
nif->fail("Mismatch in NiSkinData bone count"); nif->fail("Mismatch in NiSkinData bone count");
root->makeRootBone(data->trafo); root->makeRootBone(&data->trafo);
for(size_t i=0; i<bnum; i++) for(size_t i=0; i<bnum; i++)
{ {

@ -114,8 +114,10 @@ public:
void skip(size_t size) { inp->getPtr(size); } void skip(size_t size) { inp->getPtr(size); }
template<class X> const X* getPtr() { return (const X*)inp->getPtr(sizeof(X)); } template<class X> X getType()
template<class X> X getType() { return *getPtr<X>(); } {
return *(const X*)inp->getPtr(sizeof(X));
}
unsigned short getShort() { return getType<unsigned short>(); } unsigned short getShort() { return getType<unsigned short>(); }
int getInt() { return getType<int>(); } int getInt() { return getType<int>(); }
float getFloat() { return getType<float>(); } float getFloat() { return getType<float>(); }
@ -136,10 +138,10 @@ public:
return getArrayLen<X>(len); return getArrayLen<X>(len);
} }
const Vector *getVector() { return getPtr<Vector>(); } Vector getVector() { return getType<Vector>(); }
const Matrix *getMatrix() { return getPtr<Matrix>(); } Matrix getMatrix() { return getType<Matrix>(); }
const Transformation *getTrafo() { return getPtr<Transformation>(); } Transformation getTrafo() { return getType<Transformation>(); }
const Vector4 *getVector4() { return getPtr<Vector4>(); } Vector4 getVector4() { return getType<Vector4>(); }
std::vector<float> getFloatLen(int num) std::vector<float> getFloatLen(int num)
{ return getArrayLen<float>(num); } { return getArrayLen<float>(num); }

@ -60,7 +60,7 @@ struct Transformation
float scale; float scale;
Vector velocity; Vector velocity;
static const Transformation* getIdentity() static const Transformation& getIdentity()
{ {
static Transformation identity; static Transformation identity;
static bool iset = false; static bool iset = false;
@ -73,7 +73,7 @@ struct Transformation
iset = true; iset = true;
} }
return &identity; return identity;
} }
}; };
#pragma pack(pop) #pragma pack(pop)

@ -42,14 +42,14 @@ class Node : public Named
public: public:
// Node flags. Interpretation depends somewhat on the type of node. // Node flags. Interpretation depends somewhat on the type of node.
int flags; int flags;
const Transformation *trafo; Transformation trafo;
PropertyList props; PropertyList props;
// Bounding box info // Bounding box info
bool hasBounds; bool hasBounds;
const Vector *boundPos; Vector boundPos;
const Matrix *boundRot; Matrix boundRot;
const Vector *boundXYZ; // Box size Vector boundXYZ; // Box size
void read(NIFFile *nif) void read(NIFFile *nif)
{ {
@ -103,7 +103,7 @@ public:
void makeBone(short ind, const NiSkinData::BoneInfo &bi) void makeBone(short ind, const NiSkinData::BoneInfo &bi)
{ {
boneInfo = &bi; boneInfo = &bi;
boneTrafo = bi.trafo; boneTrafo = &bi.trafo;
boneIndex = ind; boneIndex = ind;
} }
}; };
@ -219,13 +219,13 @@ struct NiCamera : Node
// Level of detail modifier // Level of detail modifier
float LOD; float LOD;
}; };
const Camera *cam; Camera cam;
void read(NIFFile *nif) void read(NIFFile *nif)
{ {
Node::read(nif); Node::read(nif);
nif->getPtr<Camera>(); cam = nif->getType<Camera>();
nif->getInt(); // -1 nif->getInt(); // -1
nif->getInt(); // 0 nif->getInt(); // 0

@ -155,12 +155,12 @@ typedef Property NiWireframeProperty;
template <typename T> template <typename T>
struct StructPropT : Property struct StructPropT : Property
{ {
const T* data; T data;
void read(NIFFile *nif) void read(NIFFile *nif)
{ {
Property::read(nif); Property::read(nif);
data = nif->getPtr<T>(); data = nif->getType<T>();
} }
}; };

@ -226,7 +226,7 @@ void ManualBulletShapeLoader::handleNode(Nif::Node *node, int flags,
Ogre::Vector3 finalPos; Ogre::Vector3 finalPos;
float finalScale; float finalScale;
Nif::Transformation &final = *((Nif::Transformation*)node->trafo); Nif::Transformation &final = node->trafo;
Ogre::Vector3 nodePos = getVector(&final); Ogre::Vector3 nodePos = getVector(&final);
Ogre::Matrix3 nodeRot = getMatrix(&final); Ogre::Matrix3 nodeRot = getMatrix(&final);

@ -730,7 +730,7 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou
if (a) if (a)
{ {
alphaFlags = a->flags; alphaFlags = a->flags;
alphaTest = a->data->threshold; alphaTest = a->data.threshold;
} }
// Material // Material
@ -745,7 +745,7 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou
if (m) if (m)
{ {
// Use NiMaterialProperty data to create the data // Use NiMaterialProperty data to create the data
const S_MaterialProperty *d = m->data; const S_MaterialProperty *d = &m->data;
std::multimap<std::string,std::string>::iterator itr = MaterialMap.find(texName); std::multimap<std::string,std::string>::iterator itr = MaterialMap.find(texName);
std::multimap<std::string,std::string>::iterator lastElement; std::multimap<std::string,std::string>::iterator lastElement;
@ -858,17 +858,17 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou
Nif::NiSkinData::BoneInfoCopy boneinfocopy; Nif::NiSkinData::BoneInfoCopy boneinfocopy;
boneinfocopy.trafo.rotation = convertRotation(it->trafo->rotation); boneinfocopy.trafo.rotation = convertRotation(it->trafo.rotation);
boneinfocopy.trafo.trans = convertVector3(it->trafo->trans); boneinfocopy.trafo.trans = convertVector3(it->trafo.trans);
boneinfocopy.bonename = shape->skin->bones[boneIndex].name; boneinfocopy.bonename = shape->skin->bones[boneIndex].name;
boneinfocopy.bonehandle = bonePtr->getHandle(); boneinfocopy.bonehandle = bonePtr->getHandle();
copy.boneinfo.push_back(boneinfocopy); copy.boneinfo.push_back(boneinfocopy);
for (unsigned int i=0; i<it->weights.size(); i++) for (unsigned int i=0; i<it->weights.size(); i++)
{ {
vecPos = bonePtr->_getDerivedPosition() + vecPos = bonePtr->_getDerivedPosition() +
bonePtr->_getDerivedOrientation() * convertVector3(it->trafo->trans); bonePtr->_getDerivedOrientation() * convertVector3(it->trafo.trans);
vecRot = bonePtr->_getDerivedOrientation() * convertRotation(it->trafo->rotation); vecRot = bonePtr->_getDerivedOrientation() * convertRotation(it->trafo.rotation);
unsigned int verIndex = it->weights[i].vertex; unsigned int verIndex = it->weights[i].vertex;
//boneinfo.weights.push_back(*(it->weights.ptr + i)); //boneinfo.weights.push_back(*(it->weights.ptr + i));
Nif::NiSkinData::IndividualWeight ind; Nif::NiSkinData::IndividualWeight ind;
@ -959,9 +959,9 @@ void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bou
copy.boneSequence = boneSequence; copy.boneSequence = boneSequence;
// Rotate, scale and translate all the vertices, // Rotate, scale and translate all the vertices,
const Matrix &rot = shape->trafo->rotation; const Matrix &rot = shape->trafo.rotation;
const Vector &pos = shape->trafo->pos; const Vector &pos = shape->trafo.pos;
float scale = shape->trafo->scale; float scale = shape->trafo.scale;
copy.trafo.trans = convertVector3(original.pos); copy.trafo.trans = convertVector3(original.pos);
copy.trafo.rotation = convertRotation(original.rotation); copy.trafo.rotation = convertRotation(original.rotation);
@ -1148,19 +1148,19 @@ void NIFLoader::handleNode(Nif::Node *node, int flags,
parentBone->addChild(bone); parentBone->addChild(bone);
bone->setInheritOrientation(true); bone->setInheritOrientation(true);
bone->setPosition(convertVector3(node->trafo->pos)); bone->setPosition(convertVector3(node->trafo.pos));
bone->setOrientation(convertRotation(node->trafo->rotation)); bone->setOrientation(convertRotation(node->trafo.rotation));
} }
} }
} }
Transformation original = *(node->trafo); Transformation original = node->trafo;
// Apply the parent transformation to this node. We overwrite the // Apply the parent transformation to this node. We overwrite the
// existing data with the final transformation. // existing data with the final transformation.
if (trafo) if (trafo)
{ {
// Get a non-const reference to the node's data, since we're // Get a non-const reference to the node's data, since we're
// overwriting it. TODO: Is this necessary? // overwriting it. TODO: Is this necessary?
Transformation &final = *((Transformation*)node->trafo); Transformation &final = node->trafo;
// For both position and rotation we have that: // For both position and rotation we have that:
// final_vector = old_vector + old_rotation*new_vector*old_scale // final_vector = old_vector + old_rotation*new_vector*old_scale
@ -1184,7 +1184,7 @@ void NIFLoader::handleNode(Nif::Node *node, int flags,
{ {
if (list.has(i)) if (list.has(i))
handleNode(&list[i], flags, node->trafo, bounds, bone, boneSequence); handleNode(&list[i], flags, &node->trafo, bounds, bone, boneSequence);
} }
} }
else if (node->recType == RC_NiTriShape && bNiTri) else if (node->recType == RC_NiTriShape && bNiTri)

Loading…
Cancel
Save