From bfb2d137284a08c7ae0b8edf06b5f6188789436b Mon Sep 17 00:00:00 2001 From: Shon Ferguson <shonferg@users.sourceforge.net> Date: Fri, 19 May 2006 21:27:38 +0000 Subject: [PATCH] Noticed that I had forgot to include NiObject.cpp in my last commit... unfortunately I've already started splitting off classes into their own files so that's included in this commit as well. I also noticed that I never implemented the member functions of Ref! I still haven't, but I was just surprised it's been compiling without complaint. --- NIF_Blocks.h | 2 +- niflib.cpp | 2 + niflib.h | 2 +- niflib.vcproj | 41 ++++++-- obj/NiAVObject.cpp | 7 ++ obj/NiAVObject.h | 79 ++++++++++++++ obj/NiNode.cpp | 7 ++ obj/NiNode.h | 27 +++++ obj/NiObject.cpp | 92 ++++++++++++++++ NiObject.h => obj/NiObject.h | 196 +---------------------------------- obj/NiObjectNET.cpp | 7 ++ obj/NiObjectNET.h | 36 +++++++ obj/Ref.cpp | 44 ++++++++ obj/Ref.h | 34 ++++++ xml_extract.h | 2 +- 15 files changed, 376 insertions(+), 202 deletions(-) create mode 100644 obj/NiAVObject.cpp create mode 100644 obj/NiAVObject.h create mode 100644 obj/NiNode.cpp create mode 100644 obj/NiNode.h create mode 100644 obj/NiObject.cpp rename NiObject.h => obj/NiObject.h (53%) create mode 100644 obj/NiObjectNET.cpp create mode 100644 obj/NiObjectNET.h create mode 100644 obj/Ref.cpp create mode 100644 obj/Ref.h diff --git a/NIF_Blocks.h b/NIF_Blocks.h index 4e6c48fe..a6cb046b 100644 --- a/NIF_Blocks.h +++ b/NIF_Blocks.h @@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. */ /* INCLUDES */ #include "NiObject.h" -#include "nif_math.h" +#include "obj\NiObject.h" #include "NIF_IO.h" #include <vector> diff --git a/niflib.cpp b/niflib.cpp index 4dca3a07..402947e4 100644 --- a/niflib.cpp +++ b/niflib.cpp @@ -32,6 +32,8 @@ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "niflib.h" +#include "obj/NiAVObject.h" +#include "obj/NiNode.h" #include <exception> #include <stdexcept> using namespace std; diff --git a/niflib.h b/niflib.h index 2652acce..37ea6cac 100644 --- a/niflib.h +++ b/niflib.h @@ -49,7 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include <map> #include "nif_math.h" #include "NIF_IO.h" -#include "NiObject.h" +#include "obj\NiObject.h" #include "xml_extract.h" #include "kfm.h" diff --git a/niflib.vcproj b/niflib.vcproj index 3ff59de5..0443027e 100644 --- a/niflib.vcproj +++ b/niflib.vcproj @@ -149,9 +149,22 @@ RuntimeLibrary="3"/> </FileConfiguration> </File> - <File - RelativePath=".\NiObject.cpp"> - </File> + <Filter + Name="obj" + Filter=""> + <File + RelativePath=".\obj\NiAVObject.cpp"> + </File> + <File + RelativePath=".\obj\NiNode.cpp"> + </File> + <File + RelativePath=".\obj\NiObject.cpp"> + </File> + <File + RelativePath=".\obj\NiObjectNET.cpp"> + </File> + </Filter> </Filter> <Filter Name="Header Files" @@ -166,9 +179,25 @@ <File RelativePath=".\niflib.h"> </File> - <File - RelativePath=".\NiObject.h"> - </File> + <Filter + Name="obj" + Filter=""> + <File + RelativePath=".\obj\NiAVObject.h"> + </File> + <File + RelativePath=".\obj\NiNode.h"> + </File> + <File + RelativePath=".\obj\NiObject.h"> + </File> + <File + RelativePath=".\obj\NiObjectNET.h"> + </File> + <File + RelativePath=".\obj\Ref.h"> + </File> + </Filter> </Filter> <Filter Name="Resource Files" diff --git a/obj/NiAVObject.cpp b/obj/NiAVObject.cpp new file mode 100644 index 00000000..02eb016a --- /dev/null +++ b/obj/NiAVObject.cpp @@ -0,0 +1,7 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#include "NiAVObject.h" + +//Definition of TYPE constant +const Type NiAVObject::TYPE("NiAVObject", &NiObjectNET::TYPE ); \ No newline at end of file diff --git a/obj/NiAVObject.h b/obj/NiAVObject.h new file mode 100644 index 00000000..87028c77 --- /dev/null +++ b/obj/NiAVObject.h @@ -0,0 +1,79 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#ifndef _NIAVOBJECT_H_ +#define _NIAVOBJECT_H_ + +#include "xml_extract.h" +#include "NiObjectNET.h" + +/* + * NiAVObject - An audio/video object? Part of the scene graph and has a position in 3D. + */ + +class NiAVObject; + +typedef Ref<NiAVObject> NiAVObjectRef; + +class NiAVObject : public NiObjectNET { +public: + NiAVObject() {} + ~NiAVObject() {} + //Run-Time Type Information + static const Type TYPE; + + short flags; + Vector3 localTranslate; + Matrix33 localRotate; + float localScale; + Vector3 localVelocity; + //TODO: list of NiProperty pointers. Need functions to add/remove. + //TODO: Bounding Box. What to do with newer files that have a link? Wrap this in a function and translate? + + /*! + * 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. + * \return A 4x4 transformation matrix built from the node's transform attributes. + * \sa INode::GetWorldTransform + */ + Matrix44 GetLocalTransform() const; + + /*! + * This function will return a transform matrix that represents the location of this node in world space. In other words, it concatenates all parent transforms up to the root of the scene to give the ultimate combined transform from the origin for this node. + * \return The 4x4 world transform matrix of this node. + * \sa INode::GetLocalTransform + */ + Matrix44 GetWorldTransform() const; + + /*! + * This function returns the bind position world matrix. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. + * \return The 4x4 world bind position matrix of this node. + * \sa INode::GetLocalBindPos, INode::SetWorldBindPos + */ + Matrix44 GetWorldBindPos() const; + + /*! This function returns the bind position world matrix of this node multiplied with the inverse of the bind position world matrix of its parent object if any. Thus it returns the bind position of the object in local coordinates. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. + * \return The 4x4 local bind position matrix of this node. + * \sa INode::SetWorldBindPos, INode::GetWorldBindPos + */ + Matrix44 GetLocalBindPos() const; + + /*! + * This function sets the bind position of this object relative to the origin. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. This function must be called on every object in a skin and bones system (the bones and the skinned shapes) in order for skinning information to be written to a Nif file. + * \param m The 4x4 world bind position matrix of this node + * \sa INode::GetLocalBindPos, INode::GetWorldBindPos + */ + void SetWorldBindPos( Matrix44 const & m ); + + void SetParent( NiAVObjectRef new_parent ) { + parent = new_parent; + } + NiAVObjectRef GetParent() { return parent; } + +protected: + NiAVObject * parent; + void ResetSkinnedFlag(); + Matrix44 bindPosition; + +}; + +#endif \ No newline at end of file diff --git a/obj/NiNode.cpp b/obj/NiNode.cpp new file mode 100644 index 00000000..5def7d79 --- /dev/null +++ b/obj/NiNode.cpp @@ -0,0 +1,7 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#include "NiNode.h" + +//Definition of TYPE constant +const Type NiNode::TYPE("NiNode", &NiAVObject::TYPE ); \ No newline at end of file diff --git a/obj/NiNode.h b/obj/NiNode.h new file mode 100644 index 00000000..3d6e43c4 --- /dev/null +++ b/obj/NiNode.h @@ -0,0 +1,27 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#ifndef _NINODE_H_ +#define _NINODE_H_ + +#include "NiAVObject.h" + +/* + * NiNode - A basic scene graph node. Can have children. + */ + +class NiNode; + +typedef Ref<NiNode> NiNodeRef; + +class NiNode : public NiAVObject { +public: + NiNode() {} + ~NiNode() {} + //Run-Time Type Information + static const Type TYPE; + + //TODO: Add functions to get and set children and store a list of NiObjectNET references +}; + +#endif \ No newline at end of file diff --git a/obj/NiObject.cpp b/obj/NiObject.cpp new file mode 100644 index 00000000..10d5dc56 --- /dev/null +++ b/obj/NiObject.cpp @@ -0,0 +1,92 @@ +/* Copyright (c) 2005, NIF File Format Library and Tools +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of the NIF File Format Library and Tools + project nor the names of its contributors may be used to endorse + or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. */ + +#include "NiObject.h" + +//Definition of TYPE constant +const Type NiObject::TYPE("NiObject", NULL ); + +//Static to track total number of objects in memory. Initialize to zero. +unsigned int NiObject::objectsInMemory = 0; + +/* + * Type Methods + */ + +bool Type::IsSameType( const Type & compare_to ) const { + return &compare_to == this; +} + +bool Type::IsDerivedType( const Type & compare_to ) const { + + const Type * search = this; + while ( search != NULL ) { + if ( search == &compare_to ) { + return true; + } + search = search->base_type; + } + return false; +} + +/* + * NiObject Methods + */ + +bool NiObject::IsSameType( const Type & compare_to) const { + return GetType().IsSameType( compare_to ); +} + +bool NiObject::IsSameType( const NiObject * object ) const { + return GetType().IsSameType( object->GetType() ); +} + +bool NiObject::IsDerivedType( const Type & compare_to) const { + return GetType().IsDerivedType( compare_to ); +} + +bool NiObject::IsDerivedType( const NiObject * object ) const { + return GetType().IsDerivedType( object->GetType() ); +} + +void NiObject::AddRef() { + ++_ref_count; +} + +void NiObject::SubtractRef() { + --_ref_count; + + if ( _ref_count < 1 ) { + delete this; + } +} \ No newline at end of file diff --git a/NiObject.h b/obj/NiObject.h similarity index 53% rename from NiObject.h rename to obj/NiObject.h index 2ad6ff60..f3da692e 100644 --- a/NiObject.h +++ b/obj/NiObject.h @@ -31,8 +31,8 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _INTERNAL_H_ -#define _INTERNAL_H_ +#ifndef _NIOBJECT_H_ +#define _NIOBJECT_H_ #include <iostream> #include <fstream> @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include <map> #include <vector> #include "NIF_IO.h" +#include "Ref.h" using namespace std; @@ -64,33 +65,6 @@ private: const Type * base_type; }; -/** - * Smart Pointer Template - */ -template <class T> class Ref { -public: - Ref( T * object = NULL ); - Ref(const Ref & ref_to_copy ); - - operator T*() const; - T& operator*() const; - T* operator->() const; - - Ref & operator=( T * object ); - Ref & operator=( const Ref & ref ); - - bool operator<(const Ref & ref) const { return (_object < ref._object); } - - bool operator==(T * object) const; - bool operator!=(T * object) const; - bool operator==(const Ref & ref) const; - bool operator!=(const Ref & ref) const; - -protected: - //The shared object - T* _object; -}; - /** * NiObject - Base Object class from which all other objects derive */ @@ -235,168 +209,4 @@ template <class T> const Ref<T> DynamicCast( const NiObject * object ) { } } -/* - * NiObjectNET - An object that has a name. Can have extra data and controllers attatched. - */ - -class NiObjectNET; - -typedef Ref<NiObjectNET> NiObjectNETRef; - -class NiObjectNET : public NiObject { -public: - NiObjectNET() {} - ~NiObjectNET() {} - //Run-Time Type Information - static const Type TYPE; - - string name; -private: - //TODO: pointer to extra data type... find out what that is. AExtraData right now. Need functions to add/remove. - //TODO: pointer to first NiTimeController type. Need functions to add/remove. -}; - -#include "xml_extract.h" - -/* - * NiAVObject - An audio/video object? Part of the scene graph and has a position in 3D. - */ - -class NiAVObject; - -typedef Ref<NiAVObject> NiAVObjectRef; - -class NiAVObject : public NiObjectNET { -public: - NiAVObject() {} - ~NiAVObject() {} - //Run-Time Type Information - static const Type TYPE; - - short flags; - Vector3 localTranslate; - Matrix33 localRotate; - float localScale; - Vector3 localVelocity; - //TODO: list of NiProperty pointers. Need functions to add/remove. - //TODO: Bounding Box. What to do with newer files that have a link? Wrap this in a function and translate? - - /*! - * 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. - * \return A 4x4 transformation matrix built from the node's transform attributes. - * \sa INode::GetWorldTransform - */ - Matrix44 GetLocalTransform() const; - - /*! - * This function will return a transform matrix that represents the location of this node in world space. In other words, it concatenates all parent transforms up to the root of the scene to give the ultimate combined transform from the origin for this node. - * \return The 4x4 world transform matrix of this node. - * \sa INode::GetLocalTransform - */ - Matrix44 GetWorldTransform() const; - - /*! - * This function returns the bind position world matrix. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. - * \return The 4x4 world bind position matrix of this node. - * \sa INode::GetLocalBindPos, INode::SetWorldBindPos - */ - Matrix44 GetWorldBindPos() const; - - /*! This function returns the bind position world matrix of this node multiplied with the inverse of the bind position world matrix of its parent object if any. Thus it returns the bind position of the object in local coordinates. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. - * \return The 4x4 local bind position matrix of this node. - * \sa INode::SetWorldBindPos, INode::GetWorldBindPos - */ - Matrix44 GetLocalBindPos() const; - - /*! - * This function sets the bind position of this object relative to the origin. The bind position (also called the rest position) is the position of an object in a skin and bones system before any posing has been done. This function must be called on every object in a skin and bones system (the bones and the skinned shapes) in order for skinning information to be written to a Nif file. - * \param m The 4x4 world bind position matrix of this node - * \sa INode::GetLocalBindPos, INode::GetWorldBindPos - */ - void SetWorldBindPos( Matrix44 const & m ); - - void SetParent( NiAVObjectRef new_parent ) { - parent = new_parent; - } - NiAVObjectRef GetParent() { return parent; } - -protected: - NiAVObject * parent; - void ResetSkinnedFlag(); - Matrix44 bindPosition; - -}; - -/* - * NiNode - A basic scene graph node. Can have children. - */ - -class NiNode; - -typedef Ref<NiNode> NiNodeRef; - -class NiNode : public NiAVObject { -public: - NiNode() {} - ~NiNode() {} - //Run-Time Type Information - static const Type TYPE; - - //TODO: Add functions to get and set children and store a list of NiObjectNET references -}; - - -////--Link Classes--// -// -//class Link { -//public: -// //Constructors -// Link () : _owner(NULL), index(-1) {} -// Link ( NiObject * owner) : _owner(owner), index(-1) {} -// //Destructor -// ~Link() { KillLink(); } -// void SetIndex( const int new_index ); -// void Nullify() { -// KillLink(); -// link.nullify(); -// }; -// blk_ref GetLink() const { return link; } -// void SetLink( const blk_ref & new_link ); -// void Fix( const vector<blk_ref> & blocks ); -// void SetOwner( IBlock * owner ); -//private: -// IBlock * _owner; -// blk_ref link; -// int index; -// void InitLink(); -// void KillLink(); -//}; -// -////--CrossRef Classes--// -// -//class CrossRef { -//public: -// //Constructors -// CrossRef () : _owner(NULL), ref(NULL), index(-1) {} -// CrossRef ( NiObject * owner) : _owner(owner), ref(NULL), index(-1) {} -// //Destructor -// ~CrossRef() { KillRef(); } -// void SetIndex( const int new_index ); -// void Nullify() { -// KillRef(); -// ref = NULL; -// } -// IBlock * GetCrossRef() const { return ref; } -// void SetCrossRef( NiObject * new_ref ); -// void LostRef( NiObject * match ); -// void Fix( const vector<blk_ref> & blocks ); -// void SetOwner( NiObject * owner ); -//private: -// IBlock * _owner; -// IBlock * ref; -// int index; -// void InitRef(); -// void KillRef(); -//}; - #endif \ No newline at end of file diff --git a/obj/NiObjectNET.cpp b/obj/NiObjectNET.cpp new file mode 100644 index 00000000..5c89651b --- /dev/null +++ b/obj/NiObjectNET.cpp @@ -0,0 +1,7 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#include "NiObjectNET.h" + +//Definition of TYPE constant +const Type NiObjectNET::TYPE("NiObjectNET", &NiObject::TYPE ); \ No newline at end of file diff --git a/obj/NiObjectNET.h b/obj/NiObjectNET.h new file mode 100644 index 00000000..75f95a2b --- /dev/null +++ b/obj/NiObjectNET.h @@ -0,0 +1,36 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#ifndef _NIOBJECTNET_H_ +#define _NIOBJECTNET_H_ + +#include "xml_extract.h" +#include "NiObject.h" + +/* + * NiObjectNET - An object that has a name. Can have extra data and controllers attatched. + */ + +class NiObjectNET; + +typedef Ref<NiObjectNET> NiObjectNETRef; + +class NiObjectNET : public NiObject /*NI_OBJECT_N_E_T_PARENTS*/ { +public: + NiObjectNET() /*NI_OBJECT_N_E_T_CONSTRUCT*/ {} + ~NiObjectNET() {} + //Run-Time Type Information + static const Type TYPE; + string name; + virtual void Read( istream& in, list<uint> link_stack, unsigned int version ) { /*NI_OBJECT_N_E_T_READ*/ } + virtual void Write( ostream& out, map<NiObjectRef,uint> link_map, unsigned int version ) const { /*NI_OBJECT_N_E_T_WRITE*/ } + virtual string asString( bool verbose = false ) const { /*NI_OBJECT_N_E_T_STRING*/ } + virtual void FixLinks( const vector<NiObjectRef> & objects, list<uint> link_stack, unsigned int version ) { /*NI_OBJECT_N_E_T_FIXLINKS*/ } + +private: + /*NI_OBJECT_N_E_T_MEMBERS*/ + //TODO: pointer to extra data type... find out what that is. AExtraData right now. Need functions to add/remove. + //TODO: pointer to first NiTimeController type. Need functions to add/remove. +}; + +#endif \ No newline at end of file diff --git a/obj/Ref.cpp b/obj/Ref.cpp new file mode 100644 index 00000000..93f3aa1c --- /dev/null +++ b/obj/Ref.cpp @@ -0,0 +1,44 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#include "NiNode.h" + +//Definition of TYPE constant +const Type NiNode::TYPE("NiNode", &NiAVObject::TYPE ); + +template <class T> +Ref<T>::Ref( T * object = NULL ); + +template <class T> +Ref::Ref(const Ref & ref_to_copy ); + +template <class T> +Ref::operator T*() const; + +template <class T> +T& Ref::operator*() const; + +template <class T> +T* Ref::operator->() const; + +template <class T> +Ref & Ref::operator=( T * object ); + +template <class T> +Ref & Ref::operator=( const Ref & ref ); + +template <class T> +bool Ref::operator<(const Ref & ref) const { + return (_object < ref._object); +} + +template <class T> +bool Ref::operator==(T * object) const; + +template <class T> +bool Ref::operator!=(T * object) const; + +template <class T> +bool Ref::operator==(const Ref & ref) const; + +bool Ref::operator!=(const Ref & ref) const; diff --git a/obj/Ref.h b/obj/Ref.h new file mode 100644 index 00000000..c41df19e --- /dev/null +++ b/obj/Ref.h @@ -0,0 +1,34 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for licence. */ + +#ifndef _REF_H_ +#define _REF_H_ + +/** + * Smart Pointer Template + */ +template <class T> class Ref { +public: + Ref( T * object = NULL ) {} + Ref(const Ref & ref_to_copy ); + + operator T*() const; + T& operator*() const; + T* operator->() const; + + Ref & operator=( T * object ); + Ref & operator=( const Ref & ref ); + + bool operator<(const Ref & ref) const; + + bool operator==(T * object) const; + bool operator!=(T * object) const; + bool operator==(const Ref & ref) const; + bool operator!=(const Ref & ref) const; + +protected: + //The shared object + T* _object; +}; + +#endif \ No newline at end of file diff --git a/xml_extract.h b/xml_extract.h index d4a13558..3ff0db78 100644 --- a/xml_extract.h +++ b/xml_extract.h @@ -46,7 +46,7 @@ #define _XML_EXTRACT_H_ #include "NIF_IO.h" -#include "NiObject.h" +#include "obj\NiObject.h" #include <iostream> #include <fstream> #include <vector> -- GitLab