object.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:4k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "dxstdafx.h"
- #include <vector>
- #include "object.h"
- CSpotLight::CSpotLight()
- {
- m_pMeshMtrl = NULL;
- m_pMesh = NULL;
- m_dwMtrls = 0;
- m_pTexture = NULL;
- m_bDeviceLost = true;
- }
- CSpotLight::~CSpotLight()
- {
- delete [] m_pMeshMtrl;
- SAFE_RELEASE( m_pTexture );
- SAFE_RELEASE( m_pMesh );
- }
- void CSpotLight::OnDestroyDevice()
- {
- SAFE_RELEASE( m_pTexture );
- SAFE_RELEASE( m_pMesh );
- }
- LRESULT CSpotLight::HandleMessages( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- D3DXVECTOR3 lightEyePt = *( m_lightCamera.GetEyePt() );
- D3DXVECTOR3 lookAtPt = *( m_lightCamera.GetLookAtPt() );
- m_light.Position.x = lightEyePt.x;
- m_light.Position.y = lightEyePt.y;
- m_light.Position.z = lightEyePt.z;
- m_light.Direction.x = lookAtPt.x - lightEyePt.x;
- m_light.Direction.y = lookAtPt.y - lightEyePt.y;
- m_light.Direction.z = -(lookAtPt.z - lightEyePt.z);
- return m_lightCamera.HandleMessages( hWnd, uMsg, wParam, lParam );
- }
- HRESULT CSpotLight::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice )
- {
- HRESULT hr;
- m_pd3dDevice = pd3dDevice;
- // 载入X文件数据
- ID3DXBuffer* mtrlBuffer;
- hr = D3DXLoadMeshFromX( L"spotlight.x", D3DXMESH_MANAGED, m_pd3dDevice, NULL, &mtrlBuffer, NULL, &m_dwMtrls, &m_pMesh );
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"spotlight.x文件不在当前目录下", L"Init spotlight failed", MB_OK );
- return hr;
- }
- // 提取模型材质
- D3DXMATERIAL* pMtrlInBuf = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
- m_pMeshMtrl = new D3DMATERIAL9[m_dwMtrls];
- for(DWORD i=0;i<m_dwMtrls;i++)
- {
- m_pMeshMtrl[i] = pMtrlInBuf[i].MatD3D;
- //在此修改mesh's material的部分属性
- m_pMeshMtrl[i].Emissive.r = m_pMeshMtrl[i].Emissive.g = m_pMeshMtrl[i].Emissive.b = 0.0f;
- m_pMeshMtrl[i].Ambient.r = m_pMeshMtrl[i].Ambient.g = m_pMeshMtrl[i].Ambient.b = 1.0f;
- }
- mtrlBuffer->Release();
- // 创建纹理
- hr = D3DXCreateTextureFromFile( m_pd3dDevice, L"spotlight.bmp", &m_pTexture );
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"spotlight.bmp不在当前目录下", L"Init spotlight failed", MB_OK );
- return hr;
- }
- return hr;
- }
- void CSpotLight::InitLight( D3DVECTOR position, D3DVECTOR direction, D3DVECTOR upVec )
- {
- // 初始化光源
- ZeroMemory( &m_light, sizeof(D3DLIGHT9) );
- m_light.Type = D3DLIGHT_SPOT;
- m_light.Position = position;
- m_light.Direction = direction;
- m_light.Diffuse = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f );
- m_light.Phi = D3DX_PI/3; //45度角
- m_light.Theta = D3DX_PI/6;//18度角
- m_light.Falloff = 1.0f; //亮度从中心向边缘均匀扩散
- m_light.Attenuation0 = 0.5f;
- m_light.Attenuation1 = 0.5f;
- m_light.Range = 20.0f;
- // 初始化相机
- D3DXVECTOR3 pos( position );
- D3DXVECTOR3 lookAt( position.x + direction.x, position.y + direction.y, position.z + direction.z );
- D3DXVECTOR3 upVector( upVec );
- m_lightCamera.SetViewParams( &pos, &lookAt, &upVector );
- m_lightCamera.SetProjParams( D3DX_PI/4, 1.0f, 0.1f, 100.0f );
- m_lightCamera.SetScalers( 0.01f, 8.0f );
- m_lightCamera.SetRotateButtons( true, false, false );
- }
- void CSpotLight::FrameMove( float fElapsedTime )
- {
- m_lightCamera.FrameMove( fElapsedTime );
- }
- void CSpotLight::PreRender()
- {
- // lightViewMat指向lightCamera的当前视口矩阵
- D3DXMATRIX lightViewMat = *( m_lightCamera.GetViewMatrix() );
- D3DXMATRIX MatViewInv;
- D3DXMatrixInverse( &MatViewInv, NULL, &lightViewMat );
- m_pd3dDevice->SetTransform( D3DTS_WORLD, &MatViewInv );
- m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
- }
- void CSpotLight::OnRender()
- {
- PreRender();
- for(DWORD i=0;i<m_dwMtrls;i++)
- {
- m_pd3dDevice->SetMaterial( &m_pMeshMtrl[i] );
- m_pd3dDevice->SetTexture( 0, m_pTexture );
- m_pMesh->DrawSubset(i);
- }
- PostRender();
- }
- void CSpotLight::PostRender()
- {
- }