Пример #1
0
HRESULT D3DXMeshTransformation( LPD3DXMESH pMesh, const D3DXMATRIX* pMatrix
							   , VisitorForD3DXVECTOR3* pPointFilter)
{
	KG_PROCESS_ERROR(NULL != pMesh && NULL != pMatrix);

	{
		DWORD fvf = pMesh->GetFVF();
		KG_PROCESS_ERROR(fvf | D3DFVF_XYZ);

		DWORD dwNumBytePerVertex = pMesh->GetNumBytesPerVertex();
		_ASSERTE(dwNumBytePerVertex >= sizeof(D3DXVECTOR3));

		DWORD dwNumVertex = pMesh->GetNumVertices();

		BYTE* pBufferStart = NULL;
		_ASSERTE(sizeof(BYTE) == 1);
		

		if(NULL == pPointFilter)
		{
			HRESULT hr = pMesh->LockVertexBuffer(0, (LPVOID*)&pBufferStart);
			KG_COM_PROCESS_ERROR(hr);
			D3DXVec3TransformCoordArray((D3DXVECTOR3*)(pBufferStart), dwNumBytePerVertex
				, (D3DXVECTOR3*)pBufferStart, dwNumBytePerVertex, pMatrix, dwNumVertex);
			pMesh->UnlockVertexBuffer();
		}
		else
		{
			//加了Filter之后性能下降是当然的,但是这不是给实时用的,所以没有关系,控制
			//调用次数和调用时机就好了
			D3DXMeshVertexEnumer vertexEnumer;
			HRESULT hr = D3DXMeshCreateVertexEnumer(pMesh, vertexEnumer);
			KG_COM_PROCESS_ERROR(hr);
			_ASSERTE(vertexEnumer.IsValid());
			HRESULT hrForVisitor = E_FAIL;
			for (UINT i = 0; i < vertexEnumer.GetVertexCount(); ++i)
			{
				const D3DXVECTOR3& vTemp = vertexEnumer.GetPos(i);
				hrForVisitor = pPointFilter->Accept(vTemp);
				if (FAILED(hr))
				{
					continue;
				}

				D3DXVECTOR3 vTransformed;
				D3DXVec3TransformCoord(&vTransformed, &vTemp, pMatrix);
				vertexEnumer.SetPos(i, vTransformed);
			}
		}

		
	}		

	return S_OK;
Exit0:
	return E_FAIL;
}
Пример #2
0
/**
 * \brief Called to render a mesh
 * \param device - the Direct3D device object
 * \param meshContainerBase - the mesh container
 * \param frameBase - frame containing the mesh
 * \author Keith Ditchburn \date 18 July 2005
*/
void CXFileEntity::DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase)
{
	DWORD attrSize = 0;
	// Cast to our extended frame type
	D3DXFRAME_EXTENDED *frame = (D3DXFRAME_EXTENDED*)frameBase;		

	// Cast to our extended mesh container
	D3DXMESHCONTAINER_EXTENDED *meshContainer = (D3DXMESHCONTAINER_EXTENDED*)meshContainerBase;
	
	// Set the world transform
    m_d3dDevice->SetTransform(D3DTS_WORLD, &frame->exCombinedTransformationMatrix);

	unsigned int pass;
	if (effect) {
		effect->SetMatrix("worldmat",&frame->exCombinedTransformationMatrix);

		effect->Begin(&pass,0);
		effect->BeginPass(0);
	}

	// Loop through all the materials in the mesh rendering each subset
    for (unsigned int iMaterial = 0; iMaterial < meshContainer->NumMaterials; iMaterial++)
    {
		// use the material in our extended data rather than the one in meshContainer->pMaterials[iMaterial].MatD3D
		//m_d3dDevice->SetMaterial( &meshContainer->exMaterials[iMaterial] );
		//m_d3dDevice->SetTexture( 0, meshContainer->exTextures[iMaterial] );

		// Select the mesh to draw, if there is skin then use the skinned mesh else the normal one
		LPD3DXMESH pDrawMesh = (meshContainer->pSkinInfo) ? meshContainer->exSkinMesh: meshContainer->MeshData.pMesh;

		// Finally Call the mesh draw function
        //pDrawMesh->DrawSubset(iMaterial);

		pDrawMesh->GetVertexBuffer(&vb.vb);
		pDrawMesh->GetIndexBuffer(&ib.ib);

		//D3DVERTEXELEMENT9 pDecl[MAX_FVF_DECL_SIZE];
		//pDrawMesh->GetDeclaration(pDecl);
		//renderSystem->CreateVertexDeclaration(&pDecl[0],&vb.declaration);
		// Получение данных о количестве вершин, индексов и полигонов
		dwNumVerticies	= pDrawMesh->GetNumVertices();
		dwNumIndecies	= pDrawMesh->GetNumFaces()*3;
		dwNumFaces		= pDrawMesh->GetNumFaces();
		vb.vertexSize = (short)pDrawMesh->GetNumBytesPerVertex();

		renderSystem->DrawIndexedPrimitive(vb,0,dwNumVerticies,ib,0,dwNumFaces);
    }

	if (effect) {
		effect->EndPass();
		effect->End();
	}
}
Пример #3
0
bool Floor::ComputeBoundingBox()
{
	if( m_computedBoundingBox) return true;
	BYTE* pVertices = NULL;
	LPD3DXMESH mesh = m_mesh->GetD3DMesh();
	HRESULT hr = mesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
	if(FAILED(hr))
		return false;
	D3DXComputeBoundingBox((D3DXVECTOR3*)pVertices, mesh->GetNumVertices(), mesh->GetNumBytesPerVertex(), &m_bottomLeft, &m_topRight);
	mesh->UnlockVertexBuffer();
	m_computedBoundingBox = true;
	return true;
}
Пример #4
0
HRESULT D3DXMeshCreateVertexEnumer( LPD3DXMESH pMesh, D3DXMeshVertexEnumer& enumer )
{
	_ASSERTE(! enumer.IsValid());
	ZeroMemory(&enumer, sizeof(D3DXMeshVertexEnumer));//因为外部传入的enumer是可能重用的,这里绝对要重新清空一次

	_ASSERTE(NULL != pMesh);
	KG_PROCESS_ERROR(NULL != pMesh);
	{
		BOOL bIsAcceptableMesh = pMesh->GetFVF() & D3DFVF_XYZ;

		KG_PROCESS_ERROR(bIsAcceptableMesh && _T("不支持没有XYZ标志的Mesh,那样不能保证每个节点开头是顶点"));

		HRESULT hr = pMesh->LockVertexBuffer(0, reinterpret_cast<LPVOID*>(&enumer.m_pBuffer));
		KG_COM_PROCESS_ERROR(hr);
		_ASSERTE(1 == sizeof(BYTE));
		enumer.m_pMesh = pMesh;
		enumer.m_pMesh->AddRef();
		enumer.m_dwNumBytePerVertex = pMesh->GetNumBytesPerVertex();
		enumer.m_dwNumVertexCount = pMesh->GetNumVertices();
		return S_OK;
	}
Exit0:
	return E_FAIL;
}
Пример #5
0
HRESULT KG3DMesh::CreateBspFile()
{
    HRESULT hrResult = E_FAIL;
    HRESULT hrRetCode = E_FAIL;
    int nRetCode = false;
    TCHAR szBSPPathName[MAX_PATH];
    void *pvVerticesBuffer = NULL;
    WORD* pIndexBuffer    = NULL;
    D3DXVECTOR3 *pPos = NULL;
    DWORD *pdwFaceIndex = NULL;
    DWORD dwStride = 0;
    DWORD i = 0;
    DWORD dwNumFaces = 0;
    DWORD dwNumVertices = 0;
    DWORD dwStartTime = timeGetTime();
    LPD3DXMESH piMesh = m_ppMeshes[SMBT_NORMAL];
    KG3DBsp *pBSP = NULL;

    KGLOG_PROCESS_ERROR(piMesh);

    dwStride      = piMesh->GetNumBytesPerVertex();
    dwNumVertices = piMesh->GetNumVertices();
    dwNumFaces    = piMesh->GetNumFaces();

    KG_PROCESS_SUCCESS(dwNumFaces < 256);

    pPos = new D3DXVECTOR3[dwNumVertices];
    KG_ASSERT_EXIT(pPos);

    pdwFaceIndex = new DWORD[dwNumFaces * 3];
    KG_ASSERT_EXIT(pdwFaceIndex);

    hrRetCode = piMesh->LockVertexBuffer(D3DLOCK_READONLY, (void **)&pvVerticesBuffer);
    KGLOG_COM_PROCESS_ERROR(hrRetCode);

    hrRetCode = piMesh->LockIndexBuffer(D3DLOCK_READONLY, (void **)&pIndexBuffer);
    KGLOG_COM_PROCESS_ERROR(hrRetCode);

    for (i = 0; i < dwNumVertices; ++i)
    {
        pPos[i] = *(D3DXVECTOR3 *)(((BYTE *)pvVerticesBuffer) + dwStride * i);
    }
    for (i = 0; i < dwNumFaces * 3; ++i)
    {
        pdwFaceIndex[i] = pIndexBuffer[i];
    }

    // -------------------------- create BSP --------------------------

    hrRetCode = ChangePathExtName(m_scName.c_str(), "bsp", sizeof(szBSPPathName), szBSPPathName);
    KGLOG_COM_PROCESS_ERROR(hrRetCode);

    pBSP = new KG3DBsp;
    KGLOG_PROCESS_ERROR(pBSP);

    hrRetCode = pBSP->CreateFromMesh(dwNumVertices, dwNumFaces, pPos, pdwFaceIndex);
    KGLOG_COM_PROCESS_ERROR(hrRetCode);

    hrRetCode = pBSP->SaveToFile(szBSPPathName);
    KGLOG_COM_PROCESS_ERROR(hrRetCode);

    DWORD dwCost = timeGetTime() - dwStartTime;
    if(dwCost > 500)
    {
        KGLogPrintf(
            KGLOG_WARNING, "BSP %d %d Face %s",
            dwCost, dwNumFaces, szBSPPathName
        );
    }

    KG_DELETE(m_lpBsp); // recreate
    m_lpBsp = pBSP;
    pBSP = NULL;
Exit1:
    hrResult = S_OK;
Exit0:
    KG_DELETE(pBSP);
    if (pIndexBuffer)
    {
        piMesh->UnlockIndexBuffer();
        pIndexBuffer = NULL;
    }
    if (pvVerticesBuffer)
    {
        piMesh->UnlockVertexBuffer();
        pvVerticesBuffer = NULL;
    }
    KG_DELETE_ARRAY(pdwFaceIndex);
    KG_DELETE_ARRAY(pPos);

    if(FAILED(hrResult))
    {
        KGLogPrintf(KGLOG_ERR, "%s 创建失败", szBSPPathName);
    }
    return hrResult;
}
VOID Update()
{
	DWORD currentTime = GetTickCount();
	static DWORD preTime;
	g_ElapsedTime = currentTime - preTime;
	preTime = currentTime;

	if ( GetAsyncKeyState( VK_LEFT ) )
	{
		g_Box[0].CenterPos.x -= g_ElapsedTime*0.002f;
	}
	if ( GetAsyncKeyState( VK_RIGHT ) )
	{
		g_Box[0].CenterPos.x += g_ElapsedTime*0.002f;
	}
	if ( GetAsyncKeyState( VK_UP ) )
	{
		g_Box[0].CenterPos.y += g_ElapsedTime*0.002f;
	}
	if ( GetAsyncKeyState( VK_DOWN ) )
	{
		g_Box[0].CenterPos.y -= g_ElapsedTime*0.002f;
	}
	if ( GetAsyncKeyState( VK_LBUTTON ) )
	{
		if (g_ElapsedTime > 20)
		{
			g_Method = !g_Method;
		}
		
	}
	if ( GetAsyncKeyState( VK_HOME ) )
	{
		g_Box[1].BoxRotateZ -= 0.2f;
	}
	if ( GetAsyncKeyState( VK_END ) )
	{
		g_Box[1].BoxRotateZ += 0.2f;
	}

	D3DXVECTOR3 *vertices;

	g_pMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**) &vertices );
	D3DXComputeBoundingBox( vertices, g_pMesh->GetNumVertices(), g_pMesh->GetNumBytesPerVertex(), &g_MinPoint, &g_MaxPoint );
	g_pMesh->UnlockVertexBuffer();

	D3DXMATRIX matScale, matTrans, matRotateZ, matWorld;
	D3DXMatrixTranslation( &matTrans, g_Box[0].CenterPos.x, g_Box[0].CenterPos.y, g_Box[0].CenterPos.z );
	D3DXMatrixScaling( &matScale, g_Box[0].BoxScaling, g_Box[0].BoxScaling, g_Box[0].BoxScaling );
	D3DXMatrixRotationZ( &matRotateZ, g_Box[0].BoxRotateZ );

	matWorld = matRotateZ* matScale * matTrans;
	D3DXVec3TransformCoord( &g_Box[0].MinPoint, &g_MinPoint, &matWorld );
	D3DXVec3TransformCoord( &g_Box[0].MaxPoint, &g_MaxPoint, &matWorld );
	
	if (!g_Method)
	{
		g_CheckFlag = CheckAABBIntersection( &g_Box[0].MinPoint, &g_Box[0].MaxPoint, &g_Box[1].MinPoint, &g_Box[1].MaxPoint );
	}
	else
	{
		g_CheckFlag = CheckOBBIntersection( &g_Box[0], &g_Box[1] );
	}
	
}
HRESULT InitGeometry( )
{
	if (FAILED(D3DXCreateBox(g_pD3DDevice, 1.f, 1.f, 1.f, &g_pMesh, NULL)))
	{
		return E_FAIL;
	}

	D3DXVECTOR3 *vertices;
	
	
	g_pMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**) &vertices );
	D3DXComputeBoundingBox( vertices, g_pMesh->GetNumVertices(), g_pMesh->GetNumBytesPerVertex(), &g_MinPoint, &g_MaxPoint );
	g_pMesh->UnlockVertexBuffer();

	
	D3DXMATRIX matScale, matTrans, matRotateZ, matWorld;

	g_Box[0].BoxScaling = 1.5f;
	g_Box[0].CenterPos = D3DXVECTOR3( 0.f, 0.f, 0.f );
	g_Box[0].BoxRotateZ = 0.f;
	g_Box[0].AxisDir[0] = D3DXVECTOR3( 1, 0, 0 );
	g_Box[0].AxisDir[1] = D3DXVECTOR3( 0, 1, 0 );
	g_Box[0].AxisDir[2] = D3DXVECTOR3( 0, 0, 1 );

	for ( int i = 0; i < 3; ++i )
	{
		g_Box[0].AxisLen[i] = 0.5f;

		D3DXVec3TransformNormal( &(g_Box[0].AxisDir[i]), &(g_Box[0].AxisDir[i]), &matRotateZ );
		D3DXVec3Normalize( &( g_Box[0].AxisDir[i] ), &( g_Box[0].AxisDir[i] ) );
		g_Box[0].AxisLen[i] = g_Box[0].AxisLen[i] * g_Box[0].BoxScaling;
	}
	D3DXMatrixTranslation( &matTrans, g_Box[0].CenterPos.x, g_Box[0].CenterPos.y, g_Box[0].CenterPos.z );
	D3DXMatrixScaling( &matScale, g_Box[0].BoxScaling, g_Box[0].BoxScaling, g_Box[0].BoxScaling );
	D3DXMatrixRotationZ( &matRotateZ, g_Box[0].BoxRotateZ );

	matWorld = matRotateZ* matScale * matTrans;
	D3DXVec3TransformCoord( &g_Box[0].MinPoint, &g_MinPoint, &matWorld );
	D3DXVec3TransformCoord( &g_Box[0].MaxPoint, &g_MaxPoint, &matWorld );

	
	g_Box[1].BoxScaling = 2.f;
	g_Box[1].CenterPos = D3DXVECTOR3( 3.f, 3.f, 0.f );
	g_Box[1].BoxRotateZ = 0.f;
	g_Box[1].AxisDir[0] = D3DXVECTOR3( 1, 0, 0 );
	g_Box[1].AxisDir[1] = D3DXVECTOR3( 0, 1, 0 );
	g_Box[1].AxisDir[2] = D3DXVECTOR3( 0, 0, 1 );

	for ( int i = 0; i < 3; ++i )
	{
		g_Box[1].AxisLen[i] = 0.5f;

		D3DXVec3TransformNormal( &( g_Box[0].AxisDir[i] ), &( g_Box[1].AxisDir[i] ), &matRotateZ );
		D3DXVec3Normalize( &( g_Box[1].AxisDir[i] ), &( g_Box[1].AxisDir[i] ) );
		g_Box[1].AxisLen[i] = g_Box[1].AxisLen[i] * g_Box[1].BoxScaling;
	}

	D3DXMatrixTranslation( &matTrans, g_Box[1].CenterPos.x, g_Box[1].CenterPos.y, g_Box[1].CenterPos.z );
	D3DXMatrixScaling( &matScale, g_Box[1].BoxScaling, g_Box[1].BoxScaling, g_Box[1].BoxScaling );
	D3DXMatrixRotationZ( &matRotateZ, g_Box[1].BoxRotateZ );

	matWorld = matRotateZ* matScale * matTrans;
	D3DXVec3TransformCoord( &g_Box[1].MinPoint, &g_MinPoint, &matWorld );
	D3DXVec3TransformCoord( &g_Box[1].MaxPoint, &g_MaxPoint, &matWorld );


	return S_OK;
}
/**
* 
* PRECONDITION: parameter EFacePosition _eSide must be either FACE_TOP, or FACE_BOTTOM. 
* 
* @author Rebeccah Cox 
* @param EFacePosition _eSide - the side the flag is on, either top or bottom.
* @param int32 _iX - the position along the X axis.
* @param int32 _iY - the position along the Z axis (looks like the y axis when 
* 						looking at the flag plate). 
* @param ETeam _eTeam - team the flagplate belongs to.
* @param uint32 _uiTextureID
* @param uint32 _uiModelID
* @return bool - returns true if the initialisation was successful.
*/
bool
CFlagPlate::Initialise(EFacePosition _eSide, int32 _iX, int32 _iY, ETeam _eTeam, uint32 _uiModelID, uint32 _uiTextureID)
{
	// Set the position and side member variables in CTile.
	m_eFace = _eSide;
	m_iX = _iX;
	m_iY = _iY;

	m_vec3Position = g_atUpRightDirectionVecs[_eSide].vec3Up * 22.5f;

	m_vec3Position += g_atUpRightDirectionVecs[_eSide].vec3Right * ((_iX * 3.0f) - 21.0f);

	m_vec3Position -= g_atUpRightDirectionVecs[_eSide].vec3Direction * ((_iY * 3.0f) - 21.0f);

	// Set the world matrix using the vectors.
	m_matWorld._11 = g_atUpRightDirectionVecs[_eSide].vec3Right.x;
	m_matWorld._21 = g_atUpRightDirectionVecs[_eSide].vec3Up.x;
	m_matWorld._31 = g_atUpRightDirectionVecs[_eSide].vec3Direction.x;

	m_matWorld._12 = g_atUpRightDirectionVecs[_eSide].vec3Right.y;
	m_matWorld._22 = g_atUpRightDirectionVecs[_eSide].vec3Up.y;
	m_matWorld._32 = g_atUpRightDirectionVecs[_eSide].vec3Direction.y;

	m_matWorld._13 = g_atUpRightDirectionVecs[_eSide].vec3Right.z; 
	m_matWorld._23 = g_atUpRightDirectionVecs[_eSide].vec3Up.z; 
	m_matWorld._33 = g_atUpRightDirectionVecs[_eSide].vec3Direction.z;

	m_matWorld._41 = m_vec3Position.x;
	m_matWorld._42 = m_vec3Position.y;
	m_matWorld._43 = m_vec3Position.z;

	m_bTraversable = true;

	m_iModelID = _uiModelID;
	m_iTextureID = _uiTextureID;

	/*// Set the model ID
	if(BAD_ID == _uiModelID)
	{
		m_iModelID = CModelManager::GetInstance().CreateModel("../../models/tile_flagplate.x");
	}

	// Set the texture ID
	if(BAD_ID == _uiTextureID)
	{
		if(TEAM_GREEN == _eTeam)
		{
			m_iTextureID = CTextureManager::GetInstance().CreateTexture("../../textures/tile_flagTile_green.png");
		}
		else
		{
			m_iTextureID = CTextureManager::GetInstance().CreateTexture("../../textures/tile_flagTile_purple.png");
		}
	}*/

	D3DXVECTOR3* pFirstVertex = 0;

	LPD3DXMESH pMesh = CModelManager::GetInstance().GetModel(m_iModelID)->GetModel();

	pMesh->LockVertexBuffer(0, (void**)&pFirstVertex);

	D3DXComputeBoundingBox(pFirstVertex, 
							pMesh->GetNumVertices(),
							pMesh->GetNumBytesPerVertex(), 
							&m_tOBB.m_vec3Min, &m_tOBB.m_vec3Max);

	pMesh->UnlockVertexBuffer();

	CEntity::Initialise();

	return (true);
}
Пример #9
0
//*************************************************************************************************************
void GenerateEdges(edgeset& out, LPD3DXMESH mesh)
{
	D3DXVECTOR3	p1, p2, p3;
	D3DXVECTOR3	a, b, n;
	Edge		e;
	BYTE*		vdata		= 0;
	WORD*		idata		= 0;
	size_t		ind;
	DWORD		numindices	= mesh->GetNumFaces() * 3;
	DWORD		stride		= mesh->GetNumBytesPerVertex();
	WORD		i1, i2, i3;
	bool		is32bit		= (mesh->GetOptions() & D3DXMESH_32BIT);

	out.clear();

	if( is32bit )
	{
		MYERROR("GenerateEdges(): 4 byte indices not implemented yet");
		return;
	}

	// generate edge info
	mesh->LockIndexBuffer(D3DLOCK_READONLY, (LPVOID*)&idata);
	mesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&vdata);

	out.reserve(512);

	for( DWORD i = 0; i < numindices; i += 3 )
	{
		if( out.capacity() <= out.size() )
			out.reserve(out.size() + 1024);

		i1 = idata[i + 0];
		i2 = idata[i + 1];
		i3 = idata[i + 2];

		p1 = *((D3DXVECTOR3*)(vdata + i1 * stride));
		p2 = *((D3DXVECTOR3*)(vdata + i2 * stride));
		p3 = *((D3DXVECTOR3*)(vdata + i3 * stride));

		a = p2 - p1;
		b = p3 - p1;

		D3DXVec3Cross(&n, &a, &b);
		D3DXVec3Normalize(&n, &n);

		if( i1 < i2 )
		{
			e.i1 = i1;
			e.i2 = i2;
			e.v1 = p1;
			e.v2 = p2;
			e.n1 = n;

			if( !out.insert(e) )
				std::cout << "Crack in mesh (first triangle)\n";
		}

		if( i2 < i3 )
		{
			e.i1 = i2;
			e.i2 = i3;
			e.v1 = p2;
			e.v2 = p3;
			e.n1 = n;

			if( !out.insert(e) )
				std::cout << "Crack in mesh (first triangle)\n";
		}

		if( i3 < i1 )
		{
			e.i1 = i3;
			e.i2 = i1;
			e.v1 = p3;
			e.v2 = p1;
			e.n1 = n;

			if( !out.insert(e) )
				std::cout << "Crack in mesh (first triangle)\n";
		}
	}

	// find second triangle for each edge
	for( DWORD i = 0; i < numindices; i += 3 )
	{
		i1 = idata[i + 0];
		i2 = idata[i + 1];
		i3 = idata[i + 2];

		p1 = *((D3DXVECTOR3*)(vdata + i1 * stride));
		p2 = *((D3DXVECTOR3*)(vdata + i2 * stride));
		p3 = *((D3DXVECTOR3*)(vdata + i3 * stride));

		a = p2 - p1;
		b = p3 - p1;

		D3DXVec3Cross(&n, &a, &b);
		D3DXVec3Normalize(&n, &n);

		if( i1 > i2 )
		{
			e.i1 = i2;
			e.i2 = i1;

			ind = out.find(e);

			if( ind == edgeset::npos )
			{
				std::cout << "Lone triangle\n";
				continue;
			}

			if( out[ind].other == 0xffffffff )
			{
				out[ind].other = i / 3;
				out[ind].n2 = n;
			}
			else
				std::cout << "Crack in mesh (second triangle)\n";
		}

		if( i2 > i3 )
		{
			e.i1 = i3;
			e.i2 = i2;

			ind = out.find(e);

			if( ind == edgeset::npos )
			{
				std::cout << "Lone triangle\n";
				continue;
			}

			if( out[ind].other == 0xffffffff )
			{
				out[ind].other = i / 3;
				out[ind].n2 = n;
			}
			else
				std::cout << "Crack in mesh (second triangle)\n";
		}

		if( i3 > i1 )
		{
			e.i1 = i1;
			e.i2 = i3;

			ind = out.find(e);

			if( ind == edgeset::npos )
			{
				std::cout << "Lone triangle\n";
				continue;
			}

			if( out[ind].other == 0xffffffff )
			{
				out[ind].other = i / 3;
				out[ind].n2 = n;
			}
			else
				std::cout << "Crack in mesh (second triangle)\n";
		}
	}

	mesh->UnlockIndexBuffer();
	mesh->UnlockVertexBuffer();
}
Пример #10
0
HRESULT InitScene()
{
	HRESULT hr;
	
	D3DVERTEXELEMENT9 elem[] =
	{
		{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
		{ 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};

	SetWindowText(hwnd, TITLE);

	MYVALID(D3DXLoadMeshFromX("../media/meshes/box.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &box));
	MYVALID(D3DXLoadMeshFromX("../media/meshes/skullocc3.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &skull));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/marble.dds", &texture1));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/wood2.jpg", &texture2));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/crate.jpg", &texture3));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/pcfnoise.bmp", &noise));

	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &shadowmap, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &blurRGBA32F, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &sincoeffs, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &coscoeffs, NULL));
	MYVALID(device->CreateTexture(SHADOWMAP_SIZE, SHADOWMAP_SIZE, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &blurARGB8, NULL));
	
	MYVALID(device->CreateTexture(800, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));
	MYVALID(device->CreateVertexDeclaration(elem, &vertexdecl));

	MYVALID(DXCreateEffect("../media/shaders/exponentialshadow.fx", device, &exponential));
	MYVALID(DXCreateEffect("../media/shaders/convolutionshadow.fx", device, &convolution));
	MYVALID(DXCreateEffect("../media/shaders/varianceshadow.fx", device, &variance));
	MYVALID(DXCreateEffect("../media/shaders/expvarianceshadow.fx", device, &expvariance));
	MYVALID(DXCreateEffect("../media/shaders/boxblur5x5.fx", device, &boxblur5x5));
	MYVALID(DXCreateEffect("../media/shaders/pcfshadow5x5.fx", device, &pcf5x5));
	MYVALID(DXCreateEffect("../media/shaders/pcss.fx", device, &pcss));
	MYVALID(DXCreateEffect("../media/shaders/irregularpcf.fx", device, &pcfirreg));

	DXRenderText(
		"Use the mouse to rotate the camera and the light\n\n0 - Unfiltered\n1 - PCF (5x5)\n2 - Irregular PCF\n3 - Variance\n"
		"4 - Convolution\n5 - Exponential\n6 - Exponential variance\n7 - PCSS", text, 800, 512);
	
	cameraangle = D3DXVECTOR2(0.78f, 0.78f);
	lightangle = D3DXVECTOR2(3.2f, 0.85f);

	DXAABox			boxbb;
	DXAABox			skullbb;
	DXAABox			tmpbb;
	D3DXMATRIX		tmp1, tmp2, tmp3;
	D3DXVECTOR3*	vdata;

	box->LockVertexBuffer(D3DLOCK_READONLY, (void**)&vdata);
	D3DXComputeBoundingBox(vdata, box->GetNumVertices(), box->GetNumBytesPerVertex(), &boxbb.Min, &boxbb.Max);
	box->UnlockVertexBuffer();

	skull->LockVertexBuffer(D3DLOCK_READONLY, (void**)&vdata);
	D3DXComputeBoundingBox(vdata, skull->GetNumVertices(), skull->GetNumBytesPerVertex(), &skullbb.Min, &skullbb.Max);
	skull->UnlockVertexBuffer();

	for( int i = 0; i < numobjects; ++i )
	{
		SceneObject& obj = objects[i];

		D3DXMatrixScaling(&tmp1, obj.scale.x, obj.scale.y, obj.scale.z);
		D3DXMatrixRotationYawPitchRoll(&tmp2, obj.angles.x, obj.angles.y, obj.angles.z);
		D3DXMatrixTranslation(&tmp3, obj.position.x, obj.position.y, obj.position.z);

		D3DXMatrixMultiply(&obj.world, &tmp1, &tmp2);
		D3DXMatrixMultiply(&obj.world, &obj.world, &tmp3);

		if( obj.type == SKULL )
			tmpbb = skullbb;
		else
			tmpbb = boxbb;

		tmpbb.TransformAxisAligned(obj.world);

		scenebb.Add(tmpbb.Min);
		scenebb.Add(tmpbb.Max);
	}

	return S_OK;
}
Пример #11
0
void CShowPoints9::SetMesh(LPD3DXMESH pNewMesh, LPD3DXSKININFO pNewSkin)
{
    HRESULT hr = S_OK;

    NumPoints = 0;
    
    UnskinnedVB.resize(0);
    SAFE_RELEASE(SkinnedVB);
    SAFE_RELEASE(Skin);

    if(pNewMesh == NULL)
        return;

    IDirect3DDevice9* device = DXUTGetD3D9Device();

    {//EFFECT
     V( device->CreateVertexDeclaration( Elements, &Declaration ) );

     ID3DXBuffer* pErrors = NULL;
     V( SASCreateEffectFromResource(
             device, 
             NULL, MAKEINTRESOURCE(IDR_SHOWLINES9FX), MAKEINTRESOURCE(RT_RCDATA),
             NULL, 
             NULL, 
             0,
             NULL, 
             &Effect, 
             &pErrors));
    if(pErrors)
         DXVGetApp().OutputA( (const char*)pErrors->GetBufferPointer() );
    SAFE_RELEASE(pErrors);

    }//EFFECT



    D3DVERTEXELEMENT9 declIn[ MAX_FVF_DECL_SIZE ];
    V( pNewMesh->GetDeclaration(declIn) );

    int iPos= -1;
    int iNorm= -1;
    for( int i = 0 ; 
        declIn[i].Stream != 255 && i < MAX_FVF_DECL_SIZE;
        i++)
    {
        if(declIn[i].Usage == D3DDECLUSAGE_POSITION && declIn[i].UsageIndex == 0)
            iPos = i;
        if(declIn[i].Usage == D3DDECLUSAGE_NORMAL && declIn[i].UsageIndex == 0)
            iNorm = i;
    }

    if(iPos == -1 || iNorm == -1)
        return;

    if( (( declIn[iPos].Type & (D3DDECLTYPE_FLOAT3|D3DDECLTYPE_FLOAT4)) == 0  ) ||
        (( declIn[iNorm].Type & (D3DDECLTYPE_FLOAT3|D3DDECLTYPE_FLOAT4)) == 0  ) )
        return;

    NumPoints = pNewMesh->GetNumVertices();

    int MeshStride = pNewMesh->GetNumBytesPerVertex();


    if(pNewSkin)
    {
        V( pNewSkin->Clone( &Skin ) ); 
        V( Skin->SetDeclaration(Elements) );
    }


    //GET VERTEX DATA

    BYTE* pSrcVB= NULL;
    V(  pNewMesh->LockVertexBuffer( D3DLOCK_READONLY, (LPVOID*)&pSrcVB ) );
    UnskinnedVB.resize(pNewMesh->GetNumVertices());
    for( DWORD iVert = 0; iVert < pNewMesh->GetNumVertices(); iVert++)
    {
        Vertex& v0 = UnskinnedVB[iVert];

        v0.Position = *(D3DXVECTOR3*) (pSrcVB+(MeshStride*iVert)+declIn[iPos].Offset);
    }
    V( pNewMesh->UnlockVertexBuffer() );

    V( device->CreateVertexBuffer( NumPoints*Stride , D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &SkinnedVB, NULL) );
    //Fill in with initial values so unskinned meshes do not have to do this every render.
    pSrcVB=(BYTE*)(void*)&UnskinnedVB.front();
    BYTE* pDstVB=NULL;
    V( SkinnedVB->Lock(0, 0, (void**)&pDstVB, 0 ) );
    {
        memcpy( pDstVB, pSrcVB, Stride*pNewMesh->GetNumVertices() );
    }
    V( SkinnedVB->Unlock() );
}
Пример #12
0
bool
CTower::Initialise(EFacePosition _eSide, int32 _iX, int32 _iY, ETeam _eTeam, uint32 _uiTextureID, uint32 _uiDisabledTextureID,
				   uint32 _uiModelID, uint32 _uiTurretModelID)
{
	// Set the position and side member variables in CTile.
    m_iX = _iX;
    m_iY = _iY;
	m_eFace = _eSide;
	m_iX = _iX;
	m_iY = _iY;

	m_eTeam = _eTeam;

	m_vec3Position = g_atUpRightDirectionVecs[_eSide].vec3Up * 22.5f;

	m_vec3Position += g_atUpRightDirectionVecs[_eSide].vec3Right * ((_iX * 3.0f) - 21.0f);

	m_vec3Position -= g_atUpRightDirectionVecs[_eSide].vec3Direction * ((_iY * 3.0f) - 21.0f);

	m_bTraversable = false;

	// Set the world matrix using the vectors.
	m_matWorld._11 = g_atUpRightDirectionVecs[_eSide].vec3Right.x;
	m_matWorld._21 = g_atUpRightDirectionVecs[_eSide].vec3Up.x;
	m_matWorld._31 = g_atUpRightDirectionVecs[_eSide].vec3Direction.x;

	m_matWorld._12 = g_atUpRightDirectionVecs[_eSide].vec3Right.y;
	m_matWorld._22 = g_atUpRightDirectionVecs[_eSide].vec3Up.y;
	m_matWorld._32 = g_atUpRightDirectionVecs[_eSide].vec3Direction.y;

	m_matWorld._13 = g_atUpRightDirectionVecs[_eSide].vec3Right.z; 
	m_matWorld._23 = g_atUpRightDirectionVecs[_eSide].vec3Up.z; 
	m_matWorld._33 = g_atUpRightDirectionVecs[_eSide].vec3Direction.z;

	m_matWorld._41 = m_vec3Position.x;
	m_matWorld._42 = m_vec3Position.y;
	m_matWorld._43 = m_vec3Position.z;

	m_vec3ShootRight = g_atUpRightDirectionVecs[_eSide].vec3Right;
	m_vec3ShootDir = g_atUpRightDirectionVecs[_eSide].vec3Direction;
	m_vec3Up = g_atUpRightDirectionVecs[_eSide].vec3Up;

	// Set the turrets world matrix
	RotateTurret(3.14f, 6.283185f/ m_fTurnSpeed);
	/*m_matTurretWorld._11 = m_vec3ShootRight.x; m_matTurretWorld._21 = m_vec3Up.x; m_matTurretWorld._31 = m_vec3ShootDir.x; m_matTurretWorld._41 = m_vec3Position.x;
	m_matTurretWorld._12 = m_vec3ShootRight.y; m_matTurretWorld._22 = m_vec3Up.y; m_matTurretWorld._32 = m_vec3ShootDir.y; m_matTurretWorld._42 = m_vec3Position.y;
	m_matTurretWorld._13 = m_vec3ShootRight.z; m_matTurretWorld._23 = m_vec3Up.z; m_matTurretWorld._33 = m_vec3ShootDir.z; m_matTurretWorld._43 = m_vec3Position.z;
	*/
	m_iModelID = _uiModelID;
	m_iTextureID = _uiTextureID;
	m_uiDisabledTextureID = _uiDisabledTextureID;
	m_uiTurretModelID = _uiTurretModelID;

	D3DXVECTOR3* pFirstVertex = 0;

	LPD3DXMESH pMesh = CModelManager::GetInstance().GetModel(m_iModelID)->GetModel();

	pMesh->LockVertexBuffer(0, (void**)&pFirstVertex);

	D3DXComputeBoundingBox(pFirstVertex, 
							pMesh->GetNumVertices(),
							pMesh->GetNumBytesPerVertex(), 
							&m_tOBB.m_vec3Min, &m_tOBB.m_vec3Max);

	pMesh->UnlockVertexBuffer();

	SetCurrentShield(m_fMaxShieldStrength);

	CEntity::Initialise();

	m_eCurrentState = TOWERAI_STATE_IDLE;
	m_eNextState = TOWERAI_STATE_IDLE;
	m_pCurrentState = new CTowerIdle();
	m_pCurrentState->Initialise(this);

	// Create bounding box.
	CreateAABB();

	return (true);
}