Archived

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

Sheep

really wrong terrain generator

Recommended Posts

Hello everybody. I was hoping for some help with this little terrain generator. I had no clue what I was doing so I just started writing. After 8 hours I got something to work....somewhat. It doesnt really look like a terrain and cant figure out the orientation of the thing or what it REALLY is. To generate the so called "terrain" I used this code:
  HRESULT CTerrain::Generate(float scale, int height,   LPDIRECT3DDEVICE8 m_pd3dDevice)
{
	srand(100);
	
	// Calculate points (hopefully)

        for (int z = 0; z < 31; z++)
	{
		for (int x = 0; x < 31; x++)
		{
		mapData[x][z].x = float(x)* 20;
		mapData[x][z].y = (rand() % height) * scale;
		mapData[x][z].z = float(z)* 20;
		}			
	}

	// Do vertex buffer stuff

        vertexSize = sizeof(mapData);
	if(FAILED(m_pd3dDevice->CreateVertexBuffer(vertexSize,   0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &m_pVB)))
		return E_FAIL;

	void* pVertices;
	if(FAILED(m_pVB->Lock(0, vertexSize, (BYTE**)&pVertices, 0)))
		return E_FAIL;
	
	memcpy(pVertices, mapData, vertexSize);
	
	m_pVB->Unlock();

	return S_OK;
}  
Then when I move up 100 units and look down, I''m not really sure what I see..... any help would be appreciated. I have no clue what I am doing.

Share this post


Link to post
Share on other sites
Hey, I just made one yesterday surprisingly enough! Anyway...I think your problem is...it sounds like you are not using index buffers as well as vertex buffers. What methods do you use to draw the landscape? DrawPrimitive, DrawIndexedPrimitive, etc?

If you need some more help I can tell you how to associate the index buffer with the vertex buffer a bit (for the landscape).

Cocyen

Share this post


Link to post
Share on other sites
I am calling DrawPrimitive with triangle lists. However, why would using index buffers make it work? But if it does then your help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Well, a Triangle Strip cannot really draw much, except for strips...you''d have to stop drawing for each one, and then start over...Setting up a index buffer with a flat plane can be a bit of a pain...but...if you don''t use one, then DrawPrimitive will not know how make what triangles with what vertices.

First of all, an indexbuffer is like an array of the triangles on the land

  
struct LandIndex
{
WORD v0;
WORD v1;
WORD v2;
}


See...it''s really an array of WORDs (if it''s 16 bit, and most I''ve seen are). And a struct like this...v0, v1, v2 each point to a vertex. So you have 1 of these for each triangle in the land...which can be calculated by...

int width = 32;
int depth = 32;
int numTri = (width-1) * (depth-1) * 2;

Here is the code for setting the index buffer to the land if you decide to go with those.

  
{
LandIndex * pI = NULL;

hr = m_pIB->Lock(0, 0, (BYTE**) &pI, 0);

int ix=0, iz=0;
int ii = 0;
int width = 32; /// or whatever width of your land

int depth = 32;

for(ii=0; ii<=numTri-2; ii+=2)
{
LandIndex & ind0 = pI[ii];
LandIndex & ind1 = pI[ii+1];

ind1.v0 = ix + iz*width;
ind1.v1 = ix + (iz+1)*width;
ind1.v2 = ix+1 + (iz+1)*width;

ind0.v0 = ix + iz*width;
ind0.v1 = ix+1 + (iz+1)*width;
ind0.v2 = ix+1 + (iz)*width;


ix++;

if(ix>=width-1)
{
ix = 0;
++iz;
}
}
}


You''ll have to read about setting up index buffers in the SDK...it''s not too rough...but...index buffers are really useful!

Share this post


Link to post
Share on other sites
I am not really sure why I cant just use triangle lists but I will try your suggestion. Also, I cant seem to figure out its orientation.....I rotated the camera in every way I can think of but I still cant seem to make it out. If you want the entire project (~250 lines) I can email it to you.

Share this post


Link to post
Share on other sites
Hehehehe.....ok, so I set up the index buffer and then I used the code that you showed me...then when I run it, green and purple lines flash randomly across the screen. Its actually pretty cool, but not what I am looking for

Share this post


Link to post
Share on other sites