From a1f23a366e09f4e49ce154252582eefaa1331b3e Mon Sep 17 00:00:00 2001
From: Shon Ferguson <shonferg@users.sourceforge.net>
Date: Fri, 11 Aug 2006 21:25:05 +0000
Subject: [PATCH] Fixed Matrix44 != operator Added bounding box related
 functions to NiAVObject.  Forgot to commit this earlier, so it removes the
 versions created by Tazpn. Hopefully fixed the skin calculations.  Results in
 skin files that always seem to look correct in Morrowind but do not always
 look correct in NifSkope.

---
 nif_math.cpp       |  6 +++---
 obj/NiAVObject.cpp | 33 +++++++++++----------------------
 obj/NiAVObject.h   | 31 +++++--------------------------
 obj/NiSkinData.cpp | 10 ++++++----
 4 files changed, 25 insertions(+), 55 deletions(-)

diff --git a/nif_math.cpp b/nif_math.cpp
index 91b94d57..79cc7fcb 100644
--- a/nif_math.cpp
+++ b/nif_math.cpp
@@ -362,11 +362,11 @@ bool Matrix44::operator==( const Matrix44 & rh ) const {
 bool Matrix44::operator!=( const Matrix44 & rh ) const {
 	for (int i = 0; i < 4; i++) {
 		for (int j = 0; j < 4; j++) {
-			if ( (*this)[i][j] == rh[i][j] )
-				return false;
+			if ( (*this)[i][j] != rh[i][j] )
+				return true;
 		}
 	}
-	return true;
+	return false;
 }
 
 Matrix44 Matrix44::Transpose() const {
diff --git a/obj/NiAVObject.cpp b/obj/NiAVObject.cpp
index e1464068..409e7002 100644
--- a/obj/NiAVObject.cpp
+++ b/obj/NiAVObject.cpp
@@ -189,34 +189,23 @@ void NiAVObject::SetVisibility( bool n ) {
 	}
 }
 
-bool NiAVObject::GetHasBoundingBox() const {
+bool NiAVObject::HasBoundingBox() const {
 	return hasBoundingBox;
 }
 
-void NiAVObject::SetHasBoundingBox( bool value ) {
-	hasBoundingBox = value;
+void NiAVObject::ClearBoundingBox() {
+	hasBoundingBox = false;
 }
 
 BoundingBox NiAVObject::GetBoundingBox() const {
-	return boundingBox;
-}
-
-void NiAVObject::SetBoundingBox( const BoundingBox & value ) {
-	boundingBox = value;
-}
-
-Ref<NiCollisionData > NiAVObject::GetCollisionData() const {
-	return collisionData;
-}
-
-void NiAVObject::SetCollisionData( Ref<NiCollisionData > value ) {
-	collisionData = value;
-}
-
-Ref<NiCollisionObject > NiAVObject::GetCollisionObject() const {
-	return collisionObject;
+	if ( hasBoundingBox == true ) {
+		return boundingBox;
+	} else {
+		throw runtime_error("This NIAVObject has no Bounding Box.");
+	}
 }
 
-void NiAVObject::SetCollisionObject( Ref<NiCollisionObject > value ) {
-	collisionObject = value;
+void NiAVObject::SetBoundingBox( const BoundingBox & n ) {
+	boundingBox = n;
+	hasBoundingBox = true;
 }
diff --git a/obj/NiAVObject.h b/obj/NiAVObject.h
index ec2b3a29..55e37a30 100644
--- a/obj/NiAVObject.h
+++ b/obj/NiAVObject.h
@@ -40,7 +40,11 @@ public:
 	virtual void FixLinks( const vector<NiObjectRef> & objects, list<uint> & link_stack, unsigned int version, unsigned int user_version );
 	virtual list<NiObjectRef> GetRefs() const;
 
-	//TODO:  Bounding Box.  What to do with newer files that have a link?  Wrap this in a function and translate?
+	//TODO:  What to do with newer files that have a link for a bounding box?  Wrap this in a function and translate?
+	void ClearBoundingBox();
+	BoundingBox GetBoundingBox() const;
+	void SetBoundingBox( const BoundingBox & n );
+	bool HasBoundingBox() const;
 	
 	/*! 
 	 * This is a conveniance function that allows you to retrieve the full 4x4 matrix transform of a node.  It accesses the "Rotation," "Translation," and "Scale" attributes and builds a complete 4x4 transformation matrix from them.
@@ -92,31 +96,6 @@ public:
 	bool GetVisibility() const;
 	void SetVisibility( bool n );
 
-	/*!
-	 * Gets whether there is a bounding box associated with this object.
-    * \return True if there is a bounding box.
-    * \sa NiAVObject::SetHasBoundingBox
-	 */
-	bool GetHasBoundingBox() const;
-
-   /*!
-   * Assigns whether there is a bounding box associated with this object.
-   * \sa NiAVObject::GetHasBoundingBox
-   */
-	void SetHasBoundingBox( bool value );
-
-	/*!
-	 * The bounding box.
-	 */
-	BoundingBox GetBoundingBox() const;
-	void SetBoundingBox( const BoundingBox & value );
-
-	/*!
-	 * Bounding box: refers to NiCollisionData
-	 */
-	Ref<NiCollisionData > GetCollisionData() const;
-	void SetCollisionData( Ref<NiCollisionData> value );
-
 	/*!
 	 * In Oblivion this links the havok objects.
 	 */
diff --git a/obj/NiSkinData.cpp b/obj/NiSkinData.cpp
index a5cf7408..0bf29e75 100644
--- a/obj/NiSkinData.cpp
+++ b/obj/NiSkinData.cpp
@@ -94,10 +94,11 @@ NiSkinData::NiSkinData( const Ref<NiTriBasedGeom> & owner ) {
 	Matrix44 owner_mat = owner->GetWorldTransform();
 
 	//Get Skeleton root world transform
-	Matrix44 skel_root_mat = skinInst->GetSkeletonRoot()->GetWorldTransform();
+	Matrix44 sr_world = skinInst->GetSkeletonRoot()->GetWorldTransform();
 
 	//Inverse owner NiTriBasedGeom matrix & multiply with skeleton root matrix
-	Matrix44 res_mat = owner_mat.Inverse() * skel_root_mat;
+	Matrix44 res_mat = owner_mat.Inverse() * sr_world;
+	//Matrix44 res_mat = (sr_world.Inverse() * owner_mat).Inverse() * sr_world;
 
 	//Store result
 	res_mat.Decompose( translation, rotation, scale );
@@ -111,12 +112,13 @@ NiSkinData::NiSkinData( const Ref<NiTriBasedGeom> & owner ) {
 		//Get bone world position
 		bone_mat = bone_nodes[i]->GetWorldTransform();
 
+		bone_mat = bone_mat * sr_world;
+
 		//Multiply NiTriBasedGeom matrix with inversed bone matrix
 		res_mat = owner_mat * bone_mat.Inverse();
 
+
 		//Store result
 		res_mat.Decompose( boneList[i].translation, boneList[i].rotation, boneList[i].scale );
-
-		//TODO:  Calculate center and radius of each bone
 	}
 }
\ No newline at end of file
-- 
GitLab