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

DirextX编程

开发平台:

Visual C++

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