GAH! Heightmaps

Started by
2 comments, last by shakazed 20 years, 9 months ago
I have a slight problem when it gets to rendering my heightmap. I think it´s something wrong with the index buffer since drawindexedprimitive fails. Here´s the code for initializing vertexbuffer and indexbuffer.

CUSTOMVERTEX vertices[128*128];

	for(int i = 0; i<128; ++i)
	{
		for(int j = 0; j<128; ++j)
		{
	
			vertices[TERRAIN_Z*i+j].x = (float)j;
			vertices[TERRAIN_Z*i+j].y = 0.0f;//iHeightMapArray[i*TERRAIN_Z+j];

			vertices[TERRAIN_Z*i+j].z = (float)i;
			vertices[TERRAIN_Z*i+j].d3dColor = D3DCOLOR_XRGB(0,255,0);
		}
	}


	if(FAILED(p_iD3DDevice9->CreateVertexBuffer((128*128)*sizeof(CUSTOMVERTEX),
		0, CUSTOMVERTEX_FVF, D3DPOOL_DEFAULT,&p_iD3DVertexBuffer9,NULL)))
	{
		MessageBox(NULL, "Can´t create vertexbuffer","Error", MB_OK);
		return false;
	}
	
	
	VOID* pVertices;
	if( FAILED(p_iD3DVertexBuffer9->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
		return false;

	memcpy( pVertices, vertices, sizeof(vertices) );
	p_iD3DVertexBuffer9->Unlock();


	
	
	//Now set up the index buffer


	long dIndices[127*127*6];


	long dPos = 0;

	for(int y = 0;y < 127;++y)
	{
		dPos = 0;

		for(int x = 0;x < 127;++x)
		{
			dIndices[TERRAIN_Z*y+dPos] = x + y * (TERRAIN_X + 1);	
			dPos++; //Go next

			dIndices[TERRAIN_Z*y+dPos]= x + 1 + y * (TERRAIN_X + 1);					//v2

			dPos++; //Go next

			dIndices[TERRAIN_Z*y+dPos] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4

			dPos++; //Go next

			dIndices[TERRAIN_Z*y+dPos] = x + y * (TERRAIN_X + 1);					//v1

			dPos++; //Go next

			dIndices[TERRAIN_Z*y+dPos] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4

			dPos++; //Go next

			dIndices[TERRAIN_Z*y+dPos] = x + (y + 1) * (TERRAIN_X + 1);					//v3

			dPos++;
		}
	}

	if(FAILED(p_iD3DDevice9->CreateIndexBuffer((127*127*6)*sizeof(long),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,
		&p_iD3DIndexBuffer9,NULL)))
	{
		MessageBox(NULL,"Failed to create index buffer", "Error", MB_OK);
		return false;
	}

	VOID *p_vData;
	if(FAILED(p_iD3DIndexBuffer9->Lock(0,sizeof(dIndices),(VOID**)&p_vData,0)))
	{
		MessageBox(NULL,"Failed to lock index buffer", "Error", MB_OK);
		return false;
	}
	memcpy(p_vData,dIndices,sizeof(dIndices));					//copy data

	p_iD3DIndexBuffer9->Unlock();	

	return true;

And this is the code I use to present the scene.

	p_iD3DDevice9->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255),1.0f,0);

	p_iD3DDevice9->BeginScene();
	SetupMatrices(); //Setup camera 

	
	// Set the location source of our vertex input stream

	p_iD3DDevice9->SetStreamSource( 0, p_iD3DVertexBuffer9,0, sizeof(CUSTOMVERTEX));
		
	// Set the location of our index information to our Index Buffer

	if(FAILED(p_iD3DDevice9->SetIndices( p_iD3DIndexBuffer9 )))
	{
		MessageBox(NULL,"Couldn´t set indices", "Error", MB_OK);
		return false;
	}


	if(FAILED(p_iD3DDevice9->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
					   0,
					   0,
					   127*127*2*3,
					   0,
					   127*127*2)))
	{
		MessageBox(NULL,"Failed to draw indexed primitive", "Error", MB_OK);
		return false;
	}



	p_iD3DDevice9->EndScene();

	RECT rRect;
	rRect.left = 0;
	rRect.top  = 0;
	rRect.right = 480;
	rRect.bottom = 480;

	//Render only from and to the given rect

	p_iD3DDevice9->Present(&rRect,&rRect,NULL,NULL);

	return true;

Have tried all sorts of things but there´s prolly something wrong in my thinking then Any help REALLY appreciated.
Advertisement
No clues anyone?
The stuff where you fill in the index buffer looks kind of weird. have you tested it on a smaller size array and examined the contents for correctness after the loops run (not actually renedering anything)?

What are TERRAIN_Z and TERRAIN_X? Why does TERRAIN_Z appear in dIndices[TERRAIN_Z*y+dPos] ? Shouldn''t dPos = 0; appear in the x loop, not the y loop? Does a manually coded small test index buffer render correctly?
TERRAIN_Z and _X are just constants that hold the height and width of the terrain, in this case 128*128. dPos = 0 has to appear in the y loop, it holds the position when creating the six six indices. Also tried to render just one primitive but that failed also.

[edited by - shakazed on July 6, 2003 4:36:45 AM]

[edited by - shakazed on July 6, 2003 6:17:54 AM]

This topic is closed to new replies.

Advertisement