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

📄 attachment.cpp

📁 3D游戏展示程序
💻 CPP
字号:
//--------------------------------------------------
//  Desc: M2 Attachment
//  From: WOW Model Viewer
//  I just coding in my own ways!
//--------------------------------------------------

#include "M2Loader.h"
#include "Attachment.h"
#include "Common.h"

Attachment::Attachment()
{
	m_pParent = NULL;
	m_pModel = NULL;
	m_BoneID = -1;
	m_Slot = -1;

	m_Scale = 1.0f;
	m_Rotate = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}

Attachment::~Attachment()
{
	ReleaseChildren();

	m_pParent = NULL;
	if(m_pModel)
	{
		delete m_pModel;
		m_pModel = NULL;
	}
}

bool Attachment::Load(Attachment *pParent, CM2Loader *pModel, int BoneId, int Slot)
{
	m_pParent = pParent;
	m_pModel = pModel;
	m_BoneID = BoneId;
	m_Slot = Slot;
	return true;
}

void Attachment::Update(void)
{
	if(m_pModel)
	{
		m_pModel->UpdateAnimation();
	}
	for(size_t i=0; i<m_Children.size(); i++)
		m_Children[i]->Update();
}

void Attachment::Render(void)
{
	D3DXMATRIX matWorld;
	g_pD3DDevice->GetTransform(D3DTS_WORLD, &matWorld);
	if(m_pModel)
	{
		D3DXMATRIX matTemp;
		D3DXMATRIX matRetsult;
		D3DXMatrixIdentity(&matRetsult);
		if(m_Scale != 1.0f)
		{
			D3DXMatrixScaling(&matTemp, m_Scale, m_Scale, m_Scale);
			matRetsult *= matTemp;
		}
		if(m_Rotate != D3DXVECTOR3(0.0f, 0.0f, 0.0f))
		{
			D3DXMatrixRotationX(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
			D3DXMatrixRotationY(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
			D3DXMatrixRotationZ(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
		}
		if(m_Position != D3DXVECTOR3(0.0f, 0.0f, 0.0f))
		{
			D3DXMatrixTranslation(&matTemp, m_Position.x, m_Position.y, m_Position.z);
			matRetsult *= matTemp;
		}
		if(m_pParent)
		{
			if(m_pParent->m_pModel)
			{
				matRetsult *= m_pParent->m_pModel->GetAttachmentMatrix(m_BoneID);
			}
		}
		
		matRetsult *= matWorld;
		g_pD3DDevice->SetTransform(D3DTS_WORLD, &matRetsult);
		
		m_pModel->Render();
	}

	for(size_t i=0; i<m_Children.size(); i++)
		m_Children[i]->Render();
	g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}

void Attachment::SetRender(bool bRender)
{
	if(m_pModel)
	{
		m_pModel->SetRender(bRender);
	}

	for(size_t i=0; i<m_Children.size(); i++)
		m_Children[i]->SetRender(bRender);
}

void Attachment::UpdateParticle(void)
{
	if(m_pModel && m_pModel->HasParticle())
	{
		m_pModel->UpdatePaticle();
	}
	for(size_t i=0; i<m_Children.size(); i++)
		m_Children[i]->UpdateParticle();
}

void Attachment::RenderParticle(const D3DXMATRIX &matRotate)
{
	D3DXMATRIX matWorld;
	g_pD3DDevice->GetTransform(D3DTS_WORLD, &matWorld);
	if(m_pModel && m_pModel->HasParticle())
	{
		D3DXMATRIX matTemp;
		D3DXMATRIX matRetsult;
		D3DXMatrixIdentity(&matRetsult);
		if(m_Scale != 1.0f)
		{
			D3DXMatrixScaling(&matTemp, m_Scale, m_Scale, m_Scale);
			matRetsult *= matTemp;
		}
		if(m_Rotate != D3DXVECTOR3(0.0f, 0.0f, 0.0f))
		{
			D3DXMatrixRotationX(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
			D3DXMatrixRotationY(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
			D3DXMatrixRotationZ(&matTemp, m_Rotate.x);
			matRetsult *= matTemp;
		}
		if(m_Position != D3DXVECTOR3(0.0f, 0.0f, 0.0f))
		{
			D3DXMatrixTranslation(&matTemp, m_Position.x, m_Position.y, m_Position.z);
			matRetsult *= matTemp;
		}
		if(m_pParent)
		{
			if(m_pParent->m_pModel)
			{
				matRetsult *= m_pParent->m_pModel->GetAttachmentMatrix(m_BoneID);
			}
		}
		matRetsult *= matWorld;
		g_pD3DDevice->SetTransform(D3DTS_WORLD, &matRetsult);

		m_pModel->RenderParticle(matRotate);
	}
	for(size_t i=0; i<m_Children.size(); i++)
		m_Children[i]->RenderParticle(matRotate);
	g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}

void Attachment::ReleaseChildren(void)
{
	for(size_t i=0; i<m_Children.size(); i++)
	{
		m_Children[i]->ReleaseChildren();
		// m_pModel在传入参数外分配内存
		// m_Children[i]在此类内分配内存
		if(m_Children[i]->m_pModel)
		{
			delete m_Children[i]->m_pModel;
			m_Children[i]->m_pModel = NULL;
		}
		delete (m_Children[i]);
		m_Children[i] = NULL;
	}

	m_Children.clear();
}

void Attachment::SetScale(float scale)
{
	m_Scale = scale;
}

void Attachment::SetRotate(const D3DXVECTOR3 &rotate)
{
	m_Rotate = rotate;
}

void Attachment::SetPosition(const D3DXVECTOR3 &pos)
{
	m_Position = pos;
}

Attachment* Attachment::AddChild(const char *filename, int id, int slot, float scale /* = 1.0f */, 
								 D3DXVECTOR3 rotate /* = D3DXVECTOR3(0.0f, 0.0f, 0.0f) */,
								 D3DXVECTOR3 pos /* = D3DXVECTOR3(0.0f, 0.0f, 0.0f) */)
{
	if(filename==NULL || filename[0]==0 || id<0)
		return NULL;

	CM2Loader *pModel = new CM2Loader;
	if(pModel==NULL)	return NULL;
	if(pModel->Load(filename))
	{
		return AddChild(pModel, id, slot, scale, rotate, pos);
	}
	else
	{
		delete pModel;
		return NULL;
	}
}

Attachment* Attachment::AddChild(CM2Loader *model, int id, int slot, float scale /* = 1.0f */, 
								 D3DXVECTOR3 rotate /* = D3DXVECTOR3(0.0f, 0.0f, 0.0f) */,
								 D3DXVECTOR3 pos /* = D3DXVECTOR3(0.0f, 0.0f, 0.0f) */)
{
	if(model == NULL)	return NULL;

	Attachment *pAttach = new Attachment();
	if(pAttach == NULL)	return NULL;

	pAttach->Load(this, model, id, slot);
	pAttach->SetScale(scale);
	pAttach->SetRotate(rotate);
	pAttach->SetPosition(pos);
	m_Children.push_back(pAttach);
	return pAttach;
}

void Attachment::ReleaseSlot(int slot)
{
	for(size_t i=0; i<m_Children.size(); i++)
	{
		if(m_Children[i]->m_Slot == slot)
		{
			delete m_Children[i];
			m_Children.erase(m_Children.begin() + i);
			continue;
		}
	}
}




⌨️ 快捷键说明

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