Archived

This topic is now archived and is closed to further replies.

steg

applying height data to terrain

Recommended Posts

Hi all, Well, I''ve got all my quads to be drawn using index buffers, code is as follows :
  
void CTerrain::CreateIndices()
{
	int index = 0;
	int offset = 0;
	m_pIndices = new WORD[m_xDim*m_zDim*6];   

	// same as above, but looks neater.....

	int x = m_xDim + 1;
	for(int row=0; row<=m_zDim-1; row++)
	{
		for(int col=0; col < m_xDim; col++)
		{
			m_pIndices[index] = col + x + offset;     
			m_pIndices[index+1] = col + offset;       
			m_pIndices[index+2] = col + (x +1) + offset;    
			m_pIndices[index+3] = col + 1 + offset;    
			m_pIndices[index+4] = col + (x +1) + offset;    
			m_pIndices[index+5] = col + offset;        
			index += 6;
		}
		offset += m_xDim + 1;
	}
}

HRESULT CTerrain::CreateTerrainVertexBuffer()
{

	int indicesSize = (m_xDim+1) * (m_zDim+1);
	m_pTestQuad = new TERRAINVERTEX[indicesSize];

	int index = 0;
	float x = 0, y = 0, z = 0;
	for (int amount = 0; amount < m_zDim+1; amount++)
	{
		for(int i = 0; i < m_xDim+1; i++)
		{
			m_pTestQuad[i+index].x = x;
			m_pTestQuad[i+index].y = y;
			m_pTestQuad[i+index].z = z;
			m_pTestQuad[i+index].DiffuseColor = RGB(0,255,0);
			
			x+=10;  // width of quad

		}
		x = 0;
		z -= 10;    // depth of quad

		y = 0
		index += m_xDim + 1;   // next quad

	}

	HRESULT r = m_pDevice->CreateVertexBuffer(sizeof(TERRAINVERTEX)*indicesSize,0,CUSTOM_ZENVERTEX,D3DPOOL_DEFAULT,&m_pVB,NULL);

	if(FAILED(r))
	{
		return E_FAIL;
	}

	VOID* pVertices = 0;
	r = m_pVB->Lock(0,0,(void**)&pVertices,0);

	CopyMemory(pVertices,m_pTestQuad,indicesSize*sizeof(TERRAINVERTEX));

	if(FAILED(r))
	{
		return E_FAIL;
	}

	m_pVB->Unlock();

	return S_OK;
}
  
Now, you can see that I am setting the y-value for each vertices in the tri''s to 0, this makes for obviously a flat ''3d chess board'' look. I thought by giving the y-value different values I''d get some terrain kind of feel, but no ? Any ideas? Kind regards, Steve As I get older, my mind becomes more cluttered.

Share this post


Link to post
Share on other sites
you have a simple 2D table encoding the height, that's usually an Image.

So from Image[ X ][ Y ] you get Z.
Didn't read the code but if you set Y to 0 and it's your height (in my above example I took Z as my height), then you have a flat surface... and I wonder why you use a height map to begin with...


Using y as height:

for ( x = 0; x < ImageWidth; ++x )
{
for ( z = 0; z < ImageHeight; ++z )
{
Vertex.x = x;
Vertex.z = z;
Vertex.y = ImageData[x][z];
}
}



-* So many things to do, so little time to spend. *-


[edited by - Ingenu on May 8, 2003 12:02:44 PM]

Share this post


Link to post
Share on other sites