From 17a4174505b772fa9ce0159ac0ebb36b540a75cb Mon Sep 17 00:00:00 2001 From: notpete Date: Wed, 6 Oct 2004 18:45:59 +0000 Subject: [PATCH] Minor additional functionality. This was SVN commit r1218. --- source/graphics/Model.cpp | 4 +++- source/graphics/Model.h | 21 ++++++++++++++++++++- source/graphics/ModelDef.h | 11 ++++++++--- source/graphics/Terrain.cpp | 33 +++++++++++++++++++++++++++++++++ source/graphics/Terrain.h | 4 ++++ source/graphics/UnitManager.cpp | 8 ++++++++ source/graphics/UnitManager.h | 2 ++ source/maths/MathUtil.h | 8 ++++++++ source/maths/Quaternion.cpp | 17 +++++++++++++++-- source/maths/Quaternion.h | 2 ++ 10 files changed, 103 insertions(+), 7 deletions(-) diff --git a/source/graphics/Model.cpp b/source/graphics/Model.cpp index b98288c059..b3c50176a0 100755 --- a/source/graphics/Model.cpp +++ b/source/graphics/Model.cpp @@ -18,7 +18,7 @@ ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // Constructor CModel::CModel() - : m_pModelDef(0), m_Anim(0), m_AnimTime(0), + : m_pModelDef(0), m_Flags(0), m_Anim(0), m_AnimTime(0), m_BoneMatrices(0), m_InvBoneMatrices(0), m_BoneMatricesValid(false) { } @@ -316,6 +316,7 @@ CModel* CModel::Clone() const clone->SetTexture(m_Texture); clone->SetMaterial(m_Material); clone->SetAnimation(m_Anim); + clone->SetFlags(m_Flags); for (uint i=0;iAddProp(m_Props[i].m_Point,m_Props[i].m_Model->Clone()); @@ -330,6 +331,7 @@ void CModel::SetTransform(const CMatrix3D& transform) { // call base class to set transform on this object CRenderableObject::SetTransform(transform); + m_BoneMatricesValid=false; // now set transforms on props const CMatrix3D* bonematrices=GetBoneMatrices(); diff --git a/source/graphics/Model.h b/source/graphics/Model.h index b1a9f5ab06..5b58525085 100755 --- a/source/graphics/Model.h +++ b/source/graphics/Model.h @@ -17,6 +17,7 @@ #include "SkeletonAnim.h" #include "Material.h" +#define MODELFLAG_CASTSHADOWS (1<<0) /////////////////////////////////////////////////////////////////////////////// // CModel: basically, a mesh object - holds the texturing and skinning @@ -25,6 +26,8 @@ class CModel : public CRenderableObject { public: struct Prop { + Prop() : m_Point(0), m_Model(0) {} + SPropPoint* m_Point; CModel* m_Model; }; @@ -59,6 +62,19 @@ public: // get the currently playing animation, if any CSkeletonAnim* GetAnimation() { return m_Anim; } + // set object flags + void SetFlags(u32 flags) { m_Flags=flags; } + // get object flags + u32 GetFlags() const { return m_Flags; } + + // recurse down tree setting dirty bits + void SetDirtyRec(u32 dirtyflags) { + SetDirty(dirtyflags); + for (size_t i=0;iSetDirtyRec(dirtyflags); + } + } + // calculate object space bounds of this model, based solely on vertex positions void CalcObjectBounds(); // calculate bounds encompassing all vertex positions for given animation @@ -89,7 +105,8 @@ public: // remove a prop from the given point void RemoveProp(SPropPoint* point); // return prop list - const std::vector& GetProps() { return m_Props; } + std::vector& GetProps() { return m_Props; } + const std::vector& GetProps() const { return m_Props; } // return a clone of this model CModel* Clone() const; @@ -100,6 +117,8 @@ private: // calculate necessary bone transformation matrices for skinning void GenerateBoneMatrices(); + // object flags + u32 m_Flags; // texture used by model CTexture m_Texture; // model's material diff --git a/source/graphics/ModelDef.h b/source/graphics/ModelDef.h index ffb1294865..e3d2fab628 100755 --- a/source/graphics/ModelDef.h +++ b/source/graphics/ModelDef.h @@ -94,15 +94,20 @@ public: public: // accessor: get vertex data int GetNumVertices() const { return m_NumVertices; } - SModelVertex *GetVertices() const { return m_pVertices; } + SModelVertex* GetVertices() const { return m_pVertices; } // accessor: get face data int GetNumFaces() const { return m_NumFaces; } - SModelFace *GetFaces() const { return m_pFaces; } + SModelFace* GetFaces() const { return m_pFaces; } // accessor: get bone data int GetNumBones() const { return m_NumBones; } - CBoneState *GetBones() const { return m_Bones; } + CBoneState* GetBones() const { return m_Bones; } + + + // accessor: get prop data + int GetNumPropPoints() const { return m_NumPropPoints; } + SPropPoint* GetPropPoints() const { return m_PropPoints; } // find and return pointer to prop point matching given name; return // null if no match (case insensitive search) diff --git a/source/graphics/Terrain.cpp b/source/graphics/Terrain.cpp index 59dfd246d9..0022f1ae68 100755 --- a/source/graphics/Terrain.cpp +++ b/source/graphics/Terrain.cpp @@ -331,3 +331,36 @@ void CTerrain::SetHeightMap(u16* heightmap) } } } + + +/////////////////////////////////////////////////////////////////////////////// +// FlattenArea: flatten out an area of terrain (specified in world space +// coords); return the average height of the flattened area +float CTerrain::FlattenArea(float x0,float x1,float z0,float z1) +{ + u32 tx0=u32(clamp(int(float(x0/CELL_SIZE)),0,int(m_MapSize))); + u32 tx1=u32(clamp(int(float(x1/CELL_SIZE)+1.0f),0,int(m_MapSize))); + u32 tz0=u32(clamp(int(float(z0/CELL_SIZE)),0,int(m_MapSize))); + u32 tz1=u32(clamp(int(float(z1/CELL_SIZE)+1.0f),0,int(m_MapSize))); + + u32 count=0; + u32 y=0; + for (u32 x=tx0;x<=tx1;x++) { + for (u32 z=tz0;z<=tz1;z++) { + y+=m_Heightmap[z*m_MapSize + x]; + count++; + } + } + y/=count; + + for (u32 x=tx0;x<=tx1;x++) { + for (u32 z=tz0;z<=tz1;z++) { + m_Heightmap[z*m_MapSize + x]=y; + CPatch* patch=GetPatch(x/PATCH_SIZE,z/PATCH_SIZE); + patch->SetDirty(RENDERDATA_UPDATE_VERTICES); + } + } + + return y*HEIGHT_SCALE; +} + diff --git a/source/graphics/Terrain.h b/source/graphics/Terrain.h index 58e10a5d43..2e62b356a2 100755 --- a/source/graphics/Terrain.h +++ b/source/graphics/Terrain.h @@ -61,6 +61,10 @@ public: // calculate the normal at a given vertex void CalcNormal(u32 i,u32 j,CVector3D& normal); + // flatten out an area of terrain (specified in world space coords); return + // the average height of the flattened area + float FlattenArea(float x0,float x1,float z0,float z1); + private: // delete any data allocated by this terrain void ReleaseData(); diff --git a/source/graphics/UnitManager.cpp b/source/graphics/UnitManager.cpp index fff6740203..df2f4350c5 100755 --- a/source/graphics/UnitManager.cpp +++ b/source/graphics/UnitManager.cpp @@ -46,6 +46,14 @@ void CUnitManager::RemoveUnit(CUnit* unit) } } +/////////////////////////////////////////////////////////////////////////////// +// DeleteUnit: remove given unit from world and delete it +void CUnitManager::DeleteUnit(CUnit* unit) +{ + RemoveUnit(unit); + delete unit; +} + /////////////////////////////////////////////////////////////////////////////// // DeleteAll: remove and delete all units void CUnitManager::DeleteAll() diff --git a/source/graphics/UnitManager.h b/source/graphics/UnitManager.h index faf6ccf00f..bf0d5bcbe5 100755 --- a/source/graphics/UnitManager.h +++ b/source/graphics/UnitManager.h @@ -31,6 +31,8 @@ public: void AddUnit(CUnit* unit); // remove given unit from world, but don't delete it void RemoveUnit(CUnit* unit); + // remove given unit from world and delete it + void DeleteUnit(CUnit* unit); // remove and delete all units void DeleteAll(); diff --git a/source/maths/MathUtil.h b/source/maths/MathUtil.h index 9c93f8925d..8d9f54c1ac 100755 --- a/source/maths/MathUtil.h +++ b/source/maths/MathUtil.h @@ -17,6 +17,14 @@ T Interpolate( T& a, T& b, float l ) return( a + ( b - a ) * l ); } +template +T clamp(T value,T min,T max) +{ + if (valuemax) return max; + else return value; +} + #if 0 /* diff --git a/source/maths/Quaternion.cpp b/source/maths/Quaternion.cpp index 8fc7c9c45e..049e2128ff 100755 --- a/source/maths/Quaternion.cpp +++ b/source/maths/Quaternion.cpp @@ -15,8 +15,8 @@ const float EPSILON=0.0001f; CQuaternion::CQuaternion() { - m_V.Clear (); - m_W = 0; + m_V.Clear(); + m_W = 1; } //quaternion addition @@ -202,3 +202,16 @@ void CQuaternion::FromAxisAngle(const CVector3D& axis,float angle) m_V.Z=axis.Z*sinHalfTheta; m_W=cosHalfTheta; } + + +/////////////////////////////////////////////////////////////////////////////////////////////// +// Normalize: normalize this quaternion +void CQuaternion::Normalize() +{ + float lensqrd=SQR(m_V.X)+SQR(m_V.Y)+SQR(m_V.Z)+SQR(m_W); + if (lensqrd>0) { + float invlen=1.0f/float(sqrt(lensqrd)); + m_V*=invlen; + m_W*=invlen; + } +} diff --git a/source/maths/Quaternion.h b/source/maths/Quaternion.h index 81c5ff8d56..0ab0aa8a29 100755 --- a/source/maths/Quaternion.h +++ b/source/maths/Quaternion.h @@ -42,6 +42,8 @@ public: // create a quaternion from axis/angle representation of a rotation void FromAxisAngle(const CVector3D& axis,float angle); + // normalize this quaternion + void Normalize(); }; #endif