DirectX 3D 基本框架(二)

来源:互联网 发布:java编辑器哪个好 编辑:程序博客网 时间:2024/05/18 09:19

对上次所用的DirectX 3D基本框架进行一定的扩充,修改如下

d3dUtility.h中

1.增加了一些常用颜色的常量。

2.增加了3个光照函数。分别是初始化点光源,方向光及聚光灯。

3.增加了初始化材质函数及一些常见的材质常量。

 

d3dUtility.cpp中

实现了.h文件中声明的新增的4个函数。

 

代码清单:

  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: d3dUtility.h
  4. // by tianzhihen 
  5. // 2008.10.9
  6. // MSVC++ 8.0
  7. //////////////////////////////////////////////////////////////////////////////////////////////////
  8. #ifndef __d3dUtilityH
  9. #define __d3dUtilityH
  10. #include <d3dx9.h>
  11. namespace d3d
  12. {
  13.     bool InitD3D(
  14.         HINSTANCE hInstance,    //应用程序实例
  15.         int width, int height,  //后备缓冲宽/高
  16.         bool windowed,          //显示模式(窗口/全屏)
  17.         D3DDEVTYPE deviceType,  //设备类型(HAL/REF)
  18.         IDirect3DDevice9** device   //指向设备(显卡)接口地址的指针
  19.         );
  20.     //进入消息循环
  21.     int EnterMsgLoop(
  22.         bool (*ptr_display)(float timeDelta));
  23.     //回调函数
  24.     LRESULT CALLBACK WndProc(
  25.         HWND hwnd,
  26.         UINT msg,
  27.         WPARAM wParam,
  28.         LPARAM lParam
  29.         );
  30.     template<class T> void Release(T t)
  31.     {
  32.         if (t)
  33.         {
  34.             t->Release();
  35.             t = 0;
  36.         }
  37.     }
  38.     template<class T> void Delete(T t)
  39.     {
  40.         if (t)
  41.         {
  42.             delete t;
  43.             t = 0;
  44.         }
  45.     }
  46.     //
  47.     // 颜色常量
  48.     //
  49.     const D3DXCOLOR      WHITE( D3DCOLOR_XRGB(255, 255, 255) );
  50.     const D3DXCOLOR      BLACK( D3DCOLOR_XRGB(  0,   0,   0) );
  51.     const D3DXCOLOR        RED( D3DCOLOR_XRGB(255,   0,   0) );
  52.     const D3DXCOLOR      GREEN( D3DCOLOR_XRGB(  0, 255,   0) );
  53.     const D3DXCOLOR       BLUE( D3DCOLOR_XRGB(  0,   0, 255) );
  54.     const D3DXCOLOR     YELLOW( D3DCOLOR_XRGB(255, 255,   0) );
  55.     const D3DXCOLOR       CYAN( D3DCOLOR_XRGB(  0, 255, 255) );
  56.     const D3DXCOLOR    MAGENTA( D3DCOLOR_XRGB(255,   0, 255) );
  57.     
  58.     //
  59.     // 光照函数
  60.     //
  61.     D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color);
  62.     D3DLIGHT9 InitPointLight(D3DXVECTOR3* position,D3DXCOLOR* color);
  63.     D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position,D3DXVECTOR3* direction,D3DXCOLOR* color);
  64.     //
  65.     // 材质 函数/常量
  66.     //
  67.     D3DMATERIAL9 InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p);
  68.     const D3DMATERIAL9 WHITE_MTRL  = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
  69.     const D3DMATERIAL9 RED_MTRL    = InitMtrl(RED, RED, RED, BLACK, 2.0f);
  70.     const D3DMATERIAL9 GREEN_MTRL  = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
  71.     const D3DMATERIAL9 BLUE_MTRL   = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
  72.     const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
  73. }
  74. #endif
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: d3dUtility.cpp
  4. // by tianzhihen 
  5. // 2008.10.9
  6. // MSVC++ 8.0
  7. //////////////////////////////////////////////////////////////////////////////////////////////////
  8. #include "d3dUtility.h"
  9. bool d3d::InitD3D(HINSTANCE hInstance, 
  10.                   int width, int height, 
  11.                   bool windowed, 
  12.                   D3DDEVTYPE deviceType,
  13.                   IDirect3DDevice9** device )
  14. {
  15.     //
  16.     //创造应用程序窗口
  17.     //
  18.     //初始化windows窗口类
  19.     WNDCLASS wc;
  20.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  21.     wc.lpfnWndProc   = (WNDPROC)d3d::WndProc; 
  22.     wc.cbClsExtra    = 0;
  23.     wc.cbWndExtra    = 0;
  24.     wc.hInstance     = hInstance;
  25.     wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
  26.     wc.hCursor       = LoadCursor(0, IDC_ARROW);
  27.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  28.     wc.lpszMenuName  = 0;
  29.     wc.lpszClassName = "Direct3D9App";
  30.     //注册窗口类
  31.     if (!RegisterClass(&wc))
  32.     {
  33.         ::MessageBox(0,"RegisterClass() - FAILED",0,0);
  34.         return false;
  35.     }
  36.     
  37.     //创建窗口并返回其句柄
  38.     HWND hwnd = 0;
  39.     hwnd = ::CreateWindow("Direct3D9App""Direct3D9App"
  40.         WS_EX_TOPMOST,
  41.         0, 0, width, height,
  42.         0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); 
  43.     if( !hwnd )
  44.     {
  45.         ::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
  46.         return false;
  47.     }
  48.     //显示/更新 窗口
  49.     ::ShowWindow(hwnd,SW_SHOW);
  50.     ::UpdateWindow(hwnd);
  51.     //
  52.     // Init D3D
  53.     //
  54.     HRESULT hr = 0;
  55.     //获取IDirect3D9 的指针(第1步)
  56.     IDirect3D9* d3d9 = 0;
  57.     d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  58.     if (!d3d9)
  59.     {
  60.         ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
  61.         return false;
  62.     }
  63.     //校验顶点运算(第2步)
  64.     //获取设备性能
  65.     D3DCAPS9 caps;
  66.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, //指定默认显示卡
  67.         deviceType,     //指定设备类型
  68.         &caps /*返回已初始化的设备性能结构实例*/);
  69.     int vp = 0;
  70.     if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  71.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;   //支持硬件顶点运算
  72.     else
  73.         vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;   //不支持硬件顶点运算
  74.     //填充D3DPRESENT_PARAMETERS 结构(第3步)
  75.     D3DPRESENT_PARAMETERS d3dpp;
  76.     d3dpp.BackBufferWidth            = width;
  77.     d3dpp.BackBufferHeight           = height;
  78.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  79.     d3dpp.BackBufferCount            = 1;
  80.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  81.     d3dpp.MultiSampleQuality         = 0;
  82.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; //指定交换链中的缓存的页面置换方式
  83.     d3dpp.hDeviceWindow              = hwnd;
  84.     d3dpp.Windowed                   = windowed;
  85.     d3dpp.EnableAutoDepthStencil     = true
  86.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  87.     d3dpp.Flags                      = 0;
  88.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //默认刷新频率
  89.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;   //立即提交(后备缓冲)
  90.     //创建设备(第4步)
  91.     hr = d3d9->CreateDevice(
  92.         D3DADAPTER_DEFAULT, //默认显示卡
  93.         deviceType, //设备类型(HAL/REF)
  94.         hwnd,   //窗口句柄
  95.         vp,     //指定顶点运算方式
  96.         &d3dpp, //present parameters
  97.         device  //返回创建的设备接口的地址
  98.         );
  99.     if(FAILED(hr))
  100.     {
  101.         //如果创建失败,重设深度缓存的像素格式,再创建一次。
  102.         d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  103.         hr = d3d9->CreateDevice(
  104.             D3DADAPTER_DEFAULT, //默认显示卡
  105.             deviceType, //设备类型(HAL/REF)
  106.             hwnd,   //窗口句柄
  107.             vp,     //指定顶点运算方式
  108.             &d3dpp, //present parameters
  109.             device  //返回创建的设备接口的地址
  110.             );
  111.         if (FAILED(hr))
  112.         {
  113.             d3d9->Release();
  114.             ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
  115.             return false;
  116.         }
  117.     }
  118.     d3d9->Release();
  119.     return true;
  120. }
  121. int d3d::EnterMsgLoop(bool (*ptr_display)(float timeDelta))
  122. {
  123.     //
  124.     //消息循环
  125.     //
  126.     MSG msg;
  127.     ::ZeroMemory(&msg,sizeof(MSG));
  128.     static float lastTime = (float)timeGetTime(); 
  129.     while (msg.message!=WM_QUIT)
  130.     {
  131.         if (::PeekMessage(&msg,0,0,0,PM_REMOVE))
  132.         {
  133.             ::TranslateMessage(&msg);
  134.             ::DispatchMessage(&msg);
  135.         }
  136.         else
  137.         {
  138.             float currTime = (float)timeGetTime();
  139.             float timeDelta = (currTime - lastTime)*0.001f;
  140.             ptr_display(timeDelta);
  141.             lastTime = currTime;
  142.         }
  143.     }
  144.     return (int)msg.wParam;
  145. }
  146. D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color)
  147. {
  148.     //
  149.     // 初始化方向光
  150.     //
  151.     D3DLIGHT9 light;
  152.     ::ZeroMemory(&light, sizeof(light));
  153.     light.Type      = D3DLIGHT_DIRECTIONAL;
  154.     light.Ambient   = *color * 0.6f;
  155.     light.Diffuse   = *color;
  156.     light.Specular  = *color * 0.3f;
  157.     light.Direction = *direction;
  158.     return light;
  159. }
  160. D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
  161. {
  162.     //
  163.     // 初始化点光源
  164.     //
  165.     D3DLIGHT9 light;
  166.     ::ZeroMemory(&light, sizeof(light));
  167.     light.Type      = D3DLIGHT_POINT;
  168.     light.Ambient   = *color * 0.6f;
  169.     light.Diffuse   = *color;
  170.     light.Specular  = *color * 0.6f;
  171.     light.Position  = *position;
  172.     light.Range        = 1000.0f;   //最大光程
  173.     light.Attenuation0 = 1.0f;  //衰减系数
  174.     light.Attenuation1 = 0.0f;
  175.     light.Attenuation2 = 0.0f;
  176.     return light;
  177. }
  178. D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
  179. {
  180.     //
  181.     // 初始化聚光灯
  182.     //
  183.     D3DLIGHT9 light;
  184.     ::ZeroMemory(&light, sizeof(light));
  185.     light.Type      = D3DLIGHT_SPOT;
  186.     light.Ambient   = *color * 0.0f;
  187.     light.Diffuse   = *color;
  188.     light.Specular  = *color * 0.6f;
  189.     light.Position  = *position;
  190.     light.Direction = *direction;
  191.     light.Range        = 1000.0f;
  192.     light.Falloff      = 1.0f;  //内锥形到外锥形的衰减方式
  193.     light.Attenuation0 = 1.0f;
  194.     light.Attenuation1 = 0.0f;
  195.     light.Attenuation2 = 0.0f;
  196.     light.Theta        = 0.4f;  //内锥形的圆锥角
  197.     light.Phi          = 0.9f;  //外锥形的圆锥角
  198.     return light;
  199. }
  200. D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
  201. {
  202.     //
  203.     // 初始化材质
  204.     //
  205.     D3DMATERIAL9 mtrl;
  206.     mtrl.Ambient  = a;
  207.     mtrl.Diffuse  = d;
  208.     mtrl.Specular = s;
  209.     mtrl.Emissive = e;
  210.     mtrl.Power    = p;
  211.     return mtrl;
  212. }