//****************************************************************************
//说明:
//开发环境:Visual C++.NET 7.1.3091 DirectX 9.0C
//操作系统:WindowsXP SP2
//硬件:AMD Athlon XP 2500+,DDR400 256MB,Geforce4 MX440 AGP 8X 64MB
//****************************************************************************
#include<d3d9.h>
#include<d3dx9.h>
#include<windows.h>
#include<d3d9types.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"Winmm.lib")
//变量说明
HWND hWnd;
LPDIRECT3D9 D3d9;
LPDIRECT3DDEVICE9 D3ddevice9;
LPDIRECT3DVERTEXBUFFER9 g_pVB;
LPDIRECT3DINDEXBUFFER9 g_pIB;
//函数申明
BOOL InitWindow(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
//定义FVF结构体
struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
//*******************************************************************
//函数:InitWindow()
//功能:创建窗口
//编写人:梁志斌
//*******************************************************************
static BOOL InitWindow(HINSTANCE hInstance,int nCmdShow)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0; //分配给窗口类结构之后的额外字节数
wndcls.cbWndExtra=0; //分配给窗口实例之后的额外的字节数
wndcls.hbrBackground=CreateSolidBrush(RGB(0,0,255));//蓝色背景
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口中的鼠标形状
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); //窗口图标
wndcls.hInstance=hInstance; //窗口所对应的应用程序的句柄
wndcls.lpfnWndProc=(WNDPROC)WinProc; //窗口消息处理函数的指针
wndcls.lpszClassName="DirectX"; //窗口的类名称
wndcls.lpszMenuName=NULL; //窗口的菜单资源名称
wndcls.style= NULL; //窗口的风格
if(!RegisterClass(&wndcls))
{
::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
return FALSE;
}
hWnd=CreateWindow("DirectX","游戏主窗口",WS_POPUP|WS_MAXIMIZE,0,0,
GetSystemMetrics(SM_CXSCREEN),//此函数返回屏幕宽度
GetSystemMetrics(SM_CYSCREEN),//此函数返回屏幕高度
GetDesktopWindow(),NULL,hInstance,NULL);
if(!hWnd)
{
::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
return FALSE;
}
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
return TRUE;
}
//**********************************************************************
//函数:InitD3D()
//功能:初始化D3D
//编写人:梁志斌
//**********************************************************************
bool InitD3D()
{
if(NULL==(D3d9=Direct3DCreate9(D3D_SDK_VERSION)))
{
::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
return FALSE;
}
D3DDISPLAYMODE d3ddm;
if(D3D_OK!=(D3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
{
::MessageBox(0,"GetAdapterDisplayMode() - FAILED",0,0);
return FALSE;
}
D3DPRESENT_PARAMETERS D3dpp;
ZeroMemory(&D3dpp, sizeof(D3dpp));
D3dpp.Windowed=false;
D3dpp.hDeviceWindow=hWnd;
D3dpp.SwapEffect=D3DSWAPEFFECT_FLIP;
D3dpp.BackBufferCount=1; //有一个后台缓存
D3dpp.BackBufferWidth=d3ddm.Width;//屏幕宽度
D3dpp.BackBufferHeight=d3ddm.Height;//屏幕长度
//立即显示刷新后的图像
D3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
D3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;//色彩深度
//开启自动深度缓冲
D3dpp.EnableAutoDepthStencil=true;
D3dpp.AutoDepthStencilFormat=D3DFMT_D16;//生成16 bit的Z缓存
if(D3D_OK!=(D3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3dpp,&D3ddevice9)))
{
::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
return FALSE;
}
//启用Z缓存,允许消隐处理
D3ddevice9->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
//关闭"挑选"功能,允许渲染背面
D3ddevice9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
//因为使用顶点颜色渲染,所以要禁用光照处理
D3ddevice9->SetRenderState(D3DRS_LIGHTING,FALSE);
return true;
}
HRESULT InitVB()
{
//三棱锥的数学模型
CUSTOMVERTEX Vertices[] = //FVF顶点数据
{{ 0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0,255,0) }, //点A,绿色
{ -1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(255,0,0) }, //点B,红色
{ 1.0f, -1.0f, -0.577f, D3DCOLOR_XRGB(0,255,255) }, //点C,浅蓝
{ 0.0f, -1.0f, 1.155f, D3DCOLOR_XRGB(255,0,255) }}; //点D,粉红
WORD indices[] = { 0, 2, 1, 0, 3, 2, 0, 1, 3, 1, 2, 3 }; //索引序列
//创建顶点缓冲
if(D3D_OK!=(D3ddevice9->CreateVertexBuffer(sizeof(Vertices),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL)))
{
::MessageBox(0, "CreateVertexBuffer() - FAILED", 0, 0);
return FALSE;
}
//填充顶点
VOID* pVertices;
if(FAILED(g_pVB->Lock(0,sizeof(Vertices),(void**)&pVertices,0)))
{
::MessageBox(0, "Lock() - FAILED", 0, 0);
return FALSE;
}
memcpy(pVertices,Vertices,sizeof(Vertices));
g_pVB->Unlock();
//创建索引缓存区,并获取接口LPDIRECT3DINDEXBUFFER9的指针
D3ddevice9->CreateIndexBuffer(
sizeof(indices), //缓存区尺寸
0, D3DFMT_INDEX16, //使用16 bit的索引值
D3DPOOL_DEFAULT, &g_pIB, NULL );
//把索引值填入索引缓存区
void *pIndices;
g_pIB->Lock( 0, sizeof(indices), (void**)&pIndices, 0 );
memcpy( pIndices, indices, sizeof(indices) );
g_pIB->Unlock();
return S_OK;
}
void SetupMatrices()
{
D3DXMATRIXA16 matWorld;
UINT iTime = timeGetTime() % 5000;
FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;//把旋转角换算成弧度
D3DXMatrixRotationY( &matWorld, fAngle );//计算世界变换矩阵
D3ddevice9->SetTransform( D3DTS_WORLD, &matWorld );//把世界变换矩阵设置到渲染环境
D3DXVECTOR3 vEyePt( 0.0f, 1.0f,-5.0f );//观察点
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );//视线目标点
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );//上方向
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );//计算视角变换矩阵
D3ddevice9->SetTransform( D3DTS_VIEW, &matView );//把视角变换矩阵设置到渲染环境
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );//计算透视投影变换矩阵
D3ddevice9->SetTransform( D3DTS_PROJECTION, &matProj );//把投影变换矩阵设置到渲染环境
}
VOID Cleanup()
{
if(g_pVB!= NULL)
g_pVB->Release();
if(g_pIB!= NULL)
g_pIB->Release();
if(D3ddevice9!=NULL)
D3ddevice9->Release();
if(D3d9!=NULL)
D3d9->Release();
}
VOID Render()
{
//用蓝色清空背景区域
D3ddevice9->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0);
// 开始绘制场景
if(SUCCEEDED(D3ddevice9->BeginScene()))
{
SetupMatrices();//设置变换矩阵
D3ddevice9->SetFVF(D3DFVF_CUSTOMVERTEX);//设置自定义的FVF
D3ddevice9->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));//绑定顶点缓存区至设备数据源
//绑定索引缓存区
D3ddevice9->SetIndices( g_pIB );
//从索引缓存区绘制图元,参数1为图元格式,参数4为顶点数,参数6为三角形数
D3ddevice9->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 4, 0, 4 );
}
// 结束场景绘制
D3ddevice9->EndScene();
// 显示到屏幕上
D3ddevice9->Present(NULL,NULL,NULL,NULL);
}
//**********************************************************************
//函数:WinMain()
//功能:Windows 程序入口函数。创建主窗口,处理消息循环
//编写人:梁志斌
//**********************************************************************
int WINAPI WinMain(HINSTANCE hInstance, //hInstance:表示该应用程序的句柄
HINSTANCE hPrevInstance, //hPrevInstance:为了保持与16位windows应用程序的兼容性
LPSTR lpCmdLine, //lpCmdLine:指向命令行参数字符串的指针
int nCmdShow) //nCmdShow:指定应用程序窗口显示方式的整数
{
InitWindow(hInstance,nCmdShow);
InitD3D();
InitVB();
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while(GetMessage(&msg,NULL,0,0))
{
Render(); //渲染
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int)msg.wParam;
}
//*******************************************************************
//函数:WinProc()
//功能:处理窗口消息
//编写人:梁志斌
//*******************************************************************
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN: //击键消息
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hWnd); //释放窗口
Cleanup();
PostQuitMessage(0); //给窗口发送WM_QUIT消息
return 0; //处理完一个消息后返回0
}
//case WM_RBUTTONDOWN:
// MessageBox(hWnd,"鼠标右键按下了!","Mouse",MB_OK);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam); //调用缺省消息处理过程
}