Wednesday, November 30, 2011

DirectX-Draw Triangle Note

There three main function need to be use.


void InitPipeline()
{
// load and compile the two shaders
ID3D10Blob *VS,*PS;
D3DX11CompileFromFile("shaders.hlsl",0,0,"VShader","vs_4_0",0,0,0,&VS,0,0);
D3DX11CompileFromFile("shaders.hlsl",0,0,"PShader","ps_4_0",0,0,0,&PS,0,0);

// encapsulate both shaders into shader objects
dev->CreateVertexShader(VS->GetBufferPointer(),VS->GetBufferSize(),NULL,&pVS);
dev->CreatePixelShader(PS->GetBufferPointer(),PS->GetBufferSize(),NULL,&pPS);

// set the shader objects
devcon->VSSetShader(pVS,0,0);
devcon->PSSetShader(pPS,0,0);

//Create Input layout object,Created input element descriptions for the position and the color.
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0},
};
//Created an input layout object using the shader information.
dev->CreateInputLayout(ied,2,VS->GetBufferPointer(),VS->GetBufferSize(),&pLayout);

//Set the input layout object.
devcon->IASetInputLayout(pLayout);
}


Its do :
1.Loaded And Compiled with two shader
2.Create Object for each one
3.Set the two object

2.
Global


struct VERTEX{FLOAT X,Y,Z;D3DXCOLOR Color;};
ID3D11Buffer *pVBuffer;


void InitGraphics()
{
// create a triangle using the VERTEX struct
VERTEX OurVertices[] =
{
{0.0f,0.5f,0.0f,D3DXCOLOR(1.0f,0.0f,0.0f,1.0f)},
{0.45f,-0.5f,0.0f,D3DXCOLOR(0.0f,1.0f,0.0f,1.0f)},
{-0.45f,-0.5f,0.0f,D3DXCOLOR(0.0f,0.0f,1.0f,1.0f)}
};

//Created a vertex buffer object.
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd,sizeof(bd));

bd.Usage = D3D11_USAGE_DYNAMIC;// access by CPU and GPU
bd.ByteWidth = sizeof(VERTEX) *3;//size is the vertex struct * 3
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;//use as vertex buffer
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;// allow CPU to write in buffer

dev->CreateBuffer(&bd,NULL,&pVBuffer);


//vertices into a buffer ,copy the vertices into the buffer
D3D11_MAPPED_SUBRESOURCE ms;
devcon->Map(pVBuffer,NULL,D3D11_MAP_WRITE_DISCARD,NULL,&ms);
memcpy(ms.pData,OurVertices,sizeof(OurVertices));
devcon->Unmap(pVBuffer,NULL);

}


It do:
1.Create three vertex with both position
2.Create vertex buffer object
3.Copied the vertexs into vertex buffer by mapping it

At EnterFrame


void RenderFrame(void)
{
        ............

//select which vertex buffer to display,Set which vertex buffer to use
UINT stride = sizeof(VERTEX);
UINT offset = 0;
devcon->IASetVertexBuffers(0,1,&pVBuffer,&stride,&offset);

//select which primtive type we are using
devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

//draw the vertex buffer to the back buffer,Draw the triangle.
devcon->Draw(3,0);



//swtich the back buffer and front buffer
swapchain->Present(0,0);
}



void UnLoad3D()
{
swapchain->SetFullscreenState(FALSE,NULL);

pLayout->Release();

pVS->Release();
pPS->Release();
pVBuffer->Release();
swapchain -> Release();
backbuffer->Release();
dev ->Release();
devcon->Release();
}

No comments:

Post a Comment