天天看点

DirectX 3D 基本框架(二)

对上次所用的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.     D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color);
  61.     D3DLIGHT9 InitPointLight(D3DXVECTOR3* position,D3DXCOLOR* color);
  62.     D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position,D3DXVECTOR3* direction,D3DXCOLOR* color);
  63.     //
  64.     // 材质 函数/常量
  65.     //
  66.     D3DMATERIAL9 InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p);
  67.     const D3DMATERIAL9 WHITE_MTRL  = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
  68.     const D3DMATERIAL9 RED_MTRL    = InitMtrl(RED, RED, RED, BLACK, 2.0f);
  69.     const D3DMATERIAL9 GREEN_MTRL  = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
  70.     const D3DMATERIAL9 BLUE_MTRL   = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
  71.     const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
  72. }
  73. #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.     HWND hwnd = 0;
  38.     hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App", 
  39.         WS_EX_TOPMOST,
  40.         0, 0, width, height,
  41.         0 , 0 , hInstance, 0 ); 
  42.     if( !hwnd )
  43.     {
  44.         ::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
  45.         return false;
  46.     }
  47.     //显示/更新 窗口
  48.     ::ShowWindow(hwnd,SW_SHOW);
  49.     ::UpdateWindow(hwnd);
  50.     //
  51.     // Init D3D
  52.     //
  53.     HRESULT hr = 0;
  54.     //获取IDirect3D9 的指针(第1步)
  55.     IDirect3D9* d3d9 = 0;
  56.     d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  57.     if (!d3d9)
  58.     {
  59.         ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
  60.         return false;
  61.     }
  62.     //校验顶点运算(第2步)
  63.     //获取设备性能
  64.     D3DCAPS9 caps;
  65.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, //指定默认显示卡
  66.         deviceType,     //指定设备类型
  67.         &caps );
  68.     int vp = 0;
  69.     if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  70.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;   //支持硬件顶点运算
  71.     else
  72.         vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;   //不支持硬件顶点运算
  73.     //填充D3DPRESENT_PARAMETERS 结构(第3步)
  74.     D3DPRESENT_PARAMETERS d3dpp;
  75.     d3dpp.BackBufferWidth            = width;
  76.     d3dpp.BackBufferHeight           = height;
  77.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  78.     d3dpp.BackBufferCount            = 1;
  79.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  80.     d3dpp.MultiSampleQuality         = 0;
  81.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; //指定交换链中的缓存的页面置换方式
  82.     d3dpp.hDeviceWindow              = hwnd;
  83.     d3dpp.Windowed                   = windowed;
  84.     d3dpp.EnableAutoDepthStencil     = true; 
  85.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  86.     d3dpp.Flags                      = 0;
  87.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //默认刷新频率
  88.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;   //立即提交(后备缓冲)
  89.     //创建设备(第4步)
  90.     hr = d3d9->CreateDevice(
  91.         D3DADAPTER_DEFAULT, //默认显示卡
  92.         deviceType, //设备类型(HAL/REF)
  93.         hwnd,   //窗口句柄
  94.         vp,     //指定顶点运算方式
  95.         &d3dpp, //present parameters
  96.         device  //返回创建的设备接口的地址
  97.         );
  98.     if(FAILED(hr))
  99.     {
  100.         //如果创建失败,重设深度缓存的像素格式,再创建一次。
  101.         d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  102.         hr = d3d9->CreateDevice(
  103.             D3DADAPTER_DEFAULT, //默认显示卡
  104.             deviceType, //设备类型(HAL/REF)
  105.             hwnd,   //窗口句柄
  106.             vp,     //指定顶点运算方式
  107.             &d3dpp, //present parameters
  108.             device  //返回创建的设备接口的地址
  109.             );
  110.         if (FAILED(hr))
  111.         {
  112.             d3d9->Release();
  113.             ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
  114.             return false;
  115.         }
  116.     }
  117.     d3d9->Release();
  118.     return true;
  119. }
  120. int d3d::EnterMsgLoop(bool (*ptr_display)(float timeDelta))
  121. {
  122.     //
  123.     //消息循环
  124.     //
  125.     MSG msg;
  126.     ::ZeroMemory(&msg,sizeof(MSG));
  127.     static float lastTime = (float)timeGetTime(); 
  128.     while (msg.message!=WM_QUIT)
  129.     {
  130.         if (::PeekMessage(&msg,0,0,0,PM_REMOVE))
  131.         {
  132.             ::TranslateMessage(&msg);
  133.             ::DispatchMessage(&msg);
  134.         }
  135.         else
  136.         {
  137.             float currTime = (float)timeGetTime();
  138.             float timeDelta = (currTime - lastTime)*0.001f;
  139.             ptr_display(timeDelta);
  140.             lastTime = currTime;
  141.         }
  142.     }
  143.     return (int)msg.wParam;
  144. }
  145. D3DLIGHT9 d3d::InitDirectionalLight(D3DXVECTOR3* direction,D3DXCOLOR* color)
  146. {
  147.     //
  148.     // 初始化方向光
  149.     //
  150.     D3DLIGHT9 light;
  151.     ::ZeroMemory(&light, sizeof(light));
  152.     light.Type      = D3DLIGHT_DIRECTIONAL;
  153.     light.Ambient   = *color * 0.6f;
  154.     light.Diffuse   = *color;
  155.     light.Specular  = *color * 0.3f;
  156.     light.Direction = *direction;
  157.     return light;
  158. }
  159. D3DLIGHT9 d3d::InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
  160. {
  161.     //
  162.     // 初始化点光源
  163.     //
  164.     D3DLIGHT9 light;
  165.     ::ZeroMemory(&light, sizeof(light));
  166.     light.Type      = D3DLIGHT_POINT;
  167.     light.Ambient   = *color * 0.6f;
  168.     light.Diffuse   = *color;
  169.     light.Specular  = *color * 0.6f;
  170.     light.Position  = *position;
  171.     light.Range        = 1000.0f;   //最大光程
  172.     light.Attenuation0 = 1.0f;  //衰减系数
  173.     light.Attenuation1 = 0.0f;
  174.     light.Attenuation2 = 0.0f;
  175.     return light;
  176. }
  177. D3DLIGHT9 d3d::InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
  178. {
  179.     //
  180.     // 初始化聚光灯
  181.     //
  182.     D3DLIGHT9 light;
  183.     ::ZeroMemory(&light, sizeof(light));
  184.     light.Type      = D3DLIGHT_SPOT;
  185.     light.Ambient   = *color * 0.0f;
  186.     light.Diffuse   = *color;
  187.     light.Specular  = *color * 0.6f;
  188.     light.Position  = *position;
  189.     light.Direction = *direction;
  190.     light.Range        = 1000.0f;
  191.     light.Falloff      = 1.0f;  //内锥形到外锥形的衰减方式
  192.     light.Attenuation0 = 1.0f;
  193.     light.Attenuation1 = 0.0f;
  194.     light.Attenuation2 = 0.0f;
  195.     light.Theta        = 0.4f;  //内锥形的圆锥角
  196.     light.Phi          = 0.9f;  //外锥形的圆锥角
  197.     return light;
  198. }
  199. D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
  200. {
  201.     //
  202.     // 初始化材质
  203.     //
  204.     D3DMATERIAL9 mtrl;
  205.     mtrl.Ambient  = a;
  206.     mtrl.Diffuse  = d;
  207.     mtrl.Specular = s;
  208.     mtrl.Emissive = e;
  209.     mtrl.Power    = p;
  210.     return mtrl;
  211. }

继续阅读