plane.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:5k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "dxstdafx.h"
- #include "plane.h"
- CPlane::CPlane( WORD widthSampleNum,
- WORD heightSampleNum,
- D3DXVECTOR3 leftBottomPt,
- D3DXVECTOR3 rightUpPt )
- {
- m_iWidthSampleNum = widthSampleNum;
- m_iHeightSampleNum = heightSampleNum;
- m_leftBottomPt = leftBottomPt;
- m_rightUpPt = rightUpPt;
- m_pTexture = NULL;
- m_pVB = NULL;
- m_pIB = NULL;
- }
- CPlane::~CPlane()
- {
- OnDestroyDevice();
- }
- void CPlane::OnDestroyDevice()
- {
- SAFE_RELEASE( m_pTexture );
- SAFE_RELEASE( m_pVB );
- SAFE_RELEASE( m_pIB );
- }
- HRESULT CPlane::OnCreateDevice( IDirect3DDevice9* pd3dDevice )
- {
- HRESULT hr;
- if( m_leftBottomPt.y != m_rightUpPt.y )
- {
- MessageBox( NULL, L"所创建的平面不与XOZ平面平行", L"创建平面失败", MB_OK );
- return hr = 0;
- }
- //////////////////////////////////////创建Index Buffer//////////////////////////////////////////////
- int NumberIndices = 6 * (m_iWidthSampleNum - 1) * (m_iHeightSampleNum - 1);
- WORD* pIndices = new WORD[NumberIndices];
- int baseIndex = 0;
- for( WORD i=0; i < m_iWidthSampleNum - 1; i++ )
- {
- for( WORD j=0; j < m_iHeightSampleNum - 1; j++ )
- {
- pIndices[baseIndex] = i*m_iHeightSampleNum + j;
- pIndices[baseIndex + 1] = (i+1)*m_iHeightSampleNum + j+1;
- pIndices[baseIndex + 2] = (i+1)*m_iHeightSampleNum + j;
- pIndices[baseIndex + 3] = i*m_iHeightSampleNum + j;
- pIndices[baseIndex + 4] = i*m_iHeightSampleNum + j+1;
- pIndices[baseIndex + 5] = (i+1)*m_iHeightSampleNum + j+1;
- baseIndex += 6;
- }
- }
- hr = pd3dDevice->CreateIndexBuffer( NumberIndices * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIB, NULL );
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"Create Index buffer failed", L"Create Failed", MB_OK );
- return hr;
- }
- void* pVoidIndex;
- m_pIB->Lock( 0, NumberIndices * sizeof(WORD), (void**)&pVoidIndex, 0 );
- memcpy( pVoidIndex, pIndices, NumberIndices * sizeof(WORD) );
- m_pIB->Unlock();
- //////////////////////////////////////创建Index Buffer完毕//////////////////////////////////////////////
- ////////////////////////////////////创建Vertex Buffer///////////////////////////////////////////
- int NumberVertex = m_iWidthSampleNum * m_iHeightSampleNum;
- PLANEVERTEX* pVertices = new PLANEVERTEX[ NumberVertex ];
- for(int i=0;i<m_iWidthSampleNum;i++)
- {
- for(int j=0;j<m_iHeightSampleNum;j++)
- {
- float x, y, z;
- y = m_leftBottomPt.y;
- x = m_leftBottomPt.x + ( (float)i/(float)m_iWidthSampleNum )*( m_rightUpPt.x - m_leftBottomPt.x );
- z = m_leftBottomPt.z + ( (float)j/(float)m_iHeightSampleNum)*( m_rightUpPt.z - m_leftBottomPt.z );
- pVertices[( i*m_iHeightSampleNum ) + j].position = D3DXVECTOR3( x, y, z );
- pVertices[( i*m_iHeightSampleNum ) + j].color = 0xffffffff;
- pVertices[( i*m_iHeightSampleNum ) + j].normal = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
- pVertices[( i*m_iHeightSampleNum ) + j].tu = (float)i/(float)m_iWidthSampleNum * 2.0f;
- pVertices[( i*m_iHeightSampleNum ) + j].tv = (float)j/(float)m_iHeightSampleNum *2.0f;
- }
- }
- hr = pd3dDevice->CreateVertexBuffer( NumberVertex * sizeof(PLANEVERTEX),
- 0 /*Usage*/, D3D_PLANEVT_FVF, D3DPOOL_MANAGED, &m_pVB, NULL );
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"Create vertex buffer failed", L"Create Failed", MB_OK );
- return hr;
- }
- void* pVoidVertex;
- hr = m_pVB->Lock( 0, NumberVertex * sizeof(PLANEVERTEX), (void**)&pVoidVertex, 0);
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"Lock vertex buffer failed", L"Lock Buffer Failed", MB_OK );
- return hr;
- }
- memcpy( pVoidVertex, pVertices, NumberVertex * sizeof(PLANEVERTEX) );
- m_pVB->Unlock();
- ////////////////////////////////////创建Vertex Buffer完毕///////////////////////////////////////////
- //////////////////////////////////////创建纹理///////////////////////////////////////////
- hr = D3DXCreateTextureFromFile( pd3dDevice, L"plane.bmp", &m_pTexture );
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"plane.bmp不在当前目录下", L"创建纹理失败", MB_OK );
- return hr;
- }
- ////////////////////////////////////创建纹理完毕///////////////////////////////////////////
- delete [] pIndices;
- delete [] pVertices;
- return hr;
- }
- void CPlane::OnRender( IDirect3DDevice9* pd3dDevice )
- {
- HRESULT hr;
- pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- // pd3dDevice->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1 );
- // pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
- D3DXMATRIX matWorld;
- D3DXMatrixIdentity( &matWorld );
- pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
- pd3dDevice->SetTexture( 0, m_pTexture );
- pd3dDevice->SetFVF( D3D_PLANEVT_FVF );
- pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(PLANEVERTEX) );
- pd3dDevice->SetIndices( m_pIB );
- hr = pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
- 0, // BaseVertexIndex
- 0, // MinIndex
- m_iWidthSampleNum * m_iHeightSampleNum, // NumVertices
- 0, // StartIndex
- 2*( m_iWidthSampleNum - 1 ) * ( m_iHeightSampleNum - 1 ));// PrimitiveCount
- if( FAILED(hr) )
- {
- MessageBox( NULL, L"Draw Indexed Primitive failed", L"Draw Plane Failed", MB_OK );
- return;
- }
- // pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
- }