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();
}

Monday, November 28, 2011

DirextX -Begin D3D

The DirectX Begin


#include windows.h
#include windowsx.h
#include d3d11.h
#include d3d10.h
#include d3d11.h
#include d3dx10.h
#include d3dx11.h
#include windows.h

#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dx11.lib")
#pragma comment(lib,"d3dx10.lib")

IDXGISwapChain *swapchain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;

void InitD3D(HWND hWnd);
void CleanD3D(void);

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lPram);


IDXGISwapChain: The swap chain is the series of buffers which take turns being rendered on.

ID3D11Device:This variable is a pointer to a device.

ID3D11DeviceContext:A device context is similar to a device, but it is responsible for managing the GPU and the rendering pipeline (the device mostly handles video memory).

Initialize D3D


void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;

ZeroMemory(&scd,sizeof(DXGI_SWAP_CHAIN_DESC));

//Swap chain struct
scd.BufferCount = 1;//BackBuffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;//32bit-buffer
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; //how swap chain is to be used
scd.OutputWindow = hWnd;//window to be use
scd.SampleDesc.Count = 2;//How many multisample  anti-aliased
scd.Windowed = TRUE;//Screen Full or not
//scd.Flags  = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;//use alt + enter   DXGI_SWAP_CHAIN_FLAG_NONPREROTATED moniter rotation
D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,NULL,NULL,NULL,D3D11_SDK_VERSION,&scd,&swapchain,&dev,NULL,&devcon);
}



UnLoad3D


void Clean3D()
{
swapchain -> Release();
dev ->Release();
devcon->Release();
}



Program Main



int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc,sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

RECT wr = {0,0,1280,720};
AdjustWindowRect(&wr,WS_OVERLAPPEDWINDOW,FALSE);

hWnd  = CreateWindowEx(NULL,"WindowClass","RealDirectX",WS_OVERLAPPEDWINDOW,300,300,wr.right - wr.left,wr.bottom - wr.top,NULL,NULL,hInstance,NULL);

ShowWindow(hWnd,nCmdShow);

InitD3D(hWnd);

MSG msg;

while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);

DispatchMessage(&msg);

if(msg.message == WM_QUIT)
break;
}
else
{

}
}

Clean3D();
return msg.wParam;
}



Window Program


LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}break;
}

return DefWindowProc(hWnd,message,wParam,lParam);
}


1scd.Flags:Three property
                 1.DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
                 2.DXGI_SWAP_CHAIN_FLAG_NONPREROTATED
                 3.DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE
2.scd.SampleDesc.Count: Max Number is 4 Min Number is 1 ,Setting Multisample anti-aliased

----------------------- Using Back Buffer with Deep Blue

1.Setting RenderTarget
2.Setting ViewPort
3.RenderFrame

In header i add

ID3D11RenderTargetView *backbuffer;
void RenderFrame(void);


 InitD3D(HWND hWnd)
   Add Render Target &ViewPort

void InitD3D(HWND hWnd)
{
     //........

//Setting Render Target

ID3D11Texture2D *pBackBuffer;
swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),(LPVOID*)&pBackBuffer);

dev->CreateRenderTargetView(pBackBuffer,NULL,&backbuffer);
pBackBuffer->Release();
devcon->OMSetRenderTargets(1,&backbuffer,NULL);



//Setting ViewPort
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport,sizeof(D3D11_VIEWPORT));
viewport.MaxDepth = 1.0f;
viewport.MinDepth = 0.0f;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = 1280;
viewport.Height = 720;

devcon->RSSetViewports(1,&viewport);

}



void RenderFrame(void)
{
//Clear the back buffer in deep blue
devcon->ClearRenderTargetView(backbuffer,D3DXCOLOR(0.0f,0.2f,0.4f,1.0f));

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



In game Logical Add

RenderFrame And done

Friday, November 4, 2011

C# Interface & abstract Difference

以Tower Defend遊戲中的房屋:





Interface:
      定義不同種類的物件中,針對某種特性,所需要具備的相同功能。
      介面是對行為的抽象:任何類別若繼承介面並且實做後,皆具有此功能。
                   1.All the Mothod , property , event must be public。
                   2. 繼承Interface 物件,New 出一個interface 物件,Interface 內的method property 一定要被實做出來。
                   3.多重繼承


Abstract:
      介面是對行為的抽象:任何類別若繼承介面並且實做後,皆具有此功能。
                   1. abstract class can be private or protected
                   2.you can new a abstract class but not doing anything



----------------------------------------------Edit Continue