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

📄 techniqueapplication.cpp

📁 real-time(实时渲染技术DirectX)37-40
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    pD3DXMtrlBuffer->Release();

	//This is new. If the FVF doesn't match, clone the mesh and
	//create one that does. Then, release the loaded mesh. If the 
	//FVF does match, set the member mesh and move on.
	if (pOriginalMesh->GetFVF() != D3DFVF_MESHVERTEX)
	{
		pOriginalMesh->CloneMeshFVF(D3DXMESH_MANAGED,
									D3DFVF_MESHVERTEX,
                                    m_pD3DDevice, &m_pMesh);
		
		pOriginalMesh->Release();
		pOriginalMesh = NULL;
	}
	else
		m_pMesh = pOriginalMesh;

	return S_OK;
}

BOOL CTechniqueApplication::PreReset()
{
	//Delete the shaders
	m_pD3DDevice->DeleteVertexShader(m_BasicShader);

	return TRUE;
}

BOOL CTechniqueApplication::PostReset()
{
	SetupDevice();

	//Recreate the shader
	if (FAILED(CreateShaders()))
		return FALSE;

	return TRUE;
}


BOOL CTechniqueApplication::PreTerminate()
{
	//Delete the shaders
	m_pD3DDevice->DeleteVertexShader(m_BasicShader);
	
	//Clean up
	if (m_pStencilVertexBuffer)
	{
		m_pStencilVertexBuffer->Release();
		m_pStencilVertexBuffer = NULL;
	}

	if (m_pCrossHairTexture)
	{
		m_pCrossHairTexture->Release();
		m_pCrossHairTexture = NULL;
	}

	if (m_pMeshVertexBuffer)
	{
		m_pMeshVertexBuffer->Release();
		m_pMeshVertexBuffer = NULL;
	}

	if (m_pMeshIndexBuffer)
	{
		m_pMeshIndexBuffer->Release();
		m_pMeshIndexBuffer  = NULL;
	}

	if (m_pMesh)
	{
		m_pMesh->Release();
		m_pMesh = NULL;
	}

	if (m_pMeshMaterials)
	{
		delete m_pMeshMaterials;
		m_pMeshMaterials = NULL;
	}

	return TRUE;
}


void CTechniqueApplication::SetupDevice()
{
	//Do all the basic setup
	RECT WindowRect;
	GetClientRect(m_hWnd, &WindowRect);
	D3DXMatrixPerspectiveFovLH(&m_ProjectionMatrix,
					D3DX_PI / 4,
					(float)(WindowRect.right - WindowRect.left) / 
					(float)(WindowRect.bottom - WindowRect.top),
				    1.0f, 1000.0f);

	D3DXMatrixIdentity(&m_WorldMatrix);
}

HRESULT CTechniqueApplication::ExtractBuffers()
{
	//Get the buffers
	m_pMesh->GetVertexBuffer(&m_pMeshVertexBuffer);
	m_pMesh->GetIndexBuffer(&m_pMeshIndexBuffer);

	MESH_VERTEX *pMeshVertices;
	short       *pIndices;
	DWORD       *pAttribs;

	//Lock the vertex buffer, but allow writing.
	m_pMeshVertexBuffer->Lock(0, 
		                      m_pMesh->GetNumVertices() * sizeof(MESH_VERTEX),
		                      (BYTE **)&pMeshVertices, 0);


	//We only need to read the indices
	m_pMeshIndexBuffer->Lock(0, 3 * m_pMesh->GetNumFaces() * sizeof(short),
	                         (BYTE **)&pIndices, D3DLOCK_READONLY);

	//The attribute buffer maps the materials to each face.
	m_pMesh->LockAttributeBuffer(D3DLOCK_READONLY, &pAttribs);

	//Loop through each face and set the vertex color based on the material.
	//This is a pretty simple example, but you could also use this to preload
	//other data, such as converting colors to data that the vertex shader
	//may use in computations.
	for (long Face = 0; Face < m_pMesh->GetNumFaces(); Face++)
	{
		D3DXCOLOR Diffuse = (D3DXCOLOR)m_pMeshMaterials[pAttribs[Face]].Diffuse;

		pMeshVertices[pIndices[Face * 3 + 0]].color = Diffuse;
		pMeshVertices[pIndices[Face * 3 + 1]].color = Diffuse;
		pMeshVertices[pIndices[Face * 3 + 2]].color = Diffuse;
	}

	//Give back all of our buffers.
	m_pMeshVertexBuffer->Unlock();
	m_pMeshIndexBuffer->Unlock();
	m_pMesh->UnlockAttributeBuffer();

	return S_OK;
}

HRESULT CTechniqueApplication::CreateShaders()
{
	//Set up the declaration to match the FVF and to
	//read from stream zero.
	DWORD Declaration[] =
	{
		D3DVSD_STREAM(0),
		D3DVSD_REG(D3DVSDE_POSITION,D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_NORMAL,  D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
		D3DVSD_END()
	};

	
	ID3DXBuffer* pShaderBuffer;
	ID3DXBuffer* pShaderErrors;

	//Assemble and create the first shader. Under real circumstances, you would 
	//probably want to do more error handling.
	if (FAILED(D3DXAssembleShaderFromFile("..\\media\\Shaders\\Basic.vsh", 
		                            0, NULL, &pShaderBuffer, &pShaderErrors)))
		return E_FAIL;

	if (FAILED(m_pD3DDevice->CreateVertexShader(Declaration, 
		                           (DWORD *)pShaderBuffer->GetBufferPointer(),
								   &m_BasicShader, 0)))
		return E_FAIL;

	//release the working buffers
	pShaderBuffer->Release();

	return S_OK;
}

void CTechniqueApplication::PreRender()
{
	//Clear the device
	m_pD3DDevice->Clear(0, NULL,
						D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
						D3DCOLOR_XRGB(100, 100, 200), 1.0f, 0);

	//Call BeginScene to set up the device
	m_pD3DDevice->BeginScene();
		
	return;
}


HRESULT CTechniqueApplication::CreateStencilVertexBuffer()
{
	//Create as managed so we don't have to worry about recreating it
	//if the device is lost.
	if (FAILED(m_pD3DDevice->CreateVertexBuffer(NUM_VERTICES * sizeof(STENCIL_VERTEX), 
		                                        0, D3DFVF_STENCILVERTEX,
									            D3DPOOL_MANAGED,
												&m_pStencilVertexBuffer)))
		return E_FAIL;

	//Create a set of 4 vertices for the plane
	STENCIL_VERTEX *pVertices;

	//Lock the vertex buffer, but allow writing.
	m_pStencilVertexBuffer->Lock(0, NUM_VERTICES * sizeof(STENCIL_VERTEX),
		                         (BYTE **)&pVertices, 0);

	//We'll use a fan. Define the center of the circle. Presently, the viewport
	//is 200 by 150, so the center is 500, 375
	pVertices[0].rhw = 1.0f;
	pVertices[0].x = 500.0f;
	pVertices[0].y = 375.0f;
	pVertices[0].z = 1.0f;

	//Now, define all the surrounding vertices except for the last one.
	//Make the circle with a radius of 70.0
	for (long CirclePoint = 1; CirclePoint < CIRCLE_RES - 1; CirclePoint++)
	{
		float Angle = 2.0f * D3DX_PI * (float)(CirclePoint - 1) / (CIRCLE_RES - 2);

		pVertices[CirclePoint].x   = 500.0f + (70.0f * cos(Angle));
		pVertices[CirclePoint].y   = 375.0f + (70.0f * sin(Angle));
		pVertices[CirclePoint].z   = 1.0f;
		pVertices[CirclePoint].rhw = 1.0f;
	}

	//Define the last one 
	pVertices[CirclePoint].x   = 570.0f;
	pVertices[CirclePoint].y   = 375.0f;
	pVertices[CirclePoint].z   = 1.0f;
	pVertices[CirclePoint].rhw = 1.0f;

	//Define the final four points that make up the crosshair image.
	//These values are all "empirical"
	pVertices[CirclePoint + 1].x     = 422.0f;
	pVertices[CirclePoint + 1].y     = 298.0f;
	pVertices[CirclePoint + 1].u     = 0.0f;
	pVertices[CirclePoint + 1].v     = 0.0f;
	pVertices[CirclePoint + 2].x     = 578.0f;
	pVertices[CirclePoint + 2].y     = 298.0f;
	pVertices[CirclePoint + 2].u     = 1.0f;
	pVertices[CirclePoint + 2].v     = 0.0f;
	pVertices[CirclePoint + 3].x     = 422.0f;
	pVertices[CirclePoint + 3].y     = 452.0f;
	pVertices[CirclePoint + 3].u     = 0.0f;
	pVertices[CirclePoint + 3].v     = 1.0f;
	pVertices[CirclePoint + 4].x     = 578.0f;
	pVertices[CirclePoint + 4].y     = 452.0f;
	pVertices[CirclePoint + 4].u     = 1.0f;
	pVertices[CirclePoint + 4].v     = 1.0f;

	pVertices[CirclePoint + 1].z = pVertices[CirclePoint + 2].z =
	pVertices[CirclePoint + 3].z = pVertices[CirclePoint + 4].z	= 1.0f;

	pVertices[CirclePoint + 1].rhw = pVertices[CirclePoint + 2].rhw =
	pVertices[CirclePoint + 3].rhw = pVertices[CirclePoint + 4].rhw	= 1.0f;

	m_pStencilVertexBuffer->Unlock();

	return S_OK;
}

⌨️ 快捷键说明

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