Archived

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

aker_jus

Increasing a dynamic VB capacity.

Recommended Posts

Hello, I am adding water chunks for my landscape engine, all chunks using a common VB. Now, while I can easily do this with one VB per chunk, I am in front of this problem. Inside the WaterCreate function, I create a new water chunk and I need to make a vb. Now, I do not know how many waters I will have, so i cannot use a standard size for the CreateVertexBuffer function, the only sure, it is that it will be a multiplicant of 4 * sizeof(LITVERTEX). Here is my current code (I hope I am not tiring you a lot..)
	RTLVERTEX* pOldV = NULL;

	RTLVERTEX* pVertices=NULL;

	//backup old vertices.
	if (land.water.size() > 0)
	{
		pOldV = new RTLVERTEX[land.water.size() * 4];

		if (FAILED(land.pWaterVB->Lock(0, 0, (void**)&pVertices, D3DLOCK_NOOVERWRITE)))
		{
			SD(pOldV);
			return RT_FAIL_VBLOCK;
		}
			for (UINT i=0; i = pVertices[i];

		if (FAILED(land.pWaterVB->Unlock()))
		{
			SD(pOldV);
			return RT_FAIL_VBUNLOCK;
		}

		SR(land.pWaterVB);
	}

	if (FAILED(pRTCore->Compon->pDev->CreateVertexBuffer((UINT)(land.water.size()+1) * 4 * sizeof(RTLVERTEX), D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,
						LVERTEX, D3DPOOL_DEFAULT, &land.pWaterVB, NULL)))
			return RT_FAIL_VBCREATE;

	//if we are creating the vb for the 2nd time, we need to restore the vertices.
	if (land.water.size() > 0)
	{
		if (FAILED(land.pWaterVB->Lock(0, 0, (void**)&pVertices, D3DLOCK_NOOVERWRITE)))
		{
			SD(pOldV);
			return RT_FAIL_VBLOCK;
		}
			for (UINT i=0; iUnlock()))
		{
			SD(pOldV);
			return RT_FAIL_VBUNLOCK;
		}
	}
	SD(pOldV);
	if (FAILED(land.pWaterVB->Lock((UINT)land.water.size() * 4 * sizeof(RTLVERTEX), (UINT)land.water.size() * 4 * sizeof(RTLVERTEX), (void**)&pVertices, 0)))
		return RT_FAIL_VBLOCK;

		pVertices[0].CreateLVertex (Vec3(0,0,0), water.color, 0,0);
		pVertices[1].CreateLVertex (Vec3(0, 0, (float)height), water.color, 0,8);
		pVertices[2].CreateLVertex (Vec3((float)width, 0, 0), water.color, 8,0);
		pVertices[3].CreateLVertex (Vec3((float)width, 0, (float)height), water.color, 8,8);

	if (FAILED(land.pWaterVB->Unlock()))
		return RT_FAIL_VBUNLOCK;
 
What I am trying to do here, is keep a backup of the old vertex buffer contents and fill the new one with those. Then, fill the new quad too. But this isn''t working for some reason. I am rendering from the VB correctly, it works, its just this part. I might be missing something, it can''t be so complex. Thanks

Share this post


Link to post
Share on other sites
Unfortunately you cannot resize a VB - you can create a new one and copy the smaller ones dat ain but this would be slow. You are probably best working out your worst case scenario and just setting that as the VB size (or use the Max number from the Caps of your card).

I could be wrong on the following point (but i''ll put it so i can be proved wrong) - try Draw*PrimitiveUP and use a dynamic array (ie STL::vector). This may work out faster than creating and destroying VBs every frame. May be slower than 1 huge VB though.

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites
I thought of that. I won''t use UP, as DrawPrimitive is faster in 99% cases. I guess I have to use a bigger vb.. too bad. Anyone else has something to suggest? (Simon?)

Share this post


Link to post
Share on other sites