Esempio n. 1
0
void cASEObject::setup(stASENode& nodeinfo, D3DXMATRIXA16* pmatParentWorld/* = NULL*/){
	m_stNodeInfo = nodeinfo;		
	m_matWorldTM = nodeinfo.LocalMat;
	m_matOriginalWorld = nodeinfo.LocalMat;
	m_stNodeAni = nodeinfo.AnimationInfo;
	
	if (pmatParentWorld){
		D3DXMATRIXA16 mtPrnI;
		D3DXMatrixInverse(&mtPrnI, NULL, pmatParentWorld);
		m_matLocalMat = m_matWorldTM * mtPrnI;
	}
	else {
		D3DXMatrixIdentity(&m_matLocalMat);
	}	
	
	D3DXMATRIXA16 matInverse;
	D3DXMatrixInverse(&matInverse, NULL, &m_matWorldTM);

	for (size_t i = 0; i < m_stNodeInfo.vecVertex.size(); i++){
		D3DXVec3TransformCoord(
			&m_stNodeInfo.vecVertex[i].p,
			&m_stNodeInfo.vecVertex[i].p,
			&matInverse);
		D3DXVec3TransformNormal(
			&m_stNodeInfo.vecVertex[i].n,
			&m_stNodeInfo.vecVertex[i].n,
			&matInverse
			);
	}

	D3DXCreateSphere(g_pD3DDevice, 0.025f, 10, 10, &m_pNodeMesh, NULL);

	if (m_stNodeInfo.nRef != INT_MAX){
		m_pMesh = NULL;
		HRESULT hr = D3DXCreateMeshFVF(m_stNodeInfo.vecVertex.size() / 3,
			m_stNodeInfo.vecVertex.size(),
			D3DXMESH_MANAGED,
			ST_PNT_VERTEX::FVF,
			g_pD3DDevice,
			&m_pMesh);

#ifdef _DEBUG
		_ASSERT(hr == S_OK && "Mesh Not Created");
#endif

		ST_PNT_VERTEX* pV = NULL;
		m_pMesh->LockVertexBuffer(0, (LPVOID*)&pV);
		memcpy(pV, &m_stNodeInfo.vecVertex[0], m_stNodeInfo.vecVertex.size() * sizeof(ST_PNT_VERTEX));
		m_pMesh->UnlockVertexBuffer();

		WORD* pI = NULL;
		m_pMesh->LockIndexBuffer(0, (LPVOID*)&pI);
		for (size_t j = 0; j < m_pMesh->GetNumVertices(); ++j)
		{
			pI[j] = j;
		}
		m_pMesh->UnlockIndexBuffer();

		DWORD* pA = NULL;
		m_pMesh->LockAttributeBuffer(0, &pA);
		for (size_t j = 0; j < m_pMesh->GetNumFaces(); j++){
			pA[j] = m_stNodeInfo.nRef;
		}
		m_pMesh->UnlockAttributeBuffer();

		std::vector<DWORD> vecAdjBuffer(m_stNodeInfo.vecVertex.size());
		m_pMesh->GenerateAdjacency(0.0f, &vecAdjBuffer[0]);

		m_pMesh->OptimizeInplace(
			D3DXMESHOPT_ATTRSORT |
			D3DXMESHOPT_COMPACT |
			D3DXMESHOPT_VERTEXCACHE,
			&vecAdjBuffer[0], 0, 0, 0);
	}
}
Esempio n. 2
0
void cHeightMap::Setup( char* szFolder,
	char* szRawFile, 
	char* szTextureFile, 
	int nBytesPerColor /*= 1*/ )
{
	std::string sFullPath(szFolder);
	sFullPath = sFullPath + std::string("/") + std::string(szRawFile);

	m_sTexture = std::string(szFolder) + std::string("/") + std::string(szTextureFile);

	FILE* fp = NULL;
	fopen_s(&fp, sFullPath.c_str(), "rb");

	fseek(fp, 0, SEEK_END);

	int nFileSize = ftell(fp);

	fseek(fp, 0, SEEK_SET);

	int nNumColor = nFileSize / nBytesPerColor;

	int nRowCount = (int)(sqrt((float)nNumColor) + 0.0001f);
	
	assert(nRowCount * nRowCount == nNumColor);

	int nTile = nRowCount - 1;

	m_nTile = nTile;

	std::vector<ST_PNT_VERTEX> vecVertex;

	vecVertex.reserve(nNumColor);
	
	for (int z = 0; z < nRowCount; ++z)
	{
		for (int x = 0; x < nRowCount; ++x)
		{
			float y = (float)fgetc(fp) / 10.f;

			ST_PNT_VERTEX v;
			v.p = D3DXVECTOR3(x, y, z);
			v.t = D3DXVECTOR2(x / (float)nTile, z / (float)nTile);
			v.n = D3DXVECTOR3(0, 1, 0);
			vecVertex.push_back(v);
			m_vecVertex.push_back(v.p);
		}
	}

	for (int z = 1; z < nRowCount - 1; ++z)
	{
		for (int x = 1; x < nRowCount - 1; ++x)
		{
			int nIndex = z * nRowCount + x;
			
			int left	= nIndex - 1;
			int right	= nIndex + 1;
			int up		= nIndex + nRowCount;
			int down	= nIndex - nRowCount;

			D3DXVECTOR3 lr = vecVertex[right].p - vecVertex[left].p;
			D3DXVECTOR3 du = vecVertex[up].p	- vecVertex[down].p;
			D3DXVECTOR3 n;
			D3DXVec3Cross(&n, &du, &lr);
			D3DXVec3Normalize(&n, &n);
			vecVertex[nIndex].n = n;
		}
	}

	std::vector<DWORD> vecIndex;
	vecIndex.reserve(nTile * nTile * 3 * 2);

	//		1--3
	//		|\ |
	//		| \|
	//		0--2


	for (int z = 0; z < nTile; ++z)
	{
		for (int x = 0; x < nTile; ++x)
		{
			DWORD _0 = (z + 0) * nRowCount + x + 0;
			DWORD _1 = (z + 1) * nRowCount + x + 0;
			DWORD _2 = (z + 0) * nRowCount + x + 1;
			DWORD _3 = (z + 1) * nRowCount + x + 1;

			vecIndex.push_back(_0);
			vecIndex.push_back(_1);
			vecIndex.push_back(_2);

			vecIndex.push_back(_3);
			vecIndex.push_back(_2);
			vecIndex.push_back(_1);
		}
	}

	fclose(fp);


	D3DXCreateMeshFVF(vecIndex.size() / 3,
		vecVertex.size(),
		D3DXMESH_MANAGED | D3DXMESH_32BIT,
		ST_PNT_VERTEX::FVF,
		g_pD3DDevice,
		&m_pMesh);

	ST_PNT_VERTEX* pV = NULL;
	m_pMesh->LockVertexBuffer(0, (LPVOID*)&pV);
	memcpy(pV, &vecVertex[0], vecVertex.size() * sizeof(ST_PNT_VERTEX));
	m_pMesh->UnlockVertexBuffer();

	DWORD* pI = NULL;
	m_pMesh->LockIndexBuffer(0, (LPVOID*)&pI);
	memcpy(pI, &vecIndex[0], vecIndex.size() * sizeof(DWORD));
	m_pMesh->UnlockIndexBuffer();

	DWORD* pA = NULL;
	m_pMesh->LockAttributeBuffer(0, &pA);
	ZeroMemory(pA, sizeof(DWORD) * m_pMesh->GetNumFaces());
	m_pMesh->UnlockAttributeBuffer();

	std::vector<DWORD> vecAdjBuffer(vecIndex.size());
	m_pMesh->GenerateAdjacency(0.0f, &vecAdjBuffer[0]);

	m_pMesh->OptimizeInplace(
		D3DXMESHOPT_ATTRSORT |
		D3DXMESHOPT_COMPACT |
		D3DXMESHOPT_VERTEXCACHE,
		&vecAdjBuffer[0]
	,0, 0, 0);


	ZeroMemory(&m_stMtl, sizeof(D3DMATERIAL9));

	m_stMtl.Ambient = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);
	m_stMtl.Diffuse = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);
	m_stMtl.Specular = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);

}