Archived

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

kapru

Filling an index buffer

Recommended Posts

I keep getting an access violation error while im trying to fill my terrain index buffer. This problem has come out in many versions of my terrain engine and i fiddle with the code and it goes away but i havnt really changed anything. So now im trying to figure out exactly what im doing wrong so i can stop it happening in future projects. here is the structure of my terrain patch
	struct sTerrainPatch
	{
		LPDIRECT3DTEXTURE8 PatchTexture;
		LPDIRECT3DVERTEXBUFFER8 PatchVB;
		LPDIRECT3DINDEXBUFFER8 PatchIB;
		int NumVertices;
		int NumIndices;
		unsigned short *ind;
		int x,y;	//Position the the patch within the heightmap

		bool inited;
		bool filled;
	};
and here is the source of the fill index buffer procedure
cTerrainEngine::FillPatchIndexBuffer(sTerrainPatch *patch)
{
	HRESULT hr;
	int i,j;
	int currvert;

	hr = patch->PatchIB->Lock(	0,
								0,
								(BYTE**)&patch->ind,
								0);
	if(FAILED(hr))
	{
		//write the error to the error log and then try and

		//nicely quit the program

	}

	for(i=0;i<PATCHSIZE;i++)
		for(j=0;j<PATCHSIZE;j++)
		{
			currvert = (j*PATCHSIZEVERTS)+i;
			
			//Add the first tri of the tile

			patch->ind[patch->NumIndices++] = currvert;
			patch->ind[patch->NumIndices++] = currvert+1;
			patch->ind[patch->NumIndices++] = currvert+PATCHSIZEVERTS+1;

			//Add the second tri of the tile

			patch->ind[patch->NumIndices++] = currvert;
			patch->ind[patch->NumIndices++] = currvert+PATCHSIZEVERTS+1;
			patch->ind[patch->NumIndices++] = currvert+PATCHSIZEVERTS;
		}
		patch->PatchIB->Unlock();
}
if you need anything else i''ll post it up asap

Share this post


Link to post
Share on other sites
Can you show us the code where you create the index buffer? I wonder if you have created it large enough

Share this post


Link to post
Share on other sites
Here the index buffer creation code


device->CreateIndexBuffer((PATCHSIZE*PATCHSIZE)*6,
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&patch->PatchIB);

Share this post


Link to post
Share on other sites
What happens if you change
(PATCHSIZE*PATCHSIZE)*6, 

to
(PATCHSIZE*PATCHSIZE)*6*sizeof(unsigned short) 

The former sets the buffer''s size to PATCHSIZE*PATCHSIZE*6 bytes and the latter sets the size to the same number of shorts.

Share this post


Link to post
Share on other sites