Here's my vertex shader:
cbuffer cbObject : register(b0) {
matrix mWorld : packoffset(c0);
};
cbuffer cbFrame : register(b1) {
matrix mView : packoffset(c0);
matrix mProjection : packoffset(c4);
};
struct VS_INPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
};
struct VS_OUTPUT {
float4 Position : SV_POSITION;
float3 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
};
VS_OUTPUT MainVS(VS_INPUT input) {
VS_OUTPUT output;
output.Position = mul(input.Position, mul(mWorld, mul(mView, mProjection)));
output.Normal = mul(input.Normal, mWorld);
output.Tex0 = input.Tex0;
return output;
}
Inside OnD3D11CreateDevice I setup camera parameters:
D3DXVECTOR3 vecEye( 0.0f, 50.0f, 50.0f );
D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f );
g_Camera.SetViewParams( &vecEye, &vecAt );
Same with OnD3D11ResizedSwapChain:
float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height;
g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f );
g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON );
Before calling Draw I setup some parameters:
// shader
deviceContext->VSSetShader(pVertexShader, 0, 0);
deviceContext->PSSetShader(pPixelShader, 0, 0);
extern CModelViewerCamera g_Camera;
D3D11_MAPPED_SUBRESOURCE MappedResource;
// frame's view and proj matrices
deviceContext->Map(bufferFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
cbFrame *pPerFrame = (cbFrame*)MappedResource.pData;
pPerFrame->mView = *g_Camera.GetViewMatrix();
pPerFrame->mProjection = *g_Camera.GetProjMatrix();
deviceContext->Unmap(bufferFrame, 0);
deviceContext->VSSetConstantBuffers(1, 1, &bufferFrame);
// primitive topology, vertex format and index buffer
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
deviceContext->IASetInputLayout(vertexFormat);
deviceContext->IASetIndexBuffer(bufferIndex, DXGI_FORMAT_R16_UINT, 0);
// world matrix
deviceContext->Map(bufferObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
cbObject *pPerObject = (cbObject*)MappedResource.pData;
pPerObject->mWorld = pHeightmap->mWorld;
deviceContext->Unmap(bufferObject, 0);
deviceContext->VSSetConstantBuffers(0, 1, &bufferObject);
// vertex buffer
UINT stride = sizeof(Vertex);
UINT offset = 0;
deviceContext->IASetVertexBuffers(0, 1, &bufferVertex, &stride, &offset);
// and finally draw
deviceContext->DrawIndexed(127 * 127 * 2 * 3, 0, 0);
I'm not sure what other information I have to provide. Let me know if I missed anything.
Thank you in advance.