terrain problem

Started by
5 comments, last by Penten 20 years, 1 month ago
I have been working on a class that loads terrain from a heigtmap. (using the two kings tutorial) and when I run it I get this rather than what I should be getting, this. Parts of the terrain are rendered above the rest but they seem to be the right shape... strange. This is the code I use to load the heightmap:
// defines //

#define CTERRAIN_X 64
#define CTERRAIN_Y 64
#define D3DFVF_CTERRAINVERTEX ( D3DFVF_XYZ | D3DFVF_DIFFUSE )

// structs //

struct sTerrainVertex
{
float	fX, fY, fZ;
int 	dwColor;
};

// class //

class cTerrain
{
private:
	LPDIRECT3DVERTEXBUFFER9 m_d3dVB;
	LPDIRECT3DINDEXBUFFER9 m_d3dIB;
	int m_vertexNum, m_primitiveNum;
	LPDIRECT3DDEVICE9 m_device;

public:
	void Init( LPDIRECT3DDEVICE9 device, HWND hWnd );
	void Render();
};
void cTerrain::Init(LPDIRECT3DDEVICE9 device, HWND hWnd)
{
	m_device = device;

	m_d3dVB = NULL;
	m_d3dIB = NULL;

	m_vertexNum = ( CTERRAIN_X + 1 ) * ( CTERRAIN_Y + 1 );
	m_primitiveNum = CTERRAIN_X * CTERRAIN_Y * 2;

	char heightMap[CTERRAIN_X+1][CTERRAIN_Y+1];
	sTerrainVertex *vertexData;
	short *indexData;
	std::ifstream file;
	file.open("heightmap.raw",std::ios::binary );//open the hightmap file

	//read in the data

	for( unsigned  y = 0; y < (CTERRAIN_Y + 1); ++y )
	{
		for( unsigned x = 0; x < (CTERRAIN_X + 1); ++x )
		{
			 file.get(heightMap[x][y]);
		}
	}

	file.close(); //close file


	device->CreateVertexBuffer( sizeof( sTerrainVertex ) * m_vertexNum, D3DUSAGE_WRITEONLY,
		D3DFVF_CTERRAINVERTEX, D3DPOOL_MANAGED, &m_d3dVB, NULL );
	m_d3dVB->Lock( 0, 0, (void**)&vertexData, 0 );

	for( y = 0; y < (CTERRAIN_Y + 1); ++y )
	{
		for( unsigned x = 0; x < (CTERRAIN_X + 1); ++x )
		{
		vertexData[x + y * (CTERRAIN_X + 1)].fX = (float)x;
		vertexData[x + y * (CTERRAIN_X + 1)].fY = (float)y;
		vertexData[x + y * (CTERRAIN_X + 1)].fZ = (float)heightMap[x][y] / 15.0f;
		vertexData[x + y * (CTERRAIN_X + 1)].dwColor = 0xffffffff;
		}
	}

	m_d3dVB->Unlock();

	// create and fill index buffer

	// NOTE: read up on IBs

	device->CreateIndexBuffer( sizeof(short) * m_primitiveNum * 3, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, 
		D3DPOOL_MANAGED, &m_d3dIB ,NULL );
	m_d3dIB->Lock(0, 0, (void**)&indexData, 0 );

	for(y = 0;y < CTERRAIN_Y;++y)
	{
		for(unsigned x = 0;x < CTERRAIN_X;++x)
		{
		*indexData++ = x + y * (CTERRAIN_X + 1);
		*indexData++ = x + 1 + y * (CTERRAIN_X + 1);
		*indexData++ = x + 1 + (y + 1) * (CTERRAIN_X + 1);

		*indexData++ = x + y * (CTERRAIN_X + 1);
		*indexData++ = x + 1 + (y + 1) * (CTERRAIN_X + 1);
		*indexData++ = x + (y + 1) * (CTERRAIN_X + 1);
		}
	}

	m_d3dIB->Unlock();

}

void cTerrain::Render()
{
	m_device->SetStreamSource(0,m_d3dVB,0,sizeof(sTerrainVertex));
	m_device->SetIndices(m_d3dIB);
	m_device->SetFVF( D3DFVF_CTERRAINVERTEX );
	m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,m_vertexNum,0,m_primitiveNum );
}
( almost identical to the two kings code at the moment ) Any Ideas? ( using DX9 and MSVC++ 7 ) [edited by - PenTen on March 27, 2004 3:21:28 PM]
Advertisement
Is it possible that your data has negative values which are interpreted as large positive values due to signed/unsigned conversion?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I don''t think so as I''m not assigning any unsigned variables to anything ( I don''t think so anyway.. )
Seems this is coming up only at the lowest part of your heightmap. See if you can insert a check if the value is lower then possible value then make it equal to the lowest possible value.

~Wave
vertexData[x + y * (CTERRAIN_X + 1)].fZ = (float)heightMap[x][y] / 15.0f;

Perhaps if the value is less then 15 in this area, the decimals screw it up? I know that seems retarded, but its worth a shot =P
The two kings tutorial is some what incomplete, i also implemented terrain rendering using their tutorial after many difficulties, i made it to generate U V Texture cords, but don''t know how to set up normals. Here see my engine, there is TerrainCode in it. Use it.

This is the topic where i posted my engines link download it and then tell me. if it works or not.
http://www.gamedev.net/community/forums/topic.asp?topic_id=215956
>>>Syntax is same, Thinking is Different<<<
	if( (float)(heightMap[x][y] ) < 15))        vertexData[x + y * (CTERRAIN_X + 1)].fZ = 8.6f;else	vertexData[x + y * (CTERRAIN_X + 1)].fZ = (float)(heightMap[x][y] / 15.0f);

Thanks for your help everyone, that sugestion was definitley not retarded . Everything works nicely now, I even have some good flat depressions for water or whatever.



[edited by - PenTen on March 28, 2004 3:17:15 AM]

This topic is closed to new replies.

Advertisement