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

DirextX编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "d3dUtility.h"
  3. bool Initd3dDevice( IDirect3D9* pD3D, IDirect3DDevice9** ppd3dDevice, HWND hWnd)
  4. {
  5. D3DPRESENT_PARAMETERS d3dpp;
  6. ZeroMemory( &d3dpp, sizeof(d3dpp));
  7. d3dpp.Windowed = TRUE;
  8. // d3dpp.BackBufferWidth = 0;
  9. // d3dpp.BackBufferHeight = 0;
  10. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  11. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  12. d3dpp.EnableAutoDepthStencil = TRUE;
  13. d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  14. if( FAILED( pD3D -> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
  15. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  16. &d3dpp, ppd3dDevice)))
  17. {
  18. ::MessageBoxA( 0, "Create d3d Device Failed!", "Init d3d Device", 0);
  19. return false;
  20. }
  21. return true;
  22. }
  23. D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
  24. {
  25. D3DLIGHT9 light;
  26. ::ZeroMemory(&light, sizeof(light));
  27. light.Type      = D3DLIGHT_DIRECTIONAL;
  28. light.Ambient   = *color * 0.4f;
  29. light.Diffuse   = *color;
  30. light.Specular  = *color * 0.6f;
  31. light.Direction = *direction;
  32. return light;
  33. }
  34. D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
  35. {
  36. D3DLIGHT9 light;
  37. ::ZeroMemory(&light, sizeof(light));
  38. light.Type      = D3DLIGHT_POINT;
  39. light.Ambient   = *color * 0.4f;
  40. light.Diffuse   = *color;
  41. light.Specular  = *color * 0.6f;
  42. light.Position  = *position;
  43. light.Range        = 1000.0f;
  44. light.Falloff      = 1.0f;
  45. light.Attenuation0 = 1.0f;
  46. light.Attenuation1 = 0.0f;
  47. light.Attenuation2 = 0.0f;
  48. return light;
  49. }
  50. D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
  51. {
  52. D3DLIGHT9 light;
  53. ::ZeroMemory(&light, sizeof(light));
  54. light.Type      = D3DLIGHT_SPOT;
  55. light.Ambient   = *color * 0.4f;
  56. light.Diffuse   = *color;
  57. light.Specular  = *color * 0.6f;
  58. light.Position  = *position;
  59. light.Direction = *direction;
  60. light.Range        = 1000.0f;
  61. light.Falloff      = 1.0f;
  62. light.Attenuation0 = 1.0f;
  63. light.Attenuation1 = 0.0f;
  64. light.Attenuation2 = 0.0f;
  65. light.Theta        = 0.5f;
  66. light.Phi          = 0.7f;
  67. return light;
  68. }
  69. D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
  70. {
  71. D3DMATERIAL9 mtrl;
  72. mtrl.Ambient  = a;
  73. mtrl.Diffuse  = d;
  74. mtrl.Specular = s;
  75. mtrl.Emissive = e;
  76. mtrl.Power    = p;
  77. return mtrl;
  78. }
  79. BoundingBox::BoundingBox()
  80. {
  81. _minPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  82. _maxPoint = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  83. }
  84. BoundingBox::BoundingBox(D3DXVECTOR3 minPoint, D3DXVECTOR3 maxPoint)
  85. {
  86. _minPoint = minPoint;
  87. _maxPoint = maxPoint;
  88. }
  89. bool BoundingBox::isPointInside(D3DXVECTOR3 &p)
  90. {
  91. if( p.x >= _minPoint.x && p.y >= _minPoint.y && p.z >= _minPoint.z &&
  92. p.x <= _maxPoint.x && p.y <= _maxPoint.y && p.z <= _maxPoint.z)
  93. return true;
  94. else 
  95. return false;
  96. }
  97. BoundingSphere::BoundingSphere(D3DXVECTOR3 center, float radius)
  98. {
  99. _center = center;
  100. _radius = radius;
  101. }
  102. float GetRandomFloat(float lowBound, float highBound)
  103. {
  104. if(lowBound > highBound)
  105. return lowBound;
  106. //generate a randon float number between 0 and 1
  107. float fNum = ( rand() % 10000 ) * 0.0001f;
  108. //get a number between lowBound and highBound
  109. return ( highBound - lowBound ) * fNum + lowBound ;
  110. }
  111. void GetRandomVector(D3DXVECTOR3* out, D3DXVECTOR3* min, D3DXVECTOR3* max)
  112. {
  113. out->x = GetRandomFloat( min->x, max->x);
  114. out->y = GetRandomFloat( min->y, max->y);
  115. out->z = GetRandomFloat( min->z, max->z);
  116. }
  117. DWORD FtoDw(float f)
  118. {
  119. return *((DWORD*)&f);
  120. }