⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.cpp

📁 骨骼动画....把魔兽模型解出的代码..
💻 CPP
📖 第 1 页 / 共 5 页
字号:

			Id = Node->BaseData()->ParentId;
			if(Id != INVALID_INDEX)
			{
				ParentNode = GetNode(Id);
				if(ParentNode != NULL)
				{
					Node->ParentNode.Attach(ParentNode->ChildNodes);
				}
			}
		}
	}

	for(i = 0; i < ModelData.GeosetContainer.GetTotalSize(); i++)
	{
		if(!ModelData.GeosetContainer.ValidIndex(i)) continue;

		Geoset = ModelData.GeosetContainer[i];
		for(j = 0; j < Geoset->Data().GroupContainer.GetTotalSize(); j++)
		{
			if(!Geoset->Data().GroupContainer.ValidIndex(j)) continue;

			GeosetGroup = Geoset->Data().GroupContainer[j];
			for(k = 0; k < GeosetGroup->MatrixList.GetTotalSize(); k++)
			{
				if(!GeosetGroup->MatrixList.ValidIndex(k)) continue;

				GeosetGroupNode = GeosetGroup->MatrixList[k];
				Node = GetNode(GeosetGroupNode->NodeId);
				if(Node == NULL) continue;

				GeosetGroupNode->Node.Attach(Node->MatrixListNodes);
			}
		}
	}

	for(i = 0; i < ModelData.MaterialContainer.GetTotalSize(); i++)
	{
		if(ModelData.MaterialContainer.ValidIndex(i))
		{
			Material = ModelData.MaterialContainer[i];

			CONTAINER_CONNECT_NODES(Material->Data().LayerContainer, ModelData.TextureContainer, TextureNode, MaterialLayerNodes, TextureId);
			CONTAINER_CONNECT_NODES(Material->Data().LayerContainer, ModelData.TextureAnimationContainer, TextureAnimationNode, MaterialLayerNodes, TextureAnimationId);
		}
	}

	CurrentReference = INTERPOLATOR::InterpolatorNodes.GetFirstReference();
	while(CurrentReference != NULL)
	{
		Interpolator = CurrentReference->GetData();
		Id = Interpolator->GetGlobalSequenceId();

		if(ModelData.GlobalSequenceContainer.ValidIndex(Id))
		{
			Interpolator->GlobalSequenceNode.Attach(ModelData.GlobalSequenceContainer[Id]->InterpolatorNodes);
		}

		CurrentReference = INTERPOLATOR::InterpolatorNodes.GetNextReference(CurrentReference);
	}

	CONTAINER_CONNECT_NODES(ModelData.GeosetContainer, ModelData.MaterialContainer, MaterialNode, GeosetNodes, MaterialId);
	CONTAINER_CONNECT_NODES(ModelData.EventObjectContainer, ModelData.GlobalSequenceContainer, GlobalSequenceNode, EventObjectNodes, GlobalSequenceId);
	CONTAINER_CONNECT_NODES(ModelData.BoneContainer, ModelData.GeosetContainer, GeosetNode, BoneNodes, GeosetId);
	CONTAINER_CONNECT_NODES(ModelData.BoneContainer, ModelData.GeosetAnimationContainer, GeosetAnimationNode, BoneNodes, GeosetAnimationId);
	CONTAINER_CONNECT_NODES(ModelData.GeosetAnimationContainer, ModelData.GeosetContainer, GeosetNode, GeosetAnimationNodes, GeosetId);
	CONTAINER_CONNECT_NODES(ModelData.ParticleEmitter2Container, ModelData.TextureContainer, TextureNode, ParticleEmitter2Nodes, TextureId);
	CONTAINER_CONNECT_NODES(ModelData.RibbonEmitterContainer, ModelData.MaterialContainer, MaterialNode, RibbonEmitterNodes, MaterialId);

	NodeManagerWindow.ClearAllNodes();
	InsertNode(Root);
}


//+-----------------------------------------------------------------------------
//| Wraps the pivot points into its container
//+-----------------------------------------------------------------------------
VOID MODEL::WrapPivotPoints()
{
	INT ObjectId;
	MODEL_BASE* Node;

	SAFE_CLEAR(ModelData.PivotPointContainer);

	ObjectId = 0;
	while(TRUE)
	{
		Node = GetNode(ObjectId);
		if(Node == NULL) break;

		ModelData.PivotPointContainer.Add(new D3DXVECTOR3(Node->BaseData()->PivotPoint));

		ObjectId++;
	}
}


//+-----------------------------------------------------------------------------
//| Unwraps the pivot points from its container
//+-----------------------------------------------------------------------------
VOID MODEL::UnwrapPivotPoints()
{
	INT i;
	MODEL_BASE* Node;

	for(i = 0; i < ModelData.PivotPointContainer.GetTotalSize(); i++)
	{
		if(ModelData.PivotPointContainer.ValidIndex(i))
		{
			Node = GetNode(i);
			if(Node != NULL)
			{
				Node->BaseData()->PivotPoint = (*ModelData.PivotPointContainer[i]);
			}
		}
	}
}


//+-----------------------------------------------------------------------------
//| Adds a base node
//+-----------------------------------------------------------------------------
BOOL MODEL::AddBaseNode(MODEL_BASE* Node, MODEL_BASE* ParentNode)
{
	if(Node->BaseData()->ObjectId == INVALID_INDEX)
	{
		Node->BaseData()->ObjectId = ObjectIdManager.GetNewObjectId();
	}

	if(ParentNode != NULL)
	{
		Node->ParentNode.Attach(ParentNode->ChildNodes);
	}
	else
	{
		Node->ParentNode.Attach(Root->ChildNodes);
	}

	if(!ModelData.BaseContainer.Add(Node))
	{
		Error.SetMessage("Unable to add a new node!");
		return FALSE;
	}

	NodeManagerWindow.AddNode(Node);

	return TRUE;
}


//+-----------------------------------------------------------------------------
//| Removes a base node
//+-----------------------------------------------------------------------------
BOOL MODEL::RemoveBaseNode(MODEL_BASE* Node, HWND Window)
{
	INT i;

	if(Node->ChildNodes.GetReferenceCount() > 0)
	{
		Error.SetMessage("Unable to remove node, it is still in use by a child node!");
		return FALSE;
	}

	if(Node->MatrixListNodes.GetReferenceCount() > 0)
	{
		if(MessageBox(Window, "Some geoset matrix list groups still references this bone! Detach?", "Message", MB_YESNO | MB_ICONQUESTION) == IDNO)
		{
			Error.SetMessage("Unable to remove the bone!");
			return FALSE;
		}

		Node->MatrixListNodes.Clear();
	}

	ObjectIdManager.RemoveObjectId(Node->BaseData()->ObjectId);
	NodeManagerWindow.RemoveNode(Node);

	for(i = 0; i < ModelData.BaseContainer.GetTotalSize(); i++)
	{
		if(ModelData.BaseContainer.ValidIndex(i))
		{
			if(ModelData.BaseContainer[i] == Node)
			{
				ModelData.BaseContainer.Remove(i);
				break;
			}
		}
	}

	return TRUE;
}


//+-----------------------------------------------------------------------------
//| Inserts a node and all its child nodes into the node manager
//+-----------------------------------------------------------------------------
VOID MODEL::InsertNode(MODEL_BASE* Node)
{
	REFERENCE<MODEL_BASE*, MODEL_BASE*>* CurrentNode;

	CurrentNode = Node->ChildNodes.GetFirstReference();
	while(CurrentNode != NULL)
	{
		NodeManagerWindow.AddNode(CurrentNode->GetData());
		InsertNode(CurrentNode->GetData());
		CurrentNode = Node->ChildNodes.GetNextReference(CurrentNode);
	}
}


//+-----------------------------------------------------------------------------
//| Creates a new vertex
//+-----------------------------------------------------------------------------
MODEL_GEOSET_VERTEX* MODEL::CreateVertex(CONST D3DXVECTOR3& Position, CONST D3DXVECTOR3& Normal, CONST D3DXVECTOR2& TexturePosition, INT VertexGroup)
{
	MODEL_GEOSET_VERTEX* Vertex;

	Vertex = new MODEL_GEOSET_VERTEX();
	if(Vertex == NULL)
	{
		Error.SetMessage("Unable to create a new vertex!");
		return FALSE;
	}

	Vertex->Position = Position;
	Vertex->Normal = Normal;
	Vertex->TexturePosition = TexturePosition;
	Vertex->VertexGroup = VertexGroup;

	return Vertex;
}


//+-----------------------------------------------------------------------------
//| Creates a new face
//+-----------------------------------------------------------------------------
MODEL_GEOSET_FACE* MODEL::CreateFace(INT Index1, INT Index2, INT Index3)
{
	MODEL_GEOSET_FACE* Face;

	Face = new MODEL_GEOSET_FACE();
	if(Face == NULL)
	{
		Error.SetMessage("Unable to create a new face!");
		return FALSE;
	}

	Face->Index1 = Index1;
	Face->Index2 = Index2;
	Face->Index3 = Index3;

	return Face;
}


//+-----------------------------------------------------------------------------
//| Creates a new matrix group
//+-----------------------------------------------------------------------------
MODEL_GEOSET_GROUP* MODEL::CreateGroup(CONST std::vector<INT>& MatrixList)
{
	INT i;
	MODEL_BASE* Node;
	MODEL_GEOSET_GROUP* Group;
	MODEL_GEOSET_GROUP_NODE* GroupNode;

	Group = new MODEL_GEOSET_GROUP();
	if(Group == NULL)
	{
		Error.SetMessage("Unable to create a new matrix group!");
		return FALSE;
	}

	Group->MatrixListSize = static_cast<INT>(MatrixList.size());

	for(i = 0; i < Group->MatrixListSize; i++)
	{
		GroupNode = new MODEL_GEOSET_GROUP_NODE();
		if(GroupNode == NULL)
		{
			Error.SetMessage("Unable to create a new matrix group node!");
			return FALSE;
		}

		GroupNode->NodeId = MatrixList[i];
		Group->MatrixList.Add(GroupNode);

		Node = GetNode(GroupNode->NodeId);
		if(Node != NULL) GroupNode->Node.Attach(Node->MatrixListNodes);
	}

	return Group;
}


//+-----------------------------------------------------------------------------
//| Renders all bones
//+-----------------------------------------------------------------------------
VOID MODEL::RenderBones()
{
	INT i;
	BOOL Render;
	MODEL_BASE* Node;
	MODEL_BASE* ParentNode;
	D3DXVECTOR3 From;
	D3DXVECTOR3 To;

	for(i = 0; i < ModelData.BaseContainer.GetTotalSize(); i++)
	{
		if(ModelData.BaseContainer.ValidIndex(i))
		{
			Render = TRUE;
			Node = ModelData.BaseContainer[i];

			while(TRUE)
			{
				if(Node->BaseData()->Type == NODE_TYPE_BONE) break;
				if(Node->BaseData()->Type == NODE_TYPE_HELPER) break;

				Render = FALSE;
				break;
			}

			if(!Render) continue;

			if(Node->ParentNode.IsAttached())
			{
				ParentNode = Node->ParentNode.GetObjectData();
				if(ParentNode == NULL) ParentNode = Root;
			}
			else
			{
				ParentNode = Root;
			}

			From = ParentNode->BaseData()->PivotPoint;
			To = Node->BaseData()->PivotPoint;

			if(SequenceNode.IsAttached())
			{
				D3DXVECTOR4 TempFrom;
				D3DXVECTOR4 TempTo;

				D3DXVec3Transform(&TempFrom, &From, &(ParentNode->Matrix()));
				D3DXVec3Transform(&TempTo, &To, &(Node->Matrix()));

				From.x = TempFrom.x;
				From.y = TempFrom.y;
				From.z = TempFrom.z;

				To.x = TempTo.x;
				To.y = TempTo.y;
				To.z = TempTo.z;
			}

			Graphics.RenderLine(From, To, COLOR_BONE);
		}
	}
}


//+-----------------------------------------------------------------------------
//| Renders all lights
//+-----------------------------------------------------------------------------
VOID MODEL::RenderLights()
{
	//XXXXXXXXXXXXXXXXXXXXX
}


//+-----------------------------------------------------------------------------
//| Renders all cameras
//+-----------------------------------------------------------------------------
VOID MODEL::RenderCameras()
{
	//XXXXXXXXXXXXXXXXXXXXX
}


//+-----------------------------------------------------------------------------
//| Renders all attachments
//+-----------------------------------------------------------------------------
VOID MODEL::RenderAttachments()
{
	//XXXXXXXXXXXXXXXXXXXXX
}


//+-----------------------------------------------------------------------------
//| Renders all collision shapes
//+-----------------------------------------------------------------------------
VOID MODEL::RenderCollisionShapes()
{
	INT i;
	MODEL_COLLISION_SHAPE* CollisionShape;
	std::list<D3DXVECTOR3>::iterator j;

	for(i = 0; i < ModelData.CollisionShapeContainer.GetTotalSize(); i++)
	{
		if(ModelData.CollisionShapeContainer.ValidIndex(i))
		{
			CollisionShape = ModelData.CollisionShapeContainer[i];
			switch(CollisionShape->Data().Type)
			{
				case COLLISION_SHAPE_TYPE_BOX:
				{
					D3DXVECTOR3 Corner1 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
					D3DXVECTOR3 Corner2 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

					j = CollisionShape->Data().VertexList.begin();
					if(j != CollisionShape->Data().VertexList.end())
					{
						Corner1 = (*j);

						j++;
						if(j != CollisionShape->Data().VertexList.end())
						{
							Corner2 = (*j);
						}
					}

					Graphics.RenderBox(Corner1, Corner2, COLOR_COLLISION_SHAPE);
					break;
				}

				case COLLISION_SHAPE_TYPE_SPHERE:
				{
					D3DXVECTOR3 Center = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
					FLOAT Radius;

					j = CollisionShape->Data().VertexList.begin();
					if(j != CollisionShape->Data().VertexList.end())
					{
						Center = (*j);
					}

					Radius = CollisionShape->Data().BoundsRadius;

					Graphics.RenderSphere(Center, Radius, COLOR_COLLISION_SHAPE);
					break;
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -