zfxBillBoard.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:5k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".zfxbillboard.h"
- #include "zfxCamera.h"
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CZFXBillBoard Impletation
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- CZFXBillBoard::CZFXBillBoard(void)
- {
- m_posVec = D3DXVECTOR3(0, 0, 0);
- m_width = 0;
- m_height = 0;
- m_pVB = NULL;
- }
- CZFXBillBoard::~CZFXBillBoard(void)
- {
- SAFE_RELEASE(m_pVB);
- }
- // 创建公告板对象,主要是顶点缓冲
- HRESULT CZFXBillBoard::CreateBoard(LPDIRECT3DDEVICE9 pd3dDevice, D3DXVECTOR3& posVec, float width, float height)
- {
- m_posVec.x = posVec.x;
- m_posVec.y = posVec.y;
- m_posVec.z = posVec.z;
- m_width = width;
- m_height = height;
- ZFXBBDVERTEX bbdVertex[4] = {
- {-width/2, 0, 0, 0xffffffff, 0, 1, },/*左下角*/
- { width/2, 0, 0, 0xffffffff, 1, 1, },/*右下角*/
- {-width/2, height, 0, 0xffffffff, 0, 0, },/*左上角*/
- { width/2, height, 0, 0xffffffff, 1, 0, },/*右上角*/
- };
- if( FAILED( pd3dDevice->CreateVertexBuffer( 4*sizeof(ZFXBBDVERTEX),
- 0 /*Usage*/, D3DFVF_ZFXBBDVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
- return E_FAIL;
- VOID* pVertices;
- if( FAILED( m_pVB->Lock( 0, sizeof(bbdVertex), (void**)&pVertices, 0 ) ) )
- return E_FAIL;
- memcpy( pVertices, bbdVertex, sizeof(bbdVertex) );
- m_pVB->Unlock();
- return D3D_OK;
- }
- // 更新公告板的朝向,视点移动之后,一定要调用该函数
- void CZFXBillBoard::UpdateOrient(CZFXCamera* camera)
- {
- float deltaX = m_posVec.x - camera->GetPosX();
- float deltaZ = m_posVec.z - camera->GetPosZ();
- float mod = sqrt(deltaX * deltaX + deltaZ * deltaZ);
- D3DXMatrixIdentity(&m_matWorld);
- D3DXMatrixTranslation(&m_matWorld, m_posVec.x, m_posVec.y, m_posVec.z);
- D3DXMATRIX matRot;
- D3DXMatrixIdentity(&matRot);
- // rotation angle == theta////////////////
- // cos(theta) 0 -sin(theta) 0
- // 0 1 0 0
- // sin(theta) 0 cos(theta) 0
- // 0 0 0 1
- //////////////////////////////////////////
- matRot(0,0) = deltaZ/mod; matRot(0,2) = -deltaX/mod;
- matRot(2,0) = -matRot(0,2); matRot(2,2) = matRot(0,0);
- D3DXMatrixMultiply(&m_matWorld, &matRot, &m_matWorld);
- }
- // 渲染公告板
- void CZFXBillBoard::OnRender(LPDIRECT3DDEVICE9 pd3dDevice)
- {
- pd3dDevice->SetTransform(D3DTS_WORLD, &m_matWorld);
- pd3dDevice->SetStreamSource(0, m_pVB, 0, sizeof(ZFXBBDVERTEX));
- pd3dDevice->SetFVF(D3DFVF_ZFXBBDVERTEX);
- pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- }
- void CZFXBillBoard::preRender(LPDIRECT3DDEVICE9 pd3dDevice)
- {
- pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
- pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true);
- pd3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x08);
- pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
- pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- }
- void CZFXBillBoard::postRender(LPDIRECT3DDEVICE9 pd3dDevice)
- {
- pd3dDevice->SetRenderState(D3DRS_LIGHTING, true);
- pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false);
- pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CZFXBillBoard Impletation End
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CZFXBillBoardMgr Impletation
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- CZFXBillBoardMgr::CZFXBillBoardMgr()
- {
- m_list.clear();
- m_pTexture = NULL;
- }
- CZFXBillBoardMgr::~CZFXBillBoardMgr()
- {
- SAFE_RELEASE(m_pTexture);
- }
- int CZFXBillBoardMgr::GetBoardNum()
- {
- return (int)m_list.size();
- }
- HRESULT CZFXBillBoardMgr::LoadTextureFromFile(LPDIRECT3DDEVICE9 pd3dDevice, LPCTSTR texFileName)
- {
- if(FAILED(D3DXCreateTextureFromFileEx(pd3dDevice, texFileName, D3DX_DEFAULT, D3DX_DEFAULT,
- D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
- D3DCOLOR_RGBA(0, 0, 0, 255), NULL, NULL, &m_pTexture)))
- return E_FAIL;
- return D3D_OK;
- }
- void CZFXBillBoardMgr::AddBillBoard(CZFXBillBoard& billBoard)
- {
- m_list.push_back(billBoard);
- }
- void CZFXBillBoardMgr::UpdateBoardOrient(CZFXCamera* camera)
- {
- for(std::list<CZFXBillBoard>::iterator iter = m_list.begin();iter != m_list.end();iter++)
- {
- iter->UpdateOrient(camera);
- }
- }
- void CZFXBillBoardMgr::OnRender(LPDIRECT3DDEVICE9 pd3dDevice)
- {
- pd3dDevice->SetTexture(0, m_pTexture);
- CZFXBillBoard::preRender(pd3dDevice);
- for(std::list<CZFXBillBoard>::iterator iter = m_list.begin();iter != m_list.end();iter++)
- {
- iter->OnRender(pd3dDevice);
- }
- CZFXBillBoard::postRender(pd3dDevice);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CZFXBillBoardMgr Impletation End
- //////////////////////////////////////////////////////////////////////////////////////////////////////