zfxd3dUtility.cpp
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:5k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. #include "zfxd3dUtility.h"
  2. bool InitD3DApp(HINSTANCE hInstance,  // 程序实例句柄
  3. bool windowed,             // 布尔变量,是否为窗口模式
  4. D3DDEVTYPE deviceType,     // HAL or REF
  5. IDirect3D9** pd3d9,      // D3D对象接口
  6. IDirect3DDevice9** pd3dDevice,// D3D设备接口
  7. PCTSTR szTitle,           // 窗口标题名
  8. int width, int height)
  9. {
  10. // 注册窗口类
  11. WNDCLASS wc;
  12. wc.style         = CS_HREDRAW | CS_VREDRAW;
  13. wc.lpfnWndProc   = (WNDPROC)WndProc; 
  14. wc.cbClsExtra    = 0;
  15. wc.cbWndExtra    = 0;
  16. wc.hInstance     = hInstance;
  17. wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
  18. wc.hCursor       = LoadCursor(0, IDC_ARROW);
  19. wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  20. wc.lpszMenuName  = 0;
  21. wc.lpszClassName = L"ZFXD3DApp";
  22. if( !RegisterClass(&wc) ) 
  23. {
  24. ::MessageBox(0, L"FAILED to register WND class", 0, 0);
  25. return false;
  26. }
  27. // 创建窗口
  28. HWND hWnd = 0;
  29. hWnd = ::CreateWindow(L"ZFXD3DApp", szTitle, 
  30. WS_OVERLAPPEDWINDOW ,
  31. 50, 50, width, height,
  32. 0 /*父窗口*/, 0 /* 菜单名 */, hInstance, 0); 
  33. if( !hWnd )
  34. {
  35. ::MessageBox(0, L"FAILED to create a window", 0, 0);
  36. return false;
  37. }
  38. ::ShowWindow(hWnd, SW_SHOW);
  39. ::UpdateWindow(hWnd);
  40. // 准备创建设备
  41. if(!((*pd3d9) = Direct3DCreate9(D3D_SDK_VERSION)))
  42. {
  43. ::MessageBox(0, L"Create D3D Object FAILED", 0, 0);
  44. return false;
  45. }
  46. // 检查硬件是否支持顶点处理
  47. D3DCAPS9 caps;
  48. (*pd3d9)->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);
  49. DWORD vp = 0;
  50. if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
  51. vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  52. else
  53. vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  54. // 填充D3DPRESENT_PARAMETERS结构体.
  55.  
  56. D3DPRESENT_PARAMETERS d3dpp;
  57. d3dpp.BackBufferWidth            = width;
  58. d3dpp.BackBufferHeight           = height;
  59. d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  60. d3dpp.BackBufferCount            = 1;
  61. d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  62. d3dpp.MultiSampleQuality         = 0;
  63. d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  64. d3dpp.hDeviceWindow              = hWnd;
  65. d3dpp.Windowed                   = windowed;
  66. d3dpp.EnableAutoDepthStencil     = true; 
  67. d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  68. d3dpp.Flags                      = 0;
  69. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  70. d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  71. // 创建设备
  72. HRESULT hr;
  73. hr = (*pd3d9)->CreateDevice(
  74. D3DADAPTER_DEFAULT,
  75. deviceType,
  76. hWnd,
  77. vp,
  78.     &d3dpp,
  79.     pd3dDevice);
  80. if( FAILED(hr) )
  81. {
  82. (*pd3d9)->Release(); // 释放D3D对象资源
  83. ::MessageBox(0, L"Create Device FAILED", 0, 0);
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool InitDXFont(IDirect3DDevice9* pd3dDevice, ID3DXFont** pFont, PCTSTR szFontType, 
  89. bool isItalic, int height, int width, int weight)
  90. {
  91. //创建字体
  92. D3DXFONT_DESC fontDesc;
  93. ZeroMemory( &fontDesc, sizeof(D3DXFONT_DESC) );
  94. fontDesc.Height = height;
  95. fontDesc.Width = width;
  96. fontDesc.Weight = weight;
  97. fontDesc.Italic = isItalic;
  98. wcscpy( fontDesc.FaceName, szFontType);
  99. if( SUCCEEDED( D3DXCreateFontIndirect( pd3dDevice, &fontDesc, pFont )))
  100. return true;
  101. else
  102. return false;
  103. }
  104. D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
  105. {
  106. D3DLIGHT9 light;
  107. ::ZeroMemory(&light, sizeof(light));
  108. light.Type      = D3DLIGHT_DIRECTIONAL;
  109. light.Ambient   = *color * 0.6f;
  110. light.Diffuse   = *color;
  111. light.Specular  = *color * 0.6f;
  112. light.Direction = *direction;
  113. return light;
  114. }
  115. D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
  116. {
  117. D3DLIGHT9 light;
  118. ::ZeroMemory(&light, sizeof(light));
  119. light.Type      = D3DLIGHT_POINT;
  120. light.Ambient   = *color * 0.6f;
  121. light.Diffuse   = *color;
  122. light.Specular  = *color * 0.6f;
  123. light.Position  = *position;
  124. light.Range        = 1000.0f;
  125. light.Falloff      = 1.0f;
  126. light.Attenuation0 = 1.0f;
  127. light.Attenuation1 = 0.0f;
  128. light.Attenuation2 = 0.0f;
  129. return light;
  130. }
  131. D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
  132. {
  133. D3DLIGHT9 light;
  134. ::ZeroMemory(&light, sizeof(light));
  135. light.Type      = D3DLIGHT_SPOT;
  136. light.Ambient   = *color * 0.0f;
  137. light.Diffuse   = *color;
  138. light.Specular  = *color * 0.6f;
  139. light.Position  = *position;
  140. light.Direction = *direction;
  141. light.Range        = 1000.0f;
  142. light.Falloff      = 1.0f;
  143. light.Attenuation0 = 1.0f;
  144. light.Attenuation1 = 0.0f;
  145. light.Attenuation2 = 0.0f;
  146. light.Theta        = 0.4f;
  147. light.Phi          = 0.9f;
  148. return light;
  149. }
  150. D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
  151. {
  152. D3DMATERIAL9 mtrl;
  153. mtrl.Ambient  = a;
  154. mtrl.Diffuse  = d;
  155. mtrl.Specular = s;
  156. mtrl.Emissive = e;
  157. mtrl.Power    = p;
  158. return mtrl;
  159. }