Some explanation of my code.The function mgral_directx::draw will be called once for each shape whenever the client area needs to be updated (i.e. the applciation receives an WM_PAINT message). The function arguments points and count store the vertex positions of the shape and number of vertices. They vary for each shape. An variable is the type ID3D10Buffer is declared as static as it can only be created only once.
I am looking forward to your kindly help, thanks in advance.
#define USE_DYNAMIC_BUFFER 1
void mgral_directx::draw(int obj_type, mpoint *points, int count)
{
static ID3D10Buffer* iD3dVB=NULL;
std::vector<Vertex> vertices(count);
Vertex* v = 0;
// Assign values to vertex buffer
#ifndef USE_DYNAMIC_BUFFER
for ( int i = 0; i < count; i++ ) {
vertices.pos = D3DXVECTOR3(points.x, points.y, points.z);
vertices.color = D3DXCOLOR(penColor_);
}
#endif
// Create vertex buffer
if ( iD3dVB==NULL ) {
D3D10_BUFFER_DESC vbd;
#ifndef USE_DYNAMIC_BUFFER
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * count;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = &vertices[0];
HR(iD3dDevice_->CreateBuffer(&vbd, &vinitData, &iD3dVB));
#else
vbd.Usage = D3D10_USAGE_DYNAMIC;
vbd.ByteWidth = sizeof(Vertex) * count;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
vbd.MiscFlags = 0;
HR(iD3dDevice_->CreateBuffer(&vbd, 0, &iD3dVB));
#endif
// Create effect
buildFX();
// Create input layout
buildVertexLayouts();
}
// Update vertex buffer
#ifdef USE_DYNAMIC_BUFFER
HR(iD3dVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v ));
for ( int i = 0; i < count; i++ ) {
v.pos = D3DXVECTOR3(points.x, points.y, points.z);
v.color = D3DXCOLOR(penColor_);
}
iD3dVB->Unmap();
#endif
// Restore default states, input layout and primitive topology
// because iD3dxFont_->DrawText changes them. Note that we can
// restore the default states by passing null.
iD3dDevice_->OMSetDepthStencilState(0, 0);
float blendFactors[] = {0.0f, 0.0f, 0.0f, 0.0f};
iD3dDevice_->OMSetBlendState(0, blendFactors, 0xffffffff);
// Bind the input layout to the device
iD3dDevice_->IASetInputLayout(mVertexLayout);
// Specify the primitive topology
iD3dDevice_->IASetPrimitiveTopology(d3dPrimitiveTopologies[obj_type]);
// Bind vertex buffer to the input slot of the device
UINT stride = sizeof(Vertex);
UINT offset = 0;
iD3dDevice_->IASetVertexBuffers(0, 1, &iD3dVB, &stride, &offset);
// set constants
D3DXMATRIX mWVP = modelMatrix_*viewMatrix_*projectMatrix_;
mfxWVPVar->SetMatrix((float*)&mWVP);
// Draw the geometry objects
D3D10_TECHNIQUE_DESC techDesc;
mTech->GetDesc( &techDesc );
for(UINT p = 0; p < techDesc.Passes; ++p) {
mTech->GetPassByIndex( p )->Apply(0);
iD3dDevice_->Draw(count, 0);
}
// Remember to add the line below after all drawing code.
iDxgiSwapChain_->Present(0, 0);
//ReleaseCOM(iD3dVB);
}
void mgral_directx::buildFX()
{
DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3D10_SHADER_DEBUG;
shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif
ID3D10Blob* compilationErrors = 0;
HRESULT hr = 0;
hr = D3DX10CreateEffectFromFile(TEXT("shader.fxo"), 0, 0,
"fx_4_0", shaderFlags, 0, iD3dDevice_, 0, 0, &mFX, &compilationErrors, 0);
if(FAILED(hr)) {
if( compilationErrors ) {
MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
ReleaseCOM(compilationErrors);
}
DXTrace(__FILE__, (DWORD)__LINE__, hr, TEXT("D3DX10CreateEffectFromFile"), true);
}
mTech = mFX->GetTechniqueByName("ColorTech");
mfxWVPVar = mFX->GetVariableByName("gWVP")->AsMatrix();
}
void mgral_directx::buildVertexLayouts()
{
// Create the vertex input layout.
D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
};
// Create the input layout
D3D10_PASS_DESC PassDesc;
mTech->GetPassByIndex(0)->GetDesc(&PassDesc);
HR(iD3dDevice_->CreateInputLayout(vertexDesc, 2, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &mVertexLayout));
}