www.pudn.com > HeightMap_D3D.zip > cTerrain.cpp


//----------------------------------------------------------------------------- 
// File: cTerrain.cpp 
// Desc: Loading and setting the terrain here from a heightMap using brute 
//		 force. 
//----------------------------------------------------------------------------- 
#include "Main.h" 
#include "cTerrain.h" 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: cTerrain() 
// Desc: Constructor of our cTerrain Class. 
//----------------------------------------------------------------------------- 
cTerrain::cTerrain() 
{ 
	m_pVB = NULL; 
	SetVertexBuffer(); 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: SetVertexBuffer() 
// Desc: Creating And Setting Vertices and Vertex Buffer Here.  
//----------------------------------------------------------------------------- 
HRESULT cTerrain::SetVertexBuffer() 
{ 
	static CUSTOMVERTEX pVertices[(TERRAIN_X - 1) * (TERRAIN_Y - 1) * 6]; 
	unsigned char pHeightMap[TERRAIN_X][TERRAIN_Y]; 
	ifstream File; 
 
	File.open("heightmap.raw",ios::binary); 
	for(int j = 0; j < TERRAIN_Y; j++ ) 
	{ 
		for(int i = 0; i < TERRAIN_X; i++ ) 
		{ 
			File.get(pHeightMap[i][j]); 
		} 
	} 
	File.close(); 
 
	int VertexCount = 0; 
	for(int y = 0; y < TERRAIN_Y - 1; y++ ) 
	{ 
		for(int x = 0;x < TERRAIN_X - 1; x++ ) 
		{ 
			pVertices[VertexCount].Point = D3DXVECTOR3( (float)x, (float)y, (float)pHeightMap[x][y] / HAX_HEIGHT ); 
			pVertices[VertexCount + 1].Point = D3DXVECTOR3( (float)x + 1, (float)y, (float)pHeightMap[x + 1][y] / HAX_HEIGHT ); 
			pVertices[VertexCount + 2].Point = D3DXVECTOR3( (float)x + 1, (float)y + 1, (float)pHeightMap[x + 1][y + 1] / HAX_HEIGHT ); 
			pVertices[VertexCount + 3].Point = D3DXVECTOR3( (float)x, (float)y, (float)pHeightMap[x][y] / HAX_HEIGHT ); 
			pVertices[VertexCount + 4].Point = D3DXVECTOR3( (float)x + 1, (float)y  + 1, (float)pHeightMap[x + 1][y + 1] / HAX_HEIGHT ); 
			pVertices[VertexCount + 5].Point = D3DXVECTOR3( (float)x, (float)y + 1, (float)pHeightMap[x][y + 1] / HAX_HEIGHT ); 
 
			VertexCount += 6; 
		} 
	} 
 
	if( FAILED( g_pApp->m_pD3DDevice->CreateVertexBuffer( sizeof(pVertices), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVB, NULL ) ) ) 
    { 
		MessageBox( hWnd, "CreateVertexBuffer() failed!", "cTerrain::SetVertexBuffer()", MB_OK ); 
        return E_FAIL; 
    } 
 
	VOID* pData; 
    if( FAILED( m_pVB->Lock( 0, sizeof(pVertices), (void**)&pData, 0 ) ) ) 
	{ 
		MessageBox( hWnd, "Lock() failed!", "cTerrain::SetVertexBuffer()", MB_OK ); 
        return E_FAIL; 
	} 
    memcpy( pData, pVertices, sizeof(pVertices) ); 
    m_pVB->Unlock(); 
 
	return S_OK; 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: Render() 
// Desc: For rendering this object to the screen.  
//----------------------------------------------------------------------------- 
HRESULT cTerrain::Render() 
{ 
	if(FAILED(g_pApp->m_pD3DDevice->SetStreamSource( 0, m_pVB, 0, sizeof(CUSTOMVERTEX) ))) 
	{ 
		MessageBox( hWnd, "SetStreamSource() failed!", "cTerrain::Render()", MB_OK ); 
        return E_FAIL; 
	} 
 
    g_pApp->m_pD3DDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); 
    g_pApp->m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,16129); 
	g_pApp->m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,48387,16129); 
	return S_OK; 
} 
 
 
 
 
//----------------------------------------------------------------------------- 
// Func: ~cTerrain() 
// Desc: Deconstructor for releasing memory. 
//----------------------------------------------------------------------------- 
cTerrain::~cTerrain() 
{ 
	SafeRelease(m_pVB); 
}