// @time  2012.3.5
// @author jadeshu
//包含头文件
#include <Windows.h>
#include <d3d9.h>
#pragma comment(lib,"d3d9.lib")
//自定义
#define ReleaseD3D(x) if (x!=NULL){x->Release();x=NULL;} //定义设备接口
IDirect3DDevice9 * g_pDevic=NULL;
//定义顶点缓冲
IDirect3DVertexBuffer9 * g_pVertexBuff=NULL;
//定义索引缓冲
IDirect3DIndexBuffer9 * g_pIndexBuff=NULL;
//定义着色模式选择
BOOL g_bFlat=FALSE;
//定义填充模式选择
int g_iFillMode=3;
//定义结构体
struct CUSTOMVERTEX
{
FLOAT _x,_y,_z,_rhw;
DWORD _color;
CUSTOMVERTEX(FLOAT x,FLOAT y,FLOAT z,FLOAT rhw,DWORD color)
:_x(x),_y(y),_z(z),_rhw(rhw),_color(color){}
};
//定义顶点灵活格式
#define CUSTOMVERTEX_FVF (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
//回调函数声明
LRESULT CALLBACK WinProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
//初始化Direct3D函数声明
HRESULT InitD3D(HWND hwnd);
//渲染声明
VOID Render();
//清除对象声明
VOID Release3D(); //主函数,入口函数
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//定义窗口类结构
TCHAR szAppName[]=TEXT("ClassN");
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
wc.cbClsExtra=0;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(hInstance,IDC_ARROW);
wc.hIcon=LoadIcon(hInstance,IDI_APPLICATION);
wc.hIconSm=NULL;
wc.hInstance=hInstance;
wc.lpfnWndProc=WinProc;
wc.lpszClassName=szAppName;
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW|CS_VREDRAW;
//注册窗口
RegisterClassEx(&wc);
//创建窗口
hwnd=CreateWindow(szAppName,TEXT("第一个DirectX程序"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,0);
//加载3D初始化
//if (SUCCEEDED(InitD3D(hwnd)))
//{
//显示和更新窗口
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
//消息循环
while(msg.message!=WM_QUIT)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//转换消息
TranslateMessage(&msg);
//投递消息
DispatchMessage(&msg);
}
else
{
//渲染
Render();
}
}
/*}*/
////清除对象
//Release3D();
//删除注册窗口
UnregisterClass(szAppName,hInstance);
return msg.wParam; }
//回调函数
LRESULT CALLBACK WinProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
case WM_CREATE:
if (FAILED(InitD3D(hwnd)))
{
return D3D_OK;
}
return 0;
case WM_LBUTTONDOWN:
g_bFlat=!g_bFlat; //着色模式取反(平面模式和高洛德模式)
return 0;
case WM_RBUTTONDOWN:
if (1==g_iFillMode)
{
g_iFillMode=2;
}
else if (2==g_iFillMode)
{
g_iFillMode=3;
}
else if (3==g_iFillMode)
{
g_iFillMode=1;
}
return 0;
case WM_DESTROY:
//清除对象
Release3D();
//退出消息循环
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
} HRESULT InitD3D(HWND hwnd)
{
//定义接口
IDirect3D9 * g_pD3D=NULL;
//创建对象接口
if (NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
{
return E_FAIL;
} //获取硬件设备信息
D3DCAPS9 caps;
g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
DWORD vp=0;
if (caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
{
vp=D3DCREATE_HARDWARE_VERTEXPROCESSING; //硬件顶点运算处理(3D处理方式)
}
else
{
vp=D3DCREATE_SOFTWARE_VERTEXPROCESSING; //软件顶点运算处理(3D处理方式)
}
//创建Direct3D设备接口
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed=TRUE;
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,vp,&d3dpp,&g_pDevic)))
{
return E_FAIL;
} //创建顶点缓冲区
if (FAILED(g_pDevic->CreateVertexBuffer(6*sizeof(CUSTOMVERTEX),0,CUSTOMVERTEX_FVF,D3DPOOL_DEFAULT,&g_pVertexBuff,0)))
{
return E_FAIL;
}
//填充顶点缓冲区
CUSTOMVERTEX * pvertexs=NULL;
g_pVertexBuff->Lock(0,0,(void **)&pvertexs,0);
pvertexs[0]=CUSTOMVERTEX(220.0f,120.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0)); //V0
pvertexs[1]=CUSTOMVERTEX(420.0f,120.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); //V1
pvertexs[2]=CUSTOMVERTEX(220.0f,320.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0)); //V2 //pvertexs[3]=CUSTOMVERTEX(420.0f,120.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0)); //V1
pvertexs[3]=CUSTOMVERTEX(420.0f,320.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,0,255)); //V3
//pvertexs[5]=CUSTOMVERTEX(220.0f,320.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0));//V2
g_pVertexBuff->Unlock(); //创建索引缓冲区
if (FAILED(g_pDevic->CreateIndexBuffer(6*sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndexBuff,0)))
{
return E_FAIL;
}
//填充索引缓冲区
WORD Indices[]={0,1,2,1,3,2};
void *pIndices=NULL;
g_pIndexBuff->Lock(0,sizeof(Indices),(void **)&pIndices,0);
memcpy(pIndices,Indices,sizeof(Indices));
/*g_pIndexBuff->Lock(0,0,(void **)&pIndices,0);
pIndices[0]=0,pIndices[1]=1,pIndices[2]=2;
pIndices[3]=1,pIndices[4]=3,pIndices[5]=2;*/
g_pIndexBuff->Unlock();
//释放对象g_pD3D
ReleaseD3D(g_pD3D);
return S_OK;
}
//渲染图形
VOID Render()
{
//设置着色模式
if (g_bFlat)
{
g_pDevic->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
}
else
{
g_pDevic->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
}
//设置填充模式
if (1==g_iFillMode)
{
//点填充
g_pDevic->SetRenderState(D3DRS_FILLMODE,D3DFILL_POINT);
}
else if (2==g_iFillMode)
{
//线框填充
g_pDevic->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
}
else if (3==g_iFillMode)
{
//面填充
g_pDevic->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
}
//清除后台缓冲区
g_pDevic->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(150,150,250),0,0);
//开始绘制
g_pDevic->BeginScene(); g_pDevic->SetStreamSource(0,g_pVertexBuff,0,sizeof(CUSTOMVERTEX));
g_pDevic->SetFVF(CUSTOMVERTEX_FVF);
/*g_pDevic->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);*/ g_pDevic->SetIndices(g_pIndexBuff);
g_pDevic->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);
//结束绘制
g_pDevic->EndScene();
//从后台缓冲区复制到前台----翻转/提交
g_pDevic->Present(NULL,NULL,NULL,NULL);
} //清除对象
VOID Release3D()
{
ReleaseD3D(g_pIndexBuff);
ReleaseD3D(g_pVertexBuff);
ReleaseD3D(g_pDevic);
//ReleaseD3D(g_pD3D);
}
05-11 11:09