Exemplo n.º 1
0
void CShadow::onTick(float fElapsedTime) {
	for (unsigned int i(0); i < m_vMesh.size(); i++) {
		D3DXVECTOR3 vSun(*m_pSun); D3DXMATRIX mInv;
		D3DXMatrixInverse(&mInv, NULL, m_vMat[i]);
		D3DXVec3TransformCoord(&vSun, &vSun, &mInv);

		ID3DXMesh* pMesh;
		m_vMesh[i]->CloneMeshFVF(m_vMesh[i]->GetOptions(), D3DFVF_XYZ, 
			CDirect3D::getInstance()->GetD3D9Device(), &pMesh);

		D3DXVECTOR3 *pVertices, *pVolume;
		WORD* pIndices;
		pMesh->LockVertexBuffer(NULL, (void**) &pVertices);
		pMesh->LockIndexBuffer(NULL, (void**) &pIndices);
		DWORD dwFaces(pMesh->GetNumFaces());

		WORD* pEdges = new WORD[dwFaces*3*2];
		DWORD dwEdges(0);
		for (DWORD j(0); j < dwFaces; j++) {
			WORD wFace0 = pIndices[j*3];
			WORD wFace1 = pIndices[j*3+1];
			WORD wFace2 = pIndices[j*3+2];
			D3DXVECTOR3 v0 = pVertices[wFace0];
			D3DXVECTOR3 v1 = pVertices[wFace1];
			D3DXVECTOR3 v2 = pVertices[wFace2];
			D3DXVECTOR3 vCross1(v2-v1);
			D3DXVECTOR3 vCross2(v1-v0);
			D3DXVECTOR3 vNormal;
			D3DXVec3Cross(&vNormal, &vCross1, &vCross2);
			if (D3DXVec3Dot(&vNormal, m_pSun) > 0.f) {
				AddEdge(pEdges, dwEdges, wFace0, wFace1);
				AddEdge(pEdges, dwEdges, wFace1, wFace2);
				AddEdge(pEdges, dwEdges, wFace2, wFace0);
			}
		}
		m_dwTriangles[i] = dwEdges*2;
		m_vVB[i]->Lock(0, sizeof(D3DXVECTOR3)*m_dwTriangles[i]*3, (void**) &pVolume, NULL);
		// some (solvable, but expensive) issues here: 
		// (view port related) direction undetermined for segment v1 - v2,
		// so CULL_CW doesn't necessarily mean 'back side'.
		for (DWORD j(0); j < dwEdges; j++) {
			D3DXVECTOR3 v1 = pVertices[pEdges[2*j+0]];	
			D3DXVECTOR3 v2 = pVertices[pEdges[2*j+1]];  
			D3DXVECTOR3 v3 = (v1 - vSun);				
			D3DXVECTOR3 v4 = (v2 - vSun);
			pVolume[j*6] = v3;		
			pVolume[j*6+1] = v1;	
			pVolume[j*6+2] = v4;	// 1 2
			pVolume[j*6+3] = v4;	// |\|
			pVolume[j*6+4] = v1;	// 3 4
			pVolume[j*6+5] = v2;
		}
		m_vVB[i]->Unlock();
		pMesh->UnlockVertexBuffer();
		pMesh->UnlockIndexBuffer();
		Safe_Release(pMesh);
		Safe_Delete_Array(pEdges);
	}
}
Exemplo n.º 2
0
BOOL RenderPick::Init(UINT width, UINT height, HWND hwnd, BOOL windowed, D3DDEVTYPE devType) {
    HRESULT hr = Render::Init(width, height, hwnd, windowed, devType);
    SGL_FAILED_DO(hr, MYTRACE_DX("Render::Init", hr); return FALSE);

    // Create the teapot.
    ID3DXMesh* teapot;
    D3DXCreateTeapot(m_D3DDev, &teapot, NULL);
    m_Teapot.Attach(teapot);

    // Compute the bounding sphere.
    BYTE* v = 0;
    teapot->LockVertexBuffer(0, (void**) &v);

    D3DXComputeBoundingSphere((D3DXVECTOR3*) v, teapot->GetNumVertices(), D3DXGetFVFVertexSize(teapot->GetFVF()), &m_BSphere.center, &m_BSphere.radius);
    teapot->UnlockVertexBuffer();

    // Build a sphere mesh that describes the teapot's bounding sphere.
    ID3DXMesh* sphere;
    D3DXCreateSphere(m_D3DDev, m_BSphere.radius, 20, 20, &sphere, NULL);
    m_Sphere.Attach(sphere);

    // Set light.
    D3DXVECTOR3 dir(0.707f, -0.0f, 0.707f);
    D3DXCOLOR clr(1.0f, 1.0f, 1.0f, 1.0f);
    D3DLIGHT9 light;
    SGL::InitDirLight(&light, dir, clr);

    m_D3DDev->SetLight(0, &light);
    m_D3DDev->LightEnable(0, TRUE);
    m_D3DDev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
    m_D3DDev->SetRenderState(D3DRS_SPECULARENABLE, FALSE);

    // Set view matrix.
    D3DXVECTOR3 pos(0.0f, 0.0f, -10.0f);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);

    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &pos, &target, &up);
    m_D3DDev->SetTransform(D3DTS_VIEW, &V);

    // Set projection matrix.
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.25f, (float) width / (float) height, 1.0f, 1000.0f);
    m_D3DDev->SetTransform(D3DTS_PROJECTION, &proj);

    // Setup a basic scene.
    m_BasicScene.reset(new BasicScene(m_D3DDev));
    if (!m_BasicScene->Init())
        return FALSE;

    if (!InitFont())
        return FALSE;

    return TRUE;
}
Exemplo n.º 3
0
void Cone3D::buildTexCoords(IDirect3DDevice9* gd3dDevice)
{
	D3DVERTEXELEMENT9 elements[64];
	UINT numElements = 0;
	VertexPos::Decl->GetDeclaration(elements, &numElements);

	ID3DXMesh* temp = 0;
	HR(mpMesh->CloneMesh(D3DXMESH_SYSTEMMEM,
		elements, gd3dDevice, &temp));

	ReleaseCOM(mpMesh);

	// Now generate texture coordinates for each vertex.
	VertexPos* vertices = 0;
	HR(temp->LockVertexBuffer(0, (void**)&vertices));

	D3DXVECTOR3 maxPoint(-FLT_MAX, -FLT_MAX, -FLT_MAX);
	D3DXVECTOR3 minPoint(FLT_MAX, FLT_MAX, FLT_MAX);

	for (UINT i = 0; i < temp->GetNumVertices(); ++i)
	{
		D3DXVec3Maximize(&maxPoint, &maxPoint, &vertices[i].mPos);
		D3DXVec3Minimize(&minPoint, &minPoint, &vertices[i].mPos);
	}

	float a = minPoint.z;
	float b = maxPoint.z;
	float h = b - a;

	for (int i = 0; i < (int)temp->GetNumVertices(); i++)
	{
		float x = vertices[i].mPos.x;
		float y = vertices[i].mPos.z;
		float z = vertices[i].mPos.y;

		float theta = atan2f(z, x);
		float y2 = y - b;

		float u = theta / (2.0f * D3DX_PI);
		float v = y2 / -h;

		vertices[i].mTexCoord.x = u;
		vertices[i].mTexCoord.y = v;
	}

	HR(temp->UnlockVertexBuffer());

	HR(temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY,
		elements, gd3dDevice, &mpMesh));

	ReleaseCOM(temp);
}
Exemplo n.º 4
0
void SolarSysDemo::genSphericalTexCoords()
{
	// D3DXCreate* functions generate vertices with position 
	// and normal data.  But for texturing, we also need
	// tex-coords.  So clone the mesh to change the vertex
	// format to a format with tex-coords.

	D3DVERTEXELEMENT9 elements[64];
	UINT numElements = 0;
	VertexPNT::Decl->GetDeclaration(elements, &numElements);

	ID3DXMesh* temp = 0;
	HR(mSphere->CloneMesh(D3DXMESH_SYSTEMMEM, 
		elements, gd3dDevice, &temp));

	ReleaseCOM(mSphere);

	// Now generate texture coordinates for each vertex.
	VertexPNT* vertices = 0;
	HR(temp->LockVertexBuffer(0, (void**)&vertices));

	for(UINT i = 0; i < temp->GetNumVertices(); ++i)
	{
		// Convert to spherical coordinates.
		D3DXVECTOR3 p = vertices[i].pos;
	
 
		float theta = atan2f(p.z, p.x);
		float phi   = acosf(p.y / sqrtf(p.x*p.x+p.y*p.y+p.z*p.z));

		// Phi and theta give the texture coordinates, but are not in 
		// the range [0, 1], so scale them into that range.

		float u = theta / (2.0f*D3DX_PI);
		float v = phi   / D3DX_PI;
		
		// Save texture coordinates.
		
		vertices[i].tex0.x = u;
		vertices[i].tex0.y = v;
	}
	HR(temp->UnlockVertexBuffer());

	// Clone back to a hardware mesh.
	HR(temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY,
		elements, gd3dDevice, &mSphere));

	ReleaseCOM(temp);
}
Exemplo n.º 5
0
bool XFileUtils::LoadMesh(
	ID3DXFileData*		pFileData, 
	IDirect3DDevice9*	pDevice, 
	ID3DXMesh**			pNewMesh, 
	ID3DXBuffer**		pMaterial,
	DWORD*				pNumAttribute,
	GSkinInfo**			pNewSkininfo)
{
	//检查类型
	_XCheckType(pFileData, "Mesh", false);
	DWORD nVertices = 0;
	DWORD nFaces = 0;
	_XCheckExcute(ParseBasemesh(pFileData, &nVertices, &nFaces, 
		nullptr, nullptr, sizeof(XVertex_p3_n3_t1)));
	ID3DXMesh* pMesh = nullptr;
	if (FAILED(D3DXCreateMeshFVF(nFaces, nVertices, 
		D3DXMESH_32BIT | D3DXMESH_MANAGED, XVertex_p3_n3_t1::fvf, pDevice, &pMesh)))
		return false;
	void* pVertices = nullptr;
	DWORD* pIndices = nullptr;
	DWORD* pAttribtues = nullptr;
	pMesh->LockVertexBuffer(0, &pVertices);
	pMesh->LockIndexBuffer(0, (void**)&pIndices);
	pMesh->LockAttributeBuffer(0, &pAttribtues);
	_XCheckExcute(ParseBasemesh(pFileData, &nVertices, &nFaces, 
		pVertices, pIndices, sizeof(XVertex_p3_n3_t1)));

	_XCheckExcute(ParseNormals(pFileData, nVertices, pVertices, pIndices,
		sizeof(XVertex_p3_n3_t1), sizeof(D3DXVECTOR3)));

	_XCheckExcute(ParseTexcoord(pFileData, pVertices,
		sizeof(XVertex_p3_n3_t1), sizeof(D3DXVECTOR3) * 2));

	_XCheckExcute(ParseMaterial(pFileData, pAttribtues, nFaces, pNumAttribute, pMaterial));

	pMesh->UnlockAttributeBuffer();
	pMesh->UnlockIndexBuffer();
	pMesh->UnlockVertexBuffer();

	*pNewMesh = pMesh;
	
	ParseSkinInfo(pFileData, pNewSkininfo);
	return true;
}
Exemplo n.º 6
0
void GWater::recreateGraphInfo()
{
	HRESULT hr = S_FALSE;

	SeaVertex *pVertextBuffer = NULL;	  //Mesh的顶点缓冲区

	DWORD *pIndexBuffer = NULL;	  //Mesh的索引缓冲区
	ID3DXMesh* mesh = 0;
	hr = D3DXCreateMeshFVF (
		mCellCount * mCellCount * 2,
		( mCellCount + 1 ) * ( mCellCount + 1 ),
		D3DXMESH_32BIT | D3DXMESH_MANAGED, FVFSea,  Content::Device.getD9Device(),
		&mesh
		);

	dDebugMsgBox ( hr, "创建海面Mesh失败!" );

	DWORD dwIndex = 0;

	mesh->LockVertexBuffer ( D3DLOCK_DISCARD, ( void** ) &pVertextBuffer );

	for ( int i = 0; i < mCellCount + 1; i++ )
	{
		for ( int j = 0; j < mCellCount + 1; j++ )
		{
			dwIndex = i * ( mCellCount + 1 ) + j;

			pVertextBuffer[dwIndex].vertex.x = ( j - mCellCount / 2.0f ) * mCellWidth;
			pVertextBuffer[dwIndex].vertex.y = 0;
			pVertextBuffer[dwIndex].vertex.z = ( i - mCellCount / 2.0f ) * mCellWidth;
			pVertextBuffer[dwIndex].u = j / 10.0f;
			pVertextBuffer[dwIndex].v = ( mCellCount - i ) / 10.0f;
		}
	}

	mesh->UnlockVertexBuffer();

	mesh->LockIndexBuffer ( D3DLOCK_DISCARD, ( void** ) &pIndexBuffer );

	DWORD dwBaseIndex = 0;

	for ( int i = 0; i < mCellCount; i++ )
	{
		for ( int j = 0; j < mCellCount; j++ )
		{
			pIndexBuffer[dwBaseIndex + 0] = i * ( mCellCount + 1 ) + j;
			pIndexBuffer[dwBaseIndex + 1] = ( i + 1 ) * ( mCellCount + 1 ) + j;
			pIndexBuffer[dwBaseIndex + 2] = ( i + 1 ) * ( mCellCount + 1 ) + j + 1;

			pIndexBuffer[dwBaseIndex + 3] = i * ( mCellCount + 1 ) + j;
			pIndexBuffer[dwBaseIndex + 4] = ( i + 1 ) * ( mCellCount + 1 ) + j + 1;;
			pIndexBuffer[dwBaseIndex + 5] = i * ( mCellCount + 1 ) + j + 1;

			dwBaseIndex += 6;
		}
	}

	mesh->UnlockIndexBuffer();

	mMeshBufferNode->setMesh ( mesh );
	mMeshBufferNode->setSubCount ( 1 );

	DWORD *pAdj = new DWORD[mesh->GetNumFaces() * 3];

	mesh->GenerateAdjacency ( 1.0f, pAdj );

	delete []pAdj ;
}
Exemplo n.º 7
0
/** Merges a set of D3DXMeshes. */
static void MergeD3DXMeshes(
	IDirect3DDevice9* Device,
	TRefCountPtr<ID3DXMesh>& OutMesh,TArray<int32>& OutBaseFaceIndex,const TArray<ID3DXMesh*>& Meshes)
{
	TArray<D3DVERTEXELEMENT9> VertexElements;
	GetD3D9MeshVertexDeclarations(VertexElements);
		
	// Count the number of faces and vertices in the input meshes.
	int32 NumFaces = 0;
	int32 NumVertices = 0;
	for(int32 MeshIndex = 0;MeshIndex < Meshes.Num();MeshIndex++)
	{
		NumFaces += Meshes[MeshIndex]->GetNumFaces();
		NumVertices += Meshes[MeshIndex]->GetNumVertices();
	}

	// Create mesh for source data
	VERIFYD3D9RESULT(D3DXCreateMesh(
		NumFaces,
		NumVertices,
		D3DXMESH_SYSTEMMEM,
		(D3DVERTEXELEMENT9*)VertexElements.GetData(),
		Device,
		OutMesh.GetInitReference()
		) );

	// Fill D3DXMesh
	FUtilVertex* ResultVertices;
	uint16*		 ResultIndices;
	::DWORD *		 ResultAttributes;
	OutMesh->LockVertexBuffer(0,(LPVOID*)&ResultVertices);
	OutMesh->LockIndexBuffer(0,(LPVOID*)&ResultIndices);
	OutMesh->LockAttributeBuffer(0, &ResultAttributes);

	int32 BaseVertexIndex = 0;
	int32 BaseFaceIndex = 0;
	for(int32 MeshIndex = 0;MeshIndex < Meshes.Num();MeshIndex++)
	{
		ID3DXMesh* Mesh = Meshes[MeshIndex];
				
		FUtilVertex* Vertices;
		uint16*		 Indices;
		::DWORD *		 Attributes;
		Mesh->LockVertexBuffer(0,(LPVOID*)&Vertices);
		Mesh->LockIndexBuffer(0,(LPVOID*)&Indices);
		Mesh->LockAttributeBuffer(0, &Attributes);

		for(uint32 FaceIndex = 0;FaceIndex < Mesh->GetNumFaces();FaceIndex++)
		{
			for(uint32 VertexIndex = 0;VertexIndex < 3;VertexIndex++)
			{
				*ResultIndices++ = BaseVertexIndex + *Indices++;
			}
		}
		OutBaseFaceIndex.Add(BaseFaceIndex);
		BaseFaceIndex += Mesh->GetNumFaces();

		FMemory::Memcpy(ResultVertices,Vertices,Mesh->GetNumVertices() * sizeof(FUtilVertex));
		ResultVertices += Mesh->GetNumVertices();
		BaseVertexIndex += Mesh->GetNumVertices();

		FMemory::Memcpy(ResultAttributes,Attributes,Mesh->GetNumFaces() * sizeof(uint32));
		ResultAttributes += Mesh->GetNumFaces();

		Mesh->UnlockIndexBuffer();
		Mesh->UnlockVertexBuffer();
		Mesh->UnlockAttributeBuffer();
	}

	OutMesh->UnlockIndexBuffer();
	OutMesh->UnlockVertexBuffer();
	OutMesh->UnlockAttributeBuffer();
}
Exemplo n.º 8
0
void Terrain::buildSubGridMesh(RECT& R, VertexPNT* gridVerts)
{
	//===============================================================
	// Create the subgrid mesh.
	ID3DXMesh* subMesh = 0;
	D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE];
	UINT numElems = 0;
	HR(VertexPNT::Decl->GetDeclaration(elems, &numElems));
	HR(D3DXCreateMesh(SubGrid::NUM_TRIS, SubGrid::NUM_VERTS, 
		D3DXMESH_MANAGED, elems, gd3dDevice, &subMesh));


	//===============================================================
	// Build Vertex Buffer.  Copy rectangle of vertices from the
	// grid into the subgrid structure.
	VertexPNT* v = 0;
	HR(subMesh->LockVertexBuffer(0, (void**)&v));
	int k = 0;
	for(int i = R.top; i <= R.bottom; ++i)
	{
		for(int j = R.left; j <= R.right; ++j)
		{
			v[k++] = gridVerts[i*mVertCols+j];
		}
	}

	//===============================================================
	// Compute the bounding box before unlocking the vertex buffer.
	AABB bndBox;
	HR(D3DXComputeBoundingBox((D3DXVECTOR3*)v, subMesh->GetNumVertices(), 
		sizeof(VertexPNT), &bndBox.minPt, &bndBox.maxPt));

	HR(subMesh->UnlockVertexBuffer());


	//===============================================================
	// Build Index and Attribute Buffer.
	// Get indices for subgrid (we don't use the verts here--the verts
	// are given by the parameter gridVerts).
	std::vector<D3DXVECTOR3> tempVerts;
	std::vector<DWORD> tempIndices;
	GenTriGrid(SubGrid::NUM_ROWS, SubGrid::NUM_COLS, mDX, mDZ, 
		D3DXVECTOR3(0.0f, 0.0f, 0.0f), tempVerts, tempIndices);

	WORD* indices  = 0;
	DWORD* attBuff = 0;
	HR(subMesh->LockIndexBuffer(0, (void**)&indices));
	HR(subMesh->LockAttributeBuffer(0, &attBuff));
	for(int i = 0; i < SubGrid::NUM_TRIS; ++i)
	{
		indices[i*3+0] = (WORD)tempIndices[i*3+0];
		indices[i*3+1] = (WORD)tempIndices[i*3+1];
		indices[i*3+2] = (WORD)tempIndices[i*3+2];

		attBuff[i] = 0; // All in subset 0.
	}
	HR(subMesh->UnlockIndexBuffer());
	HR(subMesh->UnlockAttributeBuffer());


	//===============================================================
	// Optimize for the vertex cache and build attribute table.
	DWORD* adj = new DWORD[subMesh->GetNumFaces()*3];
	HR(subMesh->GenerateAdjacency(EPSILON, adj));
	HR(subMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE|D3DXMESHOPT_ATTRSORT,
		adj, 0, 0, 0));
	delete[] adj;

	
	//===============================================================
	// Save the mesh and bounding box.
	mSubGridMeshes.push_back(subMesh);
	mSubGridBndBoxes.push_back(bndBox);
}
Exemplo n.º 9
0
void Terrain::buildGeometry()
{
	//===============================================================
	// Create one large mesh for the grid in system memory.

	DWORD numTris  = (mVertRows-1)*(mVertCols-1)*2;
	DWORD numVerts = mVertRows*mVertCols;

	ID3DXMesh* mesh = 0;
	D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE];
	UINT numElems = 0;
	HR(VertexPNT::Decl->GetDeclaration(elems, &numElems));
	HR(D3DXCreateMesh(numTris, numVerts, 
		D3DXMESH_SYSTEMMEM|D3DXMESH_32BIT, elems, gd3dDevice, &mesh));


	//===============================================================
	// Write the grid vertices and triangles to the mesh.

	VertexPNT* v = 0;
	HR(mesh->LockVertexBuffer(0, (void**)&v));
	
	std::vector<D3DXVECTOR3> verts;
	std::vector<DWORD> indices;
	GenTriGrid(mVertRows, mVertCols, mDX, mDZ, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);

	float w = mWidth;
	float d = mDepth;
	for(UINT i = 0; i < mesh->GetNumVertices(); ++i)
	{
		// We store the grid vertices in a linear array, but we can
		// convert the linear array index to an (r, c) matrix index.
		int r = i / mVertCols;
		int c = i % mVertCols;

		v[i].pos   = verts[i];
		v[i].pos.y = mHeightmap(r, c);

		v[i].tex0.x = (v[i].pos.x + (0.5f*w)) / w;
		v[i].tex0.y = (v[i].pos.z - (0.5f*d)) / -d;
	}

	// Write triangle data so we can compute normals.

	DWORD* indexBuffPtr = 0;
	HR(mesh->LockIndexBuffer(0, (void**)&indexBuffPtr));
	for(UINT i = 0; i < mesh->GetNumFaces(); ++i)
	{
		indexBuffPtr[i*3+0] = indices[i*3+0];
		indexBuffPtr[i*3+1] = indices[i*3+1];
		indexBuffPtr[i*3+2] = indices[i*3+2];
	}
	HR(mesh->UnlockIndexBuffer());

	// Compute Vertex Normals.
	HR(D3DXComputeNormals(mesh, 0));

	
	//===============================================================
	// Now break the grid up into subgrid meshes.

	// Find out the number of subgrids we'll have.  For example, if
	// m = 513, n = 257, SUBGRID_VERT_ROWS = SUBGRID_VERT_COLS = 33,
	// then subGridRows = 512/32 = 16 and sibGridCols = 256/32 = 8.
	int subGridRows = (mVertRows-1) / (SubGrid::NUM_ROWS-1);
	int subGridCols = (mVertCols-1) / (SubGrid::NUM_COLS-1);

	for(int r = 0; r < subGridRows; ++r)
	{
		for(int c = 0; c < subGridCols; ++c)
		{
			// Rectangle that indicates (via matrix indices ij) the
			// portion of global grid vertices to use for this subgrid.
			RECT R = 
			{
					c * (SubGrid::NUM_COLS-1),
					r * (SubGrid::NUM_ROWS-1),
				(c+1) * (SubGrid::NUM_COLS-1),
				(r+1) * (SubGrid::NUM_ROWS-1)
			};

			buildSubGridMesh(R, v); 
		}
	}

	HR(mesh->UnlockVertexBuffer());

	ReleaseCOM(mesh); // Done with global mesh.
}
Exemplo n.º 10
0
Mesh* GenerateCylinder(IDirect3DDevice9* device, float radius, float height, UINT sectors)
{
    if (!device)
	{
		return 0;
	}
	const DWORD numVertexes = 2 * (sectors + 1);
	const DWORD numFaces = 2 * sectors;
    
    ID3DXMesh* mesh = 0;

    HRESULT hr = D3DXCreateMeshFVF(numFaces,
                      numVertexes,
                      D3DXMESH_32BIT | D3DXMESH_VB_WRITEONLY | D3DXMESH_IB_WRITEONLY,
                      D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,
                      device,
                      &mesh);
    if (FAILED(hr))
    {
        MessageBox(0, L"Unable to generate sphere", L"Application error", MB_ICONSTOP);
        return 0;
    }
    SphereVertex* vertexes = 0;
    DWORD* indexes = 0;

    mesh->LockVertexBuffer(0, (void**)&vertexes);
    mesh->LockIndexBuffer(0, (void**)&indexes);

    const UINT rings = 1;
    //
    for (UINT ring = 0; ring < rings + 1; ring++)
    {
        for (UINT sector = 0; sector < sectors + 1; sector++)
        {
            const float z = sinf(sector * D3DX_PI * 2.f / sectors) * radius;
            const float x = cosf(sector * D3DX_PI * 2.f / sectors) * radius ;
            const float y = height * (1.0f - (ring / rings));

            SphereVertex* vertex = vertexes++;
            vertex->pos = Vec3(x, y, z);
            vertex->normal = Vec3(x, 0, z);
            vertex->tex = Vec2((FLOAT)sector / (sectors - 1), (FLOAT)ring / rings );
        }
    }

    //
    for (UINT ring = 0; ring < rings; ++ring)
    {
        for (UINT sector = 0; sector < sectors; ++sector)
        {
            DWORD v0 = (ring * (sectors + 1) + sector);
            DWORD v1 = (ring * (sectors + 1) + sector + 1);
            DWORD v2 = ((ring + 1) * (sectors + 1) + sector);
            DWORD v3 = ((ring + 1) * (sectors + 1) + sector + 1);

            *indexes++ = v0;
            *indexes++ = v1;
            *indexes++ = v2;

            *indexes++ = v2;
            *indexes++ = v1;
            *indexes++ = v3;
        }
    }

    mesh->UnlockIndexBuffer();
    mesh->UnlockVertexBuffer();
    return mesh;
}
Exemplo n.º 11
0
void PropsDemo::buildGrass()
{
	D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE];
	UINT numElems = 0;
	HR(GrassVertex::Decl->GetDeclaration(elems, &numElems));

	HR(D3DXCreateMesh(NUM_GRASS_BLOCKS*2, NUM_GRASS_BLOCKS*4, D3DXMESH_MANAGED, 
		elems, gd3dDevice, &mGrassMesh));

	GrassVertex* v = 0;
	WORD* k = 0;
	HR(mGrassMesh->LockVertexBuffer(0, (void**)&v));
	HR(mGrassMesh->LockIndexBuffer(0, (void**)&k));

	int indexOffset = 0;

	// Scale down the region in which we generate grass.
	int w = (int)(mTerrain->getWidth() * 0.15f);
	int d = (int)(mTerrain->getDepth() * 0.15f);

	// Randomly generate a grass block (three intersecting quads) around the 
	// terrain in the height range [35, 50] (similar to the trees).
	for(int i = 0; i < NUM_GRASS_BLOCKS; ++i)
	{
		//============================================
		// Construct vertices.

		// Generate random position in region.  Note that we also shift
		// this region to place it in the world.
		float x = (float)((rand() % w) - (w*0.5f)) - 30.0f;
		float z = (float)((rand() % d) - (d*0.5f)) - 20.0f;
		float y = mTerrain->getHeight(x, z); 

		// Only generate grass blocks in this height range.  If the height
		// is outside this range, generate a new random position and 
		// try again.
		if(y < 37.0f || y > 40.0f)
		{
			--i; // We are trying again, so decrement back the index.
			continue;
		}

		float sx = GetRandomFloat(0.75f, 1.25f); 
		float sy = GetRandomFloat(0.75f, 1.25f);
		float sz = GetRandomFloat(0.75f, 1.25f);
		D3DXVECTOR3 pos(x, y, z);
		D3DXVECTOR3 scale(sx, sy, sz);

		buildGrassFin(v, k, indexOffset, pos, scale);
		v += 4;
		k += 6;
	}

	HR(mGrassMesh->UnlockVertexBuffer());
	HR(mGrassMesh->UnlockIndexBuffer());


	// Fill in the attribute buffer (everything in subset 0)
	DWORD* attributeBufferPtr = 0;
	HR(mGrassMesh->LockAttributeBuffer(0, &attributeBufferPtr));
	for(UINT i = 0; i < mGrassMesh->GetNumFaces(); ++i)
		attributeBufferPtr[i] = 0;
	HR(mGrassMesh->UnlockAttributeBuffer());

	DWORD* adj = new DWORD[mGrassMesh->GetNumFaces()*3];
	HR(mGrassMesh->GenerateAdjacency(EPSILON, adj));
	HR(mGrassMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_VERTEXCACHE,
		adj, 0, 0, 0));

	delete [] adj;
}
Exemplo n.º 12
0
bool CMeshBundle::loadMesh( const CResourceId& id, const CResourceId& fullName, CMesh& mesh ) const
{
	// try to load with D3DX
	// obsolete case: .X files
	if( CStringHelper::endsWith( fullName.getUniqueName(), ".x" ) || CStringHelper::endsWith( fullName.getUniqueName(), ".X" ) ) {
		ID3DXBuffer* adjancency = NULL;
		ID3DXBuffer* material = NULL;
		ID3DXBuffer* effects = NULL;
		DWORD matCount;
		ID3DXMesh* dxmesh = NULL;

		HRESULT hres = D3DXLoadMeshFromX(
			fullName.getUniqueName().c_str(),
			D3DXMESH_SYSTEMMEM,
			&CD3DDevice::getInstance().getDevice(),
			&adjancency,
			&material,
			&effects,
			&matCount,
			&dxmesh );
		if( !SUCCEEDED( hres ) )
			return false;
		assert( dxmesh );

		if( adjancency )
			adjancency->Release();
		if( material )
			material->Release();
		if( effects )
			effects->Release();

		//
		// init our mesh

		assert( !mesh.isCreated() );
		// HACK - very limited
		int formatFlags = 0;
		DWORD dxFormat = dxmesh->GetFVF();
		if( dxFormat & D3DFVF_XYZ )
			formatFlags |= CVertexFormat::V_POSITION;
		if( dxFormat & D3DFVF_NORMAL )
			formatFlags |= CVertexFormat::V_NORMAL;
		if( dxFormat & D3DFVF_TEX1 )
			formatFlags |= CVertexFormat::V_UV0_2D;
		CVertexFormat vertFormat( formatFlags );
		// HACK
		int indexStride = 2;

		CD3DVertexDecl* vertDecl = RGET_VDECL( CVertexDesc( vertFormat ) );
		mesh.createResource( dxmesh->GetNumVertices(), dxmesh->GetNumFaces()*3, vertFormat, indexStride, *vertDecl, CMesh::BUF_STATIC );

		//
		// now, copy data into our mesh

		void *dxvb, *dxib;
		dxmesh->LockVertexBuffer( 0, &dxvb );
		dxmesh->LockIndexBuffer( 0, &dxib );
		void* myvb = mesh.lockVBWrite();
		void* myib = mesh.lockIBWrite();

		memcpy( myvb, dxvb, mesh.getVertexCount() * mesh.getVertexStride() );
		memcpy( myib, dxib, mesh.getIndexCount() * mesh.getIndexStride() );
		
		dxmesh->UnlockVertexBuffer();
		dxmesh->UnlockIndexBuffer();
		mesh.unlockVBWrite();
		mesh.unlockIBWrite();

		//
		// create groups

		int ngroups;
		dxmesh->GetAttributeTable( 0, (DWORD*)&ngroups );
		D3DXATTRIBUTERANGE *attrs = new D3DXATTRIBUTERANGE[ngroups];
		dxmesh->GetAttributeTable( attrs, (DWORD*)&ngroups );
		for( int i = 0; i < ngroups; ++i ) {
			const D3DXATTRIBUTERANGE& a = attrs[i];
			mesh.addGroup( CMesh::CGroup( a.VertexStart, a.VertexCount, a.FaceStart, a.FaceCount ) );
		}
		delete[] attrs;

		// release d3dx mesh
		dxmesh->Release();

	} else {

		// our own format
		assert( !mesh.isCreated() );
		bool ok = CMeshSerializer::loadMeshFromFile( fullName.getUniqueName().c_str(), mesh );
		if( !ok )
			return false;
	}
	mesh.computeAABBs();
	CONSOLE.write( "mesh loaded '" + id.getUniqueName() + "'" );
	return true;
}
Exemplo n.º 13
0
VCNNode* D3DConverter::ConvertMesh(const std::wstring& name, LPD3DXMESHCONTAINER baseMeshContainer,
	D3DXFRAME* frameRoot, ID3DXAnimationController* animController, LPDIRECT3DDEVICE9 device)
{
	MultiAnimMC* meshContainer = static_cast<MultiAnimMC*>(baseMeshContainer);
	ID3DXMesh* systemMesh = meshContainer->MeshData.pMesh;

	// Load vertex caches
	//
	DWORD meshFVF = systemMesh->GetFVF();
	size_t vertexCount = systemMesh->GetNumVertices();
	const DWORD stride = D3DXGetFVFVertexSize( meshFVF );
	const DWORD normalStride = D3DXGetFVFVertexSize( D3DFVF_NORMAL );
	const DWORD diffuseStride = D3DXGetFVFVertexSize( D3DFVF_DIFFUSE );
	const DWORD textureStride = D3DXGetFVFVertexSize( D3DFVF_TEX1 );

	std::vector<VCNFloat> vtPositionBuffer( vertexCount * kCacheStrides[VT_POSITION] );
	std::vector<VCNFloat> vtBlendWeights( vertexCount * kCacheStrides[VT_BLENDWEIGHTS] ); //TODO Verify the size of this shit
	std::vector<DWORD> vtBlendIndices( vertexCount * kCacheStrides[VT_BLENDINDICES] ); //TODO Verify the size of this shit

	std::vector<VCNFloat> vtNormalBuffer( vertexCount * kCacheStrides[VT_LIGHTING] );
	std::vector<VCNFloat> vtTextureBuffer( vertexCount * kCacheStrides[VT_DIFFUSE_TEX_COORDS] );

	VCNFloat* vtPositionBuf = &vtPositionBuffer[0];
	VCNFloat* vtBlendWeightBuf = &vtBlendWeights[0];
	DWORD* vtBlendIndicesBuf = &vtBlendIndices[0];

	VCNFloat* vtNormalBuf = &vtNormalBuffer[0];
	VCNFloat* vtTextureBuf = &vtTextureBuffer[0];

	BYTE* vbptr = NULL;
	BYTE* vblineptr = NULL;
	systemMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&vblineptr);

	DWORD positionBlendAndIndicesStride = GetPositionStride(meshFVF);

	for(VCNUInt i = 0; i < vertexCount; ++i)
	{
		vbptr = vblineptr;

		if ( ContainsPositionInformation(meshFVF) )
		{
			// Read position
			float* posData = (float*)vbptr;
			*vtPositionBuf = posData[0]; vtPositionBuf++;
			*vtPositionBuf = posData[1]; vtPositionBuf++;
			*vtPositionBuf = posData[2]; vtPositionBuf++;

			if (ContainsBlending(meshFVF))
			{
				// Get blend weights
				size_t blendCount = (positionBlendAndIndicesStride / 4) - 3 - 1; // -3 to remove xyz, -1 to remove indices which come after
				for(size_t i = 0; i < blendCount; ++i)
				{
					*vtBlendWeightBuf = posData[3 + i]; vtBlendWeightBuf++;
				}
				vtBlendWeightBuf += 4 - blendCount; //each item is an array of 4 floats

				// Get blend indices
				// TODO SKIN Check the format we have to send this data as.
				if ( ContainsFlag(meshFVF, D3DFVF_LASTBETA_UBYTE4) )
				{
					*vtBlendIndicesBuf = ((DWORD*)vbptr)[3 + blendCount]; vtBlendIndicesBuf++;
				}
			}

			vbptr += positionBlendAndIndicesStride;
		}
		else
		{
			VCN_ASSERT_FAIL( VCNTXT("Mesh FVF not supported (no vertex position) [FVF = %d, stride = %d]"), meshFVF, stride );
		}

		// Read normal
		if ( ContainsFlag(meshFVF, D3DFVF_NORMAL) )
		{
			D3DXVECTOR3* normal = (D3DXVECTOR3*)(vbptr);
			*vtNormalBuf = normal->x; vtNormalBuf++;
			*vtNormalBuf = normal->y; vtNormalBuf++;
			*vtNormalBuf = normal->z; vtNormalBuf++;

			// Set default diffuse color
			std::fill(vtNormalBuf, vtNormalBuf+3, 1.0f); vtNormalBuf += 3;

			vbptr += normalStride;
		}
		else
		{
			VCN_ASSERT_FAIL( VCNTXT("Mesh FVF not supported (no normals) [FVF = %d, stride = %d]"), meshFVF, stride );
		}

		if ( ContainsFlag(meshFVF, D3DFVF_DIFFUSE) ) vbptr += diffuseStride;

		// Read texcoords
		// the check with D3DFVF_TEX0 is pretty useless as it's always true... the flag value is 0...
		if ( ContainsFlag(meshFVF, D3DFVF_TEX0) || ContainsFlag(meshFVF, D3DFVF_TEX1) )
		{
			float* texCoords = (float*)(vbptr);
			*vtTextureBuf = texCoords[0]; vtTextureBuf++;
			*vtTextureBuf = texCoords[1]; vtTextureBuf++;

			vbptr += textureStride;
		}
		else
		{
			VCN_ASSERT_FAIL( VCNTXT("Mesh FVF not supported (no texture coordinates) [FVF = %d, stride = %d]"), meshFVF, stride );
		}

		vblineptr += stride;
	}
	systemMesh->UnlockVertexBuffer();

	VCND3D9* renderer = VCNRenderCore::GetInstance()->Cast<VCND3D9>();

	// Generate cache resources that will be bind to Vicuna's meshes
	VCNResID positionCache = renderer->CreateCache(VT_POSITION, &vtPositionBuffer[0], vertexCount * kCacheStrides[VT_POSITION]);
	VCNResID lightingCache = renderer->CreateCache(VT_LIGHTING, &vtNormalBuffer[0], vertexCount * kCacheStrides[VT_LIGHTING]);
	VCNResID textureCache = renderer->CreateCache(VT_DIFFUSE_TEX_COORDS, &vtTextureBuffer[0], vertexCount * kCacheStrides[VT_DIFFUSE_TEX_COORDS]);

	VCNResID blendWeightCache = renderer->CreateCache(VT_BLENDWEIGHTS, &vtBlendWeights[0], vertexCount * kCacheStrides[VT_BLENDWEIGHTS]);
	VCNResID blendIndiceCache = renderer->CreateCache(VT_BLENDINDICES, &vtBlendIndices[0], vertexCount * kCacheStrides[VT_BLENDINDICES]);

	// Get model faces
	//
	VCNUShort* ibptr = 0;
	std::vector<VCNUShort> indices( systemMesh->GetNumFaces() * 3 );
	systemMesh->LockIndexBuffer(D3DLOCK_READONLY, (LPVOID*)&ibptr);
	for(VCNUInt i = 0; i < systemMesh->GetNumFaces(); i++)
	{
		indices[(i * 3) + 0] = *(ibptr++);
		indices[(i * 3) + 1] = *(ibptr++);
		indices[(i * 3) + 2] = *(ibptr++);
	}
	systemMesh->UnlockIndexBuffer();

	// Load materials
	//
	std::vector<VCNResID> materialIDS;
	D3DXMATERIAL* d3dxMaterials = meshContainer->pMaterials;
	for (DWORD i = 0; i < meshContainer->NumMaterials; ++i)
	{
		VCNResID materialID = kInvalidResID;

		// Create the texture if it exists - it may not
		if ( d3dxMaterials[i].pTextureFilename )
		{
			VCNResID textureID = kInvalidResID;

			VCNString texturePath = VCNTXT("Textures/");
			texturePath += VCN_A2W(d3dxMaterials[i].pTextureFilename);

			// Check if the texture is already loaded
			VCND3D9Texture* resTexture = VCNResourceCore::GetInstance()->GetResource<VCND3D9Texture>(texturePath);
			if ( !resTexture )
			{
				textureID = VCNMaterialCore::GetInstance()->CreateTexture(texturePath);
				VCN_ASSERT_MSG( textureID != kInvalidResID, VCNTXT("Can't load texture %s"), texturePath.c_str() );
			}
			else
			{
				textureID = resTexture->GetResourceID();
			}

			VCNMaterial* material = new VCNMaterial();
			const VCNString materialName = StringBuilder() << name << VCNTXT("_material_") << i;
			material->SetName( materialName );
			VCNColor ambient = VCNColor((const VCNFloat*)&d3dxMaterials[i].MatD3D.Ambient);
			ambient.a = 1.0f;
			ambient += VCNColor(0.5f, 0.5f, 0.5f, 0);

			material->SetAmbientColor( ambient );
			material->SetDiffuseColor( VCNColor((const VCNFloat*)&d3dxMaterials[i].MatD3D.Diffuse) );
			material->SetSpecularColor( VCNColor((const VCNFloat*)&d3dxMaterials[i].MatD3D.Specular) );
			material->SetSpecularPower( d3dxMaterials[i].MatD3D.Power );

			VCNEffectParamSet& params = material->GetEffectParamSet();
			params.SetEffectID( eidSkinned );
			params.AddResource( VCNTXT("DiffuseTexture"), textureID );

			// Add material as a resource.
			materialID = VCNResourceCore::GetInstance()->AddResource( material->GetName(), material );
		}

		materialIDS.push_back( materialID );
	}

	// Get the model attribute table with which we will instantiate has many mesh.
	//
	DWORD attribTableSize;
	std::vector<D3DXATTRIBUTERANGE> attribTable;
	HRESULT hr = systemMesh->GetAttributeTable( 0, &attribTableSize );
	if ( FAILED(hr) )
		return 0;
	attribTable.resize( attribTableSize );
	hr = systemMesh->GetAttributeTable( &attribTable[0], &attribTableSize );
	if ( FAILED(hr) )
		return 0;

	// Set the root node
	VCNNode* rootNode = attribTableSize > 1 ?  VCNNodeCore::GetInstance()->CreateNode<VCNNode>() : 
		VCNNodeCore::GetInstance()->CreateNode<VCNRenderNode>();

	rootNode->SetTag( StringBuilder() << name << VCNTXT("_Root") );

	// For each attribute, we get the material texture
	for (DWORD i = 0; i < attribTableSize; ++i)
	{
		VCNRenderNode* partNode = attribTableSize == 1 ? 
			safe_pointer_cast<VCNRenderNode*>( rootNode ) : 
		VCNNodeCore::GetInstance()->CreateNode<VCNRenderNode>();

		const VCNString partNodeName = StringBuilder() << name << VCNTXT("_Part_") << i;
		partNode->SetTag( partNodeName );

		VCNMesh* partMesh = new VCNMesh();
		partMesh->SetCacheID(VT_POSITION, positionCache); //SKIN do this for blend weights and blend indices
		partMesh->SetCacheID(VT_LIGHTING, lightingCache);
		partMesh->SetCacheID(VT_DIFFUSE_TEX_COORDS, textureCache);
		partMesh->SetCacheID(VT_BLENDWEIGHTS, blendWeightCache);
		partMesh->SetCacheID(VT_BLENDINDICES, blendIndiceCache);
		partMesh->SetPrimitiveType(PT_TRIANGLELIST);

		partMesh->SetBoneInfluenceCount( meshContainer->m_dwMaxNumFaceInfls );
		size_t numBones = meshContainer->pSkinInfo == nullptr ? 0 : meshContainer->pSkinInfo->GetNumBones();
		if (numBones > 0)
		{
			auto offsets = std::vector<Matrix4>(numBones);
			std::transform( std::begin(meshContainer->m_amxBoneOffsets), std::end(meshContainer->m_amxBoneOffsets), std::begin(offsets), [](const D3DXMATRIX& mat)
			{
				return Matrix4( (VCNFloat*)mat.m );
			});

			partMesh->SetBoneOffsets( std::move(offsets) );

			LPD3DXBONECOMBINATION boneCombination = reinterpret_cast<LPD3DXBONECOMBINATION>( 
				meshContainer->m_pBufBoneCombos->GetBufferPointer() );

			size_t numPaletteEntries = meshContainer->m_dwNumPaletteEntries;

			std::vector<size_t> matriceIndexes;

			for(size_t paletteIndex = 0; paletteIndex < numPaletteEntries; ++paletteIndex)
			{
				size_t matIndex = boneCombination[i].BoneId[paletteIndex];
				if ( matIndex == std::numeric_limits<size_t>::max())
					continue;

				matriceIndexes.push_back(matIndex);
			}

			partMesh->SetMatrixPaletteIndexes(matriceIndexes);
		}

		const DWORD partFaceCount = attribTable[i].FaceCount;
		const void* partFaceBufferStart = &indices[attribTable[i].FaceStart * 3];
		const VCNResID indexCacheID = renderer->CreateCache(VT_INDEX, partFaceBufferStart, partFaceCount * 3 * kCacheStrides[VT_INDEX]);
		partMesh->SetFaceCount( attribTable[i].FaceCount );
		partMesh->SetFaceCache( indexCacheID );

		// Compute bounding sphere
		float radius;
		D3DXVECTOR3 center;
		D3DXComputeBoundingSphere( (D3DXVECTOR3*)(&vtPositionBuffer[0] + attribTable[i].VertexStart * 3), 
			attribTable[i].VertexCount, stride, &center, &radius );
		VCNSphere modelBoundSphere( radius, V2V<Vector3>(center) );
		partMesh->SetBoundingSphere( modelBoundSphere );

		// Add mesh resource
		const VCNString partMeshName = StringBuilder() << name << VCNTXT("_part_") << i;
		const VCNResID partMeshID = VCNResourceCore::GetInstance()->AddResource( partMeshName, partMesh );

		// Set model part node attributes
		partNode->SetMeshID( partMeshID );
		if (animController && numBones > 0)
		{
			partNode->AddComponent( new VCND3DAnimator(partMeshID, animController, frameRoot, meshContainer->m_apmxBonePointers) );
		}

		size_t index = attribTable[i].AttribId;
		index = index >= materialIDS.size() ? materialIDS.size() - 1 : index;

		partNode->SetMaterialID( materialIDS[index] );

		// Add children to root
		if ( attribTableSize > 1 )
		{
			rootNode->AttachChild( partNode->GetNodeID() );
		}
	}

	return rootNode;
}
Exemplo n.º 14
0
void CMeshConverter::OptimiseGraphicsObject(void)
{
	int						i;
	int						j;
	ID3DXMesh*				pXMesh;
	CGraphicsPrimitive*		pcPrimitive;
	CVertexBufferExtended*	psVertexBuffer;
	void*					pvDestIndexBuffer;
	void*					pvDestVertexBuffer;
	void*					pvSrcIndexBuffer;
	void*					pvSrcVertexBuffer;
	int						iVertSize;
	DWORD*					pvAdjacency;
	int						iPrimitiveStart;
	int						iOldVertSize;
	int						iNumIndices;
	int						iOldVertexBufferIndex;
	int						iNumTriangles;
	void*					pvDestBaseIndexBuffer;
	SIndexBuffer*			psIndexBuffer;
	DWORD					iMeshOptions;

	mpcGraphicsObject->SortPrimitives();
	mpcGraphicsObject->Lock();
	psIndexBuffer = mpcGraphicsObject->GetIndexBuffer();
	iVertSize = 0;
	iNumIndices = 0;
	iOldVertexBufferIndex = 0;
	iNumTriangles = 0;
	iOldVertSize = 0;
	iMeshOptions = D3DXMESH_SYSTEMMEM;
	SetFlag((int*)&iMeshOptions, D3DXMESH_32BIT, psIndexBuffer->iIndexSize == 4);
	for (i = 0; i < mpcGraphicsObject->GetNumPrimitives(); i++)
	{
		pcPrimitive = mpcGraphicsObject->GetPrimitive(i);
		psVertexBuffer = mpcGraphicsObject->GetVertexBufferForIndex(pcPrimitive->miVertexBufferIndex);

		if (iOldVertSize != psVertexBuffer->iVertexSize)
		{
			if (iNumIndices != 0)
			{
				gcD3D.CreateMesh(iNumTriangles, iNumIndices, iMeshOptions, psVertexBuffer->iVertexFormat, &pXMesh);
				iVertSize = pXMesh->GetNumBytesPerVertex();
				pXMesh->LockIndexBuffer(D3DLOCK_NO_DIRTY_UPDATE, &pvDestBaseIndexBuffer);
				pXMesh->LockVertexBuffer(D3DLOCK_NO_DIRTY_UPDATE, &pvDestVertexBuffer);

				psVertexBuffer = mpcGraphicsObject->GetVertexBufferForIndex(iOldVertexBufferIndex);
				pvSrcVertexBuffer = psVertexBuffer->pvLockedBuffer;

				if (iVertSize != psVertexBuffer->iVertexSize)
				{
					gcLogger.Error("D3DX vertex size differs from expected size");
					break;
				}

				memcpy(pvDestVertexBuffer, pvSrcVertexBuffer, psVertexBuffer->iVertexSize * psVertexBuffer->iNumVerticies);

				pvDestIndexBuffer = pvDestBaseIndexBuffer;
				for (j = iPrimitiveStart; j < i; j++)
				{
					pcPrimitive = mpcGraphicsObject->GetPrimitive(j);
					pvSrcIndexBuffer = RemapSinglePointer(psIndexBuffer->pvLockedBuffer, 2 * pcPrimitive->miStartIndex);
					memcpy(pvDestIndexBuffer, pvSrcIndexBuffer, psIndexBuffer->iIndexSize * pcPrimitive->miNumVertices);
					pvDestIndexBuffer = RemapSinglePointer(pvDestIndexBuffer, pcPrimitive->miNumVertices);
				}

				pvAdjacency = (DWORD*)malloc(pcPrimitive->miNumPrimitives * 3 * sizeof(DWORD));
				pXMesh->GenerateAdjacency(0.0f, pvAdjacency);
				pXMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_DONOTSPLIT, pvAdjacency, NULL, NULL, NULL);
				free(pvAdjacency);

				pvDestIndexBuffer = pvDestBaseIndexBuffer;
				for (j = iPrimitiveStart; j < i; j++)
				{
					pcPrimitive = mpcGraphicsObject->GetPrimitive(j);
					pvSrcIndexBuffer = RemapSinglePointer(psIndexBuffer->pvLockedBuffer, 2 * pcPrimitive->miStartIndex);
					memcpy(pvSrcIndexBuffer, pvDestIndexBuffer, psIndexBuffer->iIndexSize * pcPrimitive->miNumVertices);
					pvDestIndexBuffer = RemapSinglePointer(pvDestIndexBuffer, pcPrimitive->miNumVertices);
				}

				memcpy(pvSrcVertexBuffer, pvDestVertexBuffer, psVertexBuffer->iVertexSize * psVertexBuffer->iNumVerticies);

				pXMesh->UnlockIndexBuffer();
				pXMesh->UnlockVertexBuffer();
				pXMesh->Release();
			}
			iPrimitiveStart = i;
			iNumIndices = 0;
			iOldVertexBufferIndex = pcPrimitive->miVertexBufferIndex;
		}
		else
		{
			iNumIndices += pcPrimitive->miNumVertices;
			iNumTriangles += pcPrimitive->miNumPrimitives;
			if (iOldVertexBufferIndex != pcPrimitive->miVertexBufferIndex)
			{
				gcUserError.Set("Primitive vertex buffer index is F****D!");
				break;
			}
		}
	}
	mpcGraphicsObject->Unlock();
}
Exemplo n.º 15
0
ID3DXMesh* CMesh::createD3DXMesh() const
{
    HRESULT hr;
    ID3DXMesh* dxMesh = 0;

    DWORD meshOpts = D3DXMESH_MANAGED;
    if( getIndexStride() == 4 )
        meshOpts |= D3DXMESH_32BIT;


    // get declaration
    D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
    UINT numElements;
    getVertexDecl().getObject()->GetDeclaration( decl, &numElements );
    // create mesh
    hr = D3DXCreateMesh( getIndexCount()/3, getVertexCount(), meshOpts, decl, &CD3DDevice::getInstance().getDevice(), &dxMesh );
    if( FAILED(hr) )
        return NULL;

    // copy VB
    {
        const void* srcVB = lockVBRead();
        void* dxVB = 0;
        hr = dxMesh->LockVertexBuffer( 0, &dxVB );
        if( FAILED(hr) ) {
            dxMesh->Release();
            return NULL;
        }
        memcpy( dxVB, srcVB, getVertexCount() * getVertexStride() );
        hr = dxMesh->UnlockVertexBuffer();
        unlockVBRead();
    }
    // copy IB
    {
        const void* srcIB = lockIBRead();
        void* dxIB = 0;
        hr = dxMesh->LockIndexBuffer( 0, &dxIB );
        if( FAILED(hr) ) {
            dxMesh->Release();
            return NULL;
        }
        memcpy( dxIB, srcIB, getIndexCount() * getIndexStride() );
        hr = dxMesh->UnlockIndexBuffer();
        unlockIBRead();
    }
    // copy groups
    {
        int ngroups = getGroupCount();
        D3DXATTRIBUTERANGE* attrs = new D3DXATTRIBUTERANGE[ngroups];
        DWORD* attrBuf = 0;
        hr = dxMesh->LockAttributeBuffer( 0, &attrBuf );
        if( FAILED(hr) ) {
            dxMesh->Release();
            return NULL;
        }
        for( int g = 0; g < ngroups; ++g ) {
            attrs[g].AttribId = g;
            const CMesh::CGroup& group = getGroup(g);
            attrs[g].VertexStart = group.getFirstVertex();
            attrs[g].VertexCount = group.getVertexCount();
            attrs[g].FaceStart = group.getFirstPrim();
            attrs[g].FaceCount = group.getPrimCount();
            for( int f = 0; f < group.getPrimCount(); ++f )
                *attrBuf++ = g;
        }
        dxMesh->UnlockAttributeBuffer();
        hr = dxMesh->SetAttributeTable( attrs, ngroups );
        delete[] attrs;
    }

    return dxMesh;
}
Exemplo n.º 16
0
//--------------------------------------------------------------------------------------
HRESULT CMeshLoader::Create( IDirect3DDevice9* pd3dDevice, const WCHAR* strFilename )
{
    HRESULT hr;
    WCHAR str[ MAX_PATH ] = {0};

    // Start clean
    Destroy();

    // Store the device pointer
    m_pd3dDevice = pd3dDevice;

    // Load the vertex buffer, index buffer, and subset information from a file. In this case, 
    // an .obj file was chosen for simplicity, but it's meant to illustrate that ID3DXMesh objects
    // can be filled from any mesh file format once the necessary data is extracted from file.
    
    //V_RETURN( LoadGeometryFromOBJ( strFilename ) );
    V_RETURN( LoadGeometryFromOBJ_Fast( strFilename ) );

    // Set the current directory based on where the mesh was found
    WCHAR wstrOldDir[MAX_PATH] = {0};
    GetCurrentDirectory( MAX_PATH, wstrOldDir );
    SetCurrentDirectory( m_strMediaDir );

    // Load material textures
    for( int iMaterial = 0; iMaterial < m_Materials.GetSize(); iMaterial++ )
    {
        Material* pMaterial = m_Materials.GetAt( iMaterial );
        if( pMaterial->strTexture[0] )
        {
            // Avoid loading the same texture twice
            bool bFound = false;
            for( int x = 0; x < iMaterial; x++ )
            {
                Material* pCur = m_Materials.GetAt( x );
                if( 0 == wcscmp( pCur->strTexture, pMaterial->strTexture ) )
                {
                    bFound = true;
                    pMaterial->pTexture = pCur->pTexture;
                    break;
                }
            }

            // Not found, load the texture
            if( !bFound )
            {
                V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, pMaterial->strTexture ) );
                V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, pMaterial->strTexture,
                                                     &( pMaterial->pTexture ) ) );
                int a = 0;
            }
        }
    }

    // Restore the original current directory
    SetCurrentDirectory( wstrOldDir );

    // Create the encapsulated mesh
    ID3DXMesh* pMesh = NULL;
    V_RETURN( D3DXCreateMesh( m_Indices.GetSize() / 3, m_Vertices.GetSize(),
                              D3DXMESH_MANAGED | D3DXMESH_32BIT, VERTEX_DECL,
                              pd3dDevice, &pMesh ) );

    // Copy the vertex data
    VERTEX* pVertex;
    V_RETURN( pMesh->LockVertexBuffer( 0, ( void** )&pVertex ) );
    memcpy( pVertex, m_Vertices.GetData(), m_Vertices.GetSize() * sizeof( VERTEX ) );
    pMesh->UnlockVertexBuffer();
    m_Vertices.RemoveAll();

    // Copy the index data
    DWORD* pIndex;
    V_RETURN( pMesh->LockIndexBuffer( 0, ( void** )&pIndex ) );
    memcpy( pIndex, m_Indices.GetData(), m_Indices.GetSize() * sizeof( DWORD ) );
    pMesh->UnlockIndexBuffer();
    m_Indices.RemoveAll();

    // Copy the attribute data
    DWORD* pSubset;
    V_RETURN( pMesh->LockAttributeBuffer( 0, &pSubset ) );
    memcpy( pSubset, m_Attributes.GetData(), m_Attributes.GetSize() * sizeof( DWORD ) );
    pMesh->UnlockAttributeBuffer();
    m_Attributes.RemoveAll();

    // Reorder the vertices according to subset and optimize the mesh for this graphics 
    // card's vertex cache. When rendering the mesh's triangle list the vertices will 
    // cache hit more often so it won't have to re-execute the vertex shader.
    DWORD* aAdjacency = new DWORD[pMesh->GetNumFaces() * 3];
    if( aAdjacency == NULL )
        return E_OUTOFMEMORY;

    V( pMesh->GenerateAdjacency( 1e-6f, aAdjacency ) );
    V( pMesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, aAdjacency, NULL, NULL, NULL ) );

    SAFE_DELETE_ARRAY( aAdjacency );
    m_pMesh = pMesh;

    return S_OK;
}
Exemplo n.º 17
0
/**
 * Exports all geometry into a D3D .x file into the current working folder.
 * @param Filename	Desired filename (may include path)
 * @param bShow		Whether the D3D .x file viewer should be invoked. If shown, we'll block until it has been closed.
 */
void F3DVisualizer::Export( const TCHAR* Filename, bool bShow/*=false*/ )
{
	ID3DXMesh* Mesh;
	Mesh = NULL;
	int32 NumPrimitives = NumTriangles() + NumLines()*2;
	int32 NumVertices = NumTriangles()*3 + NumLines()*4;
	HRESULT Result = D3DXCreateMeshFVF( NumPrimitives, NumVertices, D3DXMESH_32BIT, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_SPECULAR, D3D->D3DDevice, &Mesh );
	void* VertexBuffer = NULL;
	void* IndexBuffer = NULL;
	Result = Mesh->LockVertexBuffer(D3DLOCK_DISCARD, &VertexBuffer);
	Result = Mesh->LockIndexBuffer(D3DLOCK_DISCARD, &IndexBuffer);

	D3DXVertex* Vertices = (D3DXVertex*)VertexBuffer;
	uint32* Indices = (uint32*) IndexBuffer;
	int32 NumVerticesStored = 0;
	int32 NumIndicesStored = 0;

	// Add the triangles to the vertexbuffer and indexbuffer.
	for ( int32 TriangleIndex=0; TriangleIndex < NumTriangles(); ++TriangleIndex )
	{
		const FVector4& P1 = Triangles[TriangleIndex].Vertices[0];
		const FVector4& P2 = Triangles[TriangleIndex].Vertices[1];
		const FVector4& P3 = Triangles[TriangleIndex].Vertices[2];
		const FColor& Color = Triangles[TriangleIndex].Color;
		Vertices[NumVerticesStored+0].Pos[0] = P1[0];
		Vertices[NumVerticesStored+0].Pos[1] = P1[1];
		Vertices[NumVerticesStored+0].Pos[2] = P1[2];
		Vertices[NumVerticesStored+0].Color1 = Color.DWColor();
		Vertices[NumVerticesStored+0].Color2 = 0;
		Vertices[NumVerticesStored+1].Pos[0] = P2[0];
		Vertices[NumVerticesStored+1].Pos[1] = P2[1];
		Vertices[NumVerticesStored+1].Pos[2] = P2[2];
		Vertices[NumVerticesStored+1].Color1 = Color.DWColor();
		Vertices[NumVerticesStored+1].Color2 = 0;
		Vertices[NumVerticesStored+2].Pos[0] = P3[0];
		Vertices[NumVerticesStored+2].Pos[1] = P3[1];
		Vertices[NumVerticesStored+2].Pos[2] = P3[2];
		Vertices[NumVerticesStored+2].Color1 = Color.DWColor();
		Vertices[NumVerticesStored+2].Color2 = 0;

		// Reverse triangle winding order for the .x file.
		Indices[NumIndicesStored+0] = NumVerticesStored + 0;
		Indices[NumIndicesStored+1] = NumVerticesStored + 2;
		Indices[NumIndicesStored+2] = NumVerticesStored + 1;

		NumVerticesStored += 3;
		NumIndicesStored += 3;
	}

	// Add the lines to the vertexbuffer and indexbuffer.
	for ( int32 LineIndex=0; LineIndex < NumLines(); ++LineIndex )
	{
		const FVector4& P1 = Lines[LineIndex].Vertices[0];
		const FVector4& P2 = Lines[LineIndex].Vertices[1];
		const FColor& Color = Lines[LineIndex].Color;
		Vertices[NumVerticesStored+0].Pos[0] = P1[0];
		Vertices[NumVerticesStored+0].Pos[1] = P1[1] - 5.0f;
		Vertices[NumVerticesStored+0].Pos[2] = P1[2];
		Vertices[NumVerticesStored+0].Color1 = 0;
		Vertices[NumVerticesStored+0].Color2 = Color.DWColor();
		Vertices[NumVerticesStored+1].Pos[0] = P1[0];
		Vertices[NumVerticesStored+1].Pos[1] = P1[1] + 5.0f;
		Vertices[NumVerticesStored+1].Pos[2] = P1[2];
		Vertices[NumVerticesStored+1].Color1 = 0;
		Vertices[NumVerticesStored+1].Color2 = Color.DWColor();
		Vertices[NumVerticesStored+2].Pos[0] = P2[0];
		Vertices[NumVerticesStored+2].Pos[1] = P2[1] - 5.0f;
		Vertices[NumVerticesStored+2].Pos[2] = P2[2];
		Vertices[NumVerticesStored+2].Color1 = 0;
		Vertices[NumVerticesStored+2].Color2 = Color.DWColor();
		Vertices[NumVerticesStored+3].Pos[0] = P2[0];
		Vertices[NumVerticesStored+3].Pos[1] = P2[1] + 5.0f;
		Vertices[NumVerticesStored+3].Pos[2] = P2[2];
		Vertices[NumVerticesStored+3].Color1 = 0;
		Vertices[NumVerticesStored+3].Color2 = Color.DWColor();

		Indices[NumIndicesStored+0] = NumVerticesStored+0;
		Indices[NumIndicesStored+1] = NumVerticesStored+2;
		Indices[NumIndicesStored+2] = NumVerticesStored+1;
		Indices[NumIndicesStored+3] = NumVerticesStored+2;
		Indices[NumIndicesStored+4] = NumVerticesStored+3;
		Indices[NumIndicesStored+5] = NumVerticesStored+1;

		NumVerticesStored += 4;
		NumIndicesStored += 6;
	}

	Mesh->UnlockVertexBuffer();
	Mesh->UnlockIndexBuffer();
	Result = D3DXComputeNormals( Mesh, NULL );
	D3DXMATERIAL MeshMaterial;
	MeshMaterial.MatD3D.Ambient.r = 0.1f;
	MeshMaterial.MatD3D.Ambient.g = 0.1f;
	MeshMaterial.MatD3D.Ambient.b = 0.1f;
	MeshMaterial.MatD3D.Ambient.a = 0.0f;
	MeshMaterial.MatD3D.Diffuse.r = 1.0f;
	MeshMaterial.MatD3D.Diffuse.g = 1.0f;
	MeshMaterial.MatD3D.Diffuse.b = 1.0f;
	MeshMaterial.MatD3D.Diffuse.a = 1.0f;
	MeshMaterial.MatD3D.Emissive.r = 1.0f;
	MeshMaterial.MatD3D.Emissive.g = 1.0f;
	MeshMaterial.MatD3D.Emissive.b = 1.0f;
	MeshMaterial.MatD3D.Emissive.a = 1.0f;
	MeshMaterial.MatD3D.Specular.r = 1.0f;
	MeshMaterial.MatD3D.Specular.g = 1.0f;
	MeshMaterial.MatD3D.Specular.b = 1.0f;
	MeshMaterial.MatD3D.Specular.a = 1.0f;
	MeshMaterial.MatD3D.Power = 16.0f;
	MeshMaterial.pTextureFilename = NULL;

	D3DXEFFECTINSTANCE EffectInstance;
	EffectInstance.pEffectFilename = "D3DExport.fx";
	EffectInstance.NumDefaults = 0;
	EffectInstance.pDefaults = NULL;

	// Write out the .x file.
	D3DXSaveMeshToX( Filename, Mesh, NULL, &MeshMaterial, &EffectInstance, 1, D3DXF_FILEFORMAT_BINARY );
	Mesh->Release();

	// Write out the .fx file, if it doesn't always exist.
	HANDLE ShaderFile = CreateFile( TEXT("D3DExport.fx"), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
	if (ShaderFile != INVALID_HANDLE_VALUE)
	{
		::DWORD BytesWritten;
		WriteFile(ShaderFile, ShaderFxFile, (uint32)FCStringAnsi::Strlen(ShaderFxFile), &BytesWritten, NULL);
		CloseHandle( ShaderFile );
	}

	// If specified, run the default viewer for .x files and block until it's closed.
	if ( bShow )
	{
		system( TCHAR_TO_ANSI(Filename) );
	}
}
Exemplo n.º 18
0
ID3DXMesh* GenerateSphere(IDirect3DDevice9* device, float radius, UINT rings, UINT sectors)
{
	if (!device)
	{
		return 0;
	}
	const DWORD numVertexes = rings * sectors;
	const DWORD numFaces = (rings - 1) * (sectors - 1) * 2;
    
    ID3DXMesh* mesh = 0;

    HRESULT hr = D3DXCreateMeshFVF(numFaces,
                      numVertexes,
                      D3DXMESH_32BIT | D3DXMESH_VB_WRITEONLY | D3DXMESH_IB_WRITEONLY,
                      D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,
                      device,
                      &mesh);
    if (FAILED(hr))
    {
        MessageBox(0, L"Unable to generate sphere", L"Application error", MB_ICONSTOP);
        return 0;
    }
    SphereVertex* vertexes = 0;
    DWORD* indexes = 0;

    mesh->LockVertexBuffer(0, (void**)&vertexes);
    mesh->LockIndexBuffer(0, (void**)&indexes);


    float const R = 1.f / (float)(rings - 1);
    float const S = 1.f / (float)(sectors - 1);

    //
    for (UINT ring = 0; ring < rings; ring++)
    {
        for (UINT sector = 0; sector < sectors; sector++)
        {
            float const y = sin(D3DX_PI * 0.5f + D3DX_PI * ring * R );
            float const x = cos(2 * D3DX_PI * sector * S) * sin( D3DX_PI * ring * R );
            float const z = sin(2 * D3DX_PI * sector * S) * sin( D3DX_PI * ring * R );

            SphereVertex* vertex = vertexes++;

            vertex->pos = Vec3(x, y, z) * radius;
            //
            D3DXVec3Normalize(&vertex->normal, &vertex->pos);
            vertex->tex = Vec2(sector / (float)(sectors - 1.0f),
                               ring / (float)(rings - 1.0f));
        }
    }
    //
    for (UINT ring = 0; ring < rings - 1; ++ring)
    {
        for (UINT sector = 0; sector < sectors - 1; ++sector)
        {
            DWORD v0 = ring * sectors + sector;
            DWORD v1 = ring * sectors + sector + 1;
            DWORD v2 = (ring + 1) * sectors + sector;
            DWORD v3 = (ring + 1) * sectors + sector + 1;

            *indexes++ = v0;
            *indexes++ = v1;
            *indexes++ = v2;

            *indexes++ = v2;
            *indexes++ = v1;
            *indexes++ = v3;
        }
    }

    mesh->UnlockIndexBuffer();
    mesh->UnlockVertexBuffer();
    return mesh;
}
Exemplo n.º 19
0
void SphereCylDemo::genCylTexCoords(AXIS axis)
{
	// D3DXCreate* functions generate vertices with position 
	// and normal data.  But for texturing, we also need
	// tex-coords.  So clone the mesh to change the vertex
	// format to a format with tex-coords.

	D3DVERTEXELEMENT9 elements[64];
	UINT numElements = 0;
	VertexPNT::Decl->GetDeclaration(elements, &numElements);

	ID3DXMesh* temp = 0;
	HR(mCylinder->CloneMesh(D3DXMESH_SYSTEMMEM, 
		elements, gd3dDevice, &temp));

	ReleaseCOM(mCylinder);

	// Now generate texture coordinates for each vertex.
	VertexPNT* vertices = 0;
	HR(temp->LockVertexBuffer(0, (void**)&vertices));

	// We need to get the height of the cylinder we are projecting the
	// vertices onto.  That height depends on which axis the client has
	// specified that the cylinder lies on.  The height is determined by 
	// finding the height of the bounding cylinder on the specified axis.

	D3DXVECTOR3 maxPoint(-FLT_MAX, -FLT_MAX, -FLT_MAX);
	D3DXVECTOR3 minPoint(FLT_MAX, FLT_MAX, FLT_MAX);

	for(UINT i = 0; i < temp->GetNumVertices(); ++i)
	{
		D3DXVec3Maximize(&maxPoint, &maxPoint, &vertices[i].pos);
		D3DXVec3Minimize(&minPoint, &minPoint, &vertices[i].pos);
	}

	float a = 0.0f;
	float b = 0.0f;
	float h = 0.0f;
	switch( axis )
	{
	case X_AXIS:
		a = minPoint.x;
		b = maxPoint.x;
		h = b-a;
		break;
	case Y_AXIS:
		a = minPoint.y;
		b = maxPoint.y;
		h = b-a;
		break;
	case Z_AXIS:
		a = minPoint.z;
		b = maxPoint.z;
		h = b-a;
		break;
	}


	// Iterate over each vertex and compute its texture coordinate.

	for(UINT i = 0; i < temp->GetNumVertices(); ++i)
	{
		// Get the coordinates along the axes orthogonal to the
		// axis the cylinder is aligned with.

		float x = 0.0f;
		float y = 0.0f;
		float z = 0.0f;
		switch( axis )
		{
		case X_AXIS:
			x = vertices[i].pos.y;
			z = vertices[i].pos.z;
			y = vertices[i].pos.x;
			break;
		case Y_AXIS:
			x = vertices[i].pos.x;
			z = vertices[i].pos.z;
			y = vertices[i].pos.y;
			break;
		case Z_AXIS:
			x = vertices[i].pos.x;
			z = vertices[i].pos.y;
			y = vertices[i].pos.z;
			break;
		}

		// Convert to cylindrical coordinates.

		float theta = atan2f(z, x);
		float y2    = y - b; // Transform [a, b]-->[-h, 0]

		// Transform theta from [0, 2*pi] to [0, 1] range and
		// transform y2 from [-h, 0] to [0, 1].

		float u = theta / (2.0f*D3DX_PI);
		float v = y2 / -h; 

		// Save texture coordinates.

		vertices[i].tex0.x = u;
		vertices[i].tex0.y = v;
	}

	HR(temp->UnlockVertexBuffer());

	// Clone back to a hardware mesh.
	HR(temp->CloneMesh(D3DXMESH_MANAGED | D3DXMESH_WRITEONLY,
		elements, gd3dDevice, &mCylinder));

	ReleaseCOM(temp);
}
Exemplo n.º 20
0
void World::editTerrain()
{
	BOOL hit = false;
	DWORD faceIndex, hitCount;
	float u, v, dist;
	LPD3DXBUFFER allHits;
	ID3DXMesh* mesh = mTerrain->getMesh()->getMesh();

	if(gInput->keyDown(VK_LBUTTON) || gInput->keyDown(VK_RBUTTON))
	{
		float strength = 300.0f;
		if(gInput->keyDown(VK_RBUTTON))
			strength *= -1;  

		D3DXVECTOR3 origin, dir;
		gInput->getWorldPickingRay(origin, dir);

		D3DXIntersect(mesh, &origin, &dir, &hit, &faceIndex, &u, &v, &dist, &allHits, &hitCount);

		if(hit)
		{
			DWORD* k = 0;

			mesh->LockIndexBuffer(0, (void**)&k);
			int pressed = k[faceIndex*3];
			mesh->UnlockIndexBuffer();

			VertexPNT* v = 0;
			mesh->LockVertexBuffer(0, (void**)&v);

			int size = 10;

			int x = pressed % mTerrain->getColums();
			int z = pressed / mTerrain->getColums();

			for(int i = z - size/2; i < z + size/2; i++)
				for(int j = x - size/2; j < x + size/2; j++) {
					if(i < 0 || j < 0 || i >= mTerrain->getRows() || j >= mTerrain->getColums())
						continue;

					float dist = sqrt((float)(z-i)*(z-i) + (float)(x-j)*(x-j));
					dist = max(dist, 3.0f);

					if(strength > 0)
						v[i*mTerrain->getColums() + j].pos.y += min(strength / dist, strength)/100;
					else
						v[i*mTerrain->getColums() + j].pos.y += max(strength / dist, strength)/100;
				}

			mesh->UnlockVertexBuffer();

			//mTerrain->smothOut(x, z, 10);
		}
	}

	if(gInput->keyPressed(VK_MBUTTON)) {
		D3DXIntersect(mesh, &gCamera->getPosition(), &gCamera->getDirection(), &hit, &faceIndex, &u, &v, &dist, &allHits, &hitCount);

		if(hit)
		{
			DWORD* k = 0;

			mesh->LockIndexBuffer(0, (void**)&k);
			int pressed = k[faceIndex*3];
			mesh->UnlockIndexBuffer();

			VertexPNT* v = 0;
			mesh->LockVertexBuffer(0, (void**)&v);

			int size = 10;

			int x = pressed % mTerrain->getColums();
			int z = pressed / mTerrain->getColums();


			mTerrain->smothOut(x, z, 5);
		}
	}
}