📄 techniqueapplication.cpp
字号:
{
//Delete the shaders
m_pD3DDevice->DeleteVertexShader(m_BasicShader);
//Get rid of all the render target objects
CleanUpTarget();
//Clean up
if (m_pDisplayVertexBuffer)
{
m_pDisplayVertexBuffer->Release();
m_pDisplayVertexBuffer = 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;
}
HRESULT 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);
//Create the shadow render target. This will be used to render the light
//view depth.
if (FAILED(D3DXCreateTexture(m_pD3DDevice, TEX_DIMENSION, TEX_DIMENSION, 1,
D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, &m_pDisplayTexture)))
return E_FAIL;
//Create the target depth buffer
if (FAILED(m_pD3DDevice->CreateDepthStencilSurface(TEX_DIMENSION,
TEX_DIMENSION,
D3DFMT_D24S8,
D3DMULTISAMPLE_NONE,
&m_pDisplayZSurface)))
return E_FAIL;
//Keep a handle to the back buffer for easy swapping
if (FAILED(m_pD3DDevice->GetRenderTarget(&m_pBackBuffer)))
return E_FAIL;
//Same for the depth buffer
if (FAILED(m_pD3DDevice->GetDepthStencilSurface(&m_pZBuffer)))
return E_FAIL;
//Get the top level surface of the target texture.
if (FAILED(m_pDisplayTexture->GetSurfaceLevel(0, &m_pDisplayTextureSurface)))
return E_FAIL;
return S_OK;
}
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()
{
//Don't Clear...
//Call BeginScene to set up the device
m_pD3DDevice->BeginScene();
return;
}
HRESULT CTechniqueApplication::CreateDisplayBuffer()
{
//Also, create the mask vertex buffer
if (FAILED(m_pD3DDevice->CreateVertexBuffer(4 * sizeof(DISPLAY_VERTEX),
0, D3DFVF_DISPLAYVERTEX,
D3DPOOL_MANAGED,
&m_pDisplayVertexBuffer)))
return E_FAIL;
//Create a set of 4 vertices for the texture viewing surface
DISPLAY_VERTEX *pDisplayVertices;
//Lock the vertex buffer, but allow writing.
m_pDisplayVertexBuffer->Lock(0, 4 * sizeof(DISPLAY_VERTEX), (BYTE **)&pDisplayVertices, 0);
//Base the vertex extents on the viewport size. This does not handle cases where the
//viewport may change size.
D3DVIEWPORT8 Viewport;
m_pD3DDevice->GetViewport(&Viewport);
DWORD X1 = Viewport.X;
DWORD Y1 = Viewport.Y;
DWORD X2 = Viewport.X + Viewport.Width;
DWORD Y2 = Viewport.Y + Viewport.Height;
//Also, use the viewport specs to define texture coordinates
float UMax = (float)Viewport.Width / (float)TEX_DIMENSION;
float VMax = (float)Viewport.Height / (float)TEX_DIMENSION;
//Set up the 4 corners of a small square
pDisplayVertices[0].x = X1; pDisplayVertices[0].y = Y1;
pDisplayVertices[0].z = 1.0f; pDisplayVertices[0].rhw = 1.0f;
pDisplayVertices[0].u = 0.0f; pDisplayVertices[0].v = 0.0f;
pDisplayVertices[1].x = X1; pDisplayVertices[1].y = Y2;
pDisplayVertices[1].u = 0.0f; pDisplayVertices[1].v = VMax;
pDisplayVertices[1].z = 1.0f; pDisplayVertices[1].rhw = 1.0f;
pDisplayVertices[2].x = X2; pDisplayVertices[2].y = Y1;
pDisplayVertices[2].u = UMax; pDisplayVertices[2].v = 0.0f;
pDisplayVertices[2].z = 1.0f; pDisplayVertices[2].rhw = 1.0f;
pDisplayVertices[3].x = X2; pDisplayVertices[3].y = Y2;
pDisplayVertices[3].u = UMax; pDisplayVertices[3].v = VMax;
pDisplayVertices[3].z = 1.0f; pDisplayVertices[3].rhw = 1.0f;
pDisplayVertices[0].color = pDisplayVertices[1].color =
pDisplayVertices[2].color = pDisplayVertices[3].color = 0xffffffff;
m_pDisplayVertexBuffer->Unlock();
return S_OK;
}
void CTechniqueApplication::CleanUpTarget()
{
if (m_pDisplayTextureSurface)
{
m_pDisplayTextureSurface->Release();
m_pDisplayTextureSurface = NULL;
}
if (m_pDisplayTexture)
{
m_pDisplayTexture->Release();
m_pDisplayTexture = NULL;
}
if (m_pDisplayZSurface)
{
m_pDisplayZSurface->Release();
m_pDisplayZSurface = NULL;
}
if (m_pBackBuffer)
{
m_pBackBuffer->Release();
m_pBackBuffer = NULL;
}
if (m_pZBuffer)
{
m_pZBuffer->Release();
m_pZBuffer = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -