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

DirextX编程

开发平台:

Visual C++

  1. #include "dxstdafx.h"
  2. #include <vector>
  3. #include "object.h"
  4. CSpotLight::CSpotLight()
  5. {
  6. m_pMeshMtrl = NULL;
  7. m_pMesh = NULL;
  8. m_dwMtrls = 0;
  9. m_pTexture = NULL;
  10. m_bDeviceLost = true;
  11. }
  12. CSpotLight::~CSpotLight()
  13. {
  14. delete [] m_pMeshMtrl;
  15. SAFE_RELEASE( m_pTexture );
  16. SAFE_RELEASE( m_pMesh );
  17. }
  18. void CSpotLight::OnDestroyDevice()
  19. {
  20. SAFE_RELEASE( m_pTexture );
  21. SAFE_RELEASE( m_pMesh );
  22. }
  23. LRESULT CSpotLight::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  24. {
  25. D3DXVECTOR3 lightEyePt = *( m_lightCamera.GetEyePt() );
  26. D3DXVECTOR3 lookAtPt   = *( m_lightCamera.GetLookAtPt() );
  27. m_light.Position.x = lightEyePt.x;  
  28. m_light.Position.y = lightEyePt.y;  
  29. m_light.Position.z = lightEyePt.z;
  30. m_light.Direction.x = lookAtPt.x - lightEyePt.x;
  31. m_light.Direction.y = lookAtPt.y - lightEyePt.y;
  32. m_light.Direction.z = -(lookAtPt.z - lightEyePt.z);
  33. return m_lightCamera.HandleMessages( hWnd, uMsg, wParam, lParam );
  34. }
  35. HRESULT CSpotLight::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice )
  36. {
  37. HRESULT hr;
  38. m_pd3dDevice = pd3dDevice;
  39. // 载入X文件数据
  40. ID3DXBuffer* mtrlBuffer;
  41. hr = D3DXLoadMeshFromX( L"spotlight.x", D3DXMESH_MANAGED, m_pd3dDevice, NULL, &mtrlBuffer, NULL, &m_dwMtrls, &m_pMesh );
  42. if( FAILED(hr) )
  43. {
  44. MessageBox( NULL, L"spotlight.x文件不在当前目录下", L"Init spotlight failed", MB_OK );
  45. return hr;
  46. }
  47. // 提取模型材质
  48. D3DXMATERIAL* pMtrlInBuf = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
  49. m_pMeshMtrl = new D3DMATERIAL9[m_dwMtrls];
  50. for(DWORD i=0;i<m_dwMtrls;i++)
  51. {
  52. m_pMeshMtrl[i] = pMtrlInBuf[i].MatD3D;
  53. //在此修改mesh's material的部分属性
  54. m_pMeshMtrl[i].Emissive.r = m_pMeshMtrl[i].Emissive.g = m_pMeshMtrl[i].Emissive.b = 0.0f;
  55. m_pMeshMtrl[i].Ambient.r  = m_pMeshMtrl[i].Ambient.g  = m_pMeshMtrl[i].Ambient.b  = 1.0f;
  56. }
  57. mtrlBuffer->Release();
  58. // 创建纹理
  59. hr = D3DXCreateTextureFromFile( m_pd3dDevice, L"spotlight.bmp", &m_pTexture );
  60. if( FAILED(hr) )
  61. {
  62. MessageBox( NULL, L"spotlight.bmp不在当前目录下", L"Init spotlight failed", MB_OK );
  63. return hr;
  64. }
  65. return hr;
  66. }
  67. void CSpotLight::InitLight( D3DVECTOR position, D3DVECTOR direction, D3DVECTOR upVec )
  68. {
  69. // 初始化光源
  70. ZeroMemory( &m_light, sizeof(D3DLIGHT9) );
  71. m_light.Type      = D3DLIGHT_SPOT;
  72. m_light.Position  = position;
  73. m_light.Direction = direction;
  74. m_light.Diffuse   = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f );
  75. m_light.Phi       = D3DX_PI/3; //45度角
  76. m_light.Theta     = D3DX_PI/6;//18度角
  77. m_light.Falloff   = 1.0f;      //亮度从中心向边缘均匀扩散
  78. m_light.Attenuation0 = 0.5f;
  79. m_light.Attenuation1 = 0.5f;
  80. m_light.Range = 20.0f;
  81. // 初始化相机
  82. D3DXVECTOR3 pos( position );
  83. D3DXVECTOR3 lookAt( position.x + direction.x, position.y + direction.y, position.z + direction.z );
  84. D3DXVECTOR3 upVector( upVec );
  85. m_lightCamera.SetViewParams( &pos, &lookAt, &upVector );
  86. m_lightCamera.SetProjParams( D3DX_PI/4, 1.0f, 0.1f, 100.0f );
  87. m_lightCamera.SetScalers( 0.01f, 8.0f );
  88. m_lightCamera.SetRotateButtons( true, false, false );
  89. }
  90. void CSpotLight::FrameMove( float fElapsedTime )
  91. {
  92. m_lightCamera.FrameMove( fElapsedTime );
  93. }
  94. void CSpotLight::PreRender()
  95. {
  96. // lightViewMat指向lightCamera的当前视口矩阵
  97. D3DXMATRIX lightViewMat = *( m_lightCamera.GetViewMatrix() );
  98. D3DXMATRIX  MatViewInv;
  99. D3DXMatrixInverse( &MatViewInv, NULL, &lightViewMat );
  100. m_pd3dDevice->SetTransform( D3DTS_WORLD, &MatViewInv );
  101. m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
  102. }
  103. void CSpotLight::OnRender()
  104. {
  105. PreRender();
  106. for(DWORD i=0;i<m_dwMtrls;i++)
  107. {
  108. m_pd3dDevice->SetMaterial( &m_pMeshMtrl[i] );
  109. m_pd3dDevice->SetTexture( 0, m_pTexture );
  110. m_pMesh->DrawSubset(i);
  111. }
  112. PostRender();
  113. }
  114. void CSpotLight::PostRender()
  115. {
  116. }