IndexBuffer Blues

Started by
2 comments, last by langguy 20 years, 8 months ago
I am trying to write a simple terrain engine (which is turning out to be not so simple). I've got most of the code down (C#/DX9), but when it comes to drawing the result on the screen, it fails when I try to set the device's indexbuffer. Here is my source code:

//1) Clear the back buffer

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, clClear, 1.0F, 0);

//2) indicate that we are ready to being drawing to the back buffer

device.BeginScene();

//3) draw scene -- actual rendering goes here

if(bmpHeightMap != target.bmpHeightMap)
{//if a new heightmap has been loaded on the main form, load it here too

	bmpHeightMap = target.bmpHeightMap;
	
	//create a vertex/index buffer

	int nBmpWidth = bmpHeightMap.Width;
	int nBmpHeight = bmpHeightMap.Height;
	int nTop = (nBmpHeight / -2) * 20;
	int nLeft = (nBmpWidth / -2) * 20;
	Color clPixel = Color.Black;
	CustomVertex.PositionColored[] vertices =
			new CustomVertex.PositionColored[nBmpWidth * nBmpHeight];
	int[] nIndices = new int[(6 * (nBmpWidth - 1) * (nBmpHeight - 1))];
	
	
	for(int i = 0; i < nBmpWidth; i++)
	{//create the vertices for the vertex buffer

		for(int j = 0; j < nBmpHeight; j++)
		{
			clPixel = bmpHeightMap.GetPixel(i,j);
			vertices[nCount++] = new CustomVertex.PositionColored(
								nLeft + (i * 20),
								nTop + (j * 20),
								clPixel.R * 5, Color.Green.ToArgb());
		}
	}

	nCount = 0;
	
	for(int i = 0; i < nBmpHeight * nBmpWidth; i += nBmpWidth)
	{//create the indices for the index buffer

		if(i != (nBmpWidth * (nBmpHeight - 1)))
		{
			for(int j = 0; j < nBmpWidth - 1; j++)
			{
				nIndices[nCount++] = i + j;					//A1

				nIndices[nCount++] = i + j + 1;				//A2

				nIndices[nCount++] = i + j + nBmpWidth;		//B1

				nIndices[nCount++] = i + j + 1;				//A2

				nIndices[nCount++] = i + j + nBmpWidth + 1;	//B2

				nIndices[nCount++] = i + j + nBmpWidth;		//B1

			}
		}
	}
	ibTerrain = new IndexBuffer(typeof(int), (int)nCount, device, 0, Pool.Default);
	vbTerrain = new VertexBuffer(typeof(CustomVertex.PositionColored), vertices.Length,
									device, 0, CustomVertex.PositionColored.Format,
									Pool.Default);
	//write values for index buffer

	GraphicsStream stmIB = ibTerrain.Lock(0,0,0);
	stmIB.Write(nIndices);
//						ibTerrain.SetData(nIndices, 0, 0);

	ibTerrain.Unlock();
	
	//write values for vertex buffer

	GraphicsStream stmVB = vbTerrain.Lock(0,0,0);
	stmVB.Write(vertices);
	vbTerrain.Unlock();
	
	//set camera to look at map from a distance


	clClear = Color.Black;
}
if(bmpHeightMap != null)
{//don't draw anything if a heightmap hasn't been loaded. otherwise, draw it!

	//draw the heightmap

	device.VertexFormat = CustomVertex.PositionColored.Format;
	device.SetStreamSource(0, vbTerrain, 0, VertexInformation.GetFormatSize(CustomVertex.PositionColored.Format));
	device.RenderState.FillMode = FillMode.Solid;
	device.Indices = ibTerrain;
	device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, (int)nCount, 0, (int)nCount / 6);
}
//4) indicate that we are done drawing to the back buffer

device.EndScene();

//5) Copy back buffer to the display

device.Present();
It doesn't throw an error until the "device.Indices = ibTerrain;". I looked at the SDK "fractal" example and the index buffer part is almost identical. What could possibly be wrong?? PS: Does anyone know why for the last few days GameDev.net's server is down more often than not? [edited by - langguy on August 12, 2003 5:06:35 PM]
Advertisement
Guys! I could really use some help with this! Please?
I''m not knowledgeable about the managed stuff just yet, but in your Lock call, you''re passing 0. Should you be passing the size of the buffer? Or will it take 0 as ''lock all of it''?

I like pie.
[sub]My spoon is too big.[/sub]
I think 0 does mean lock the whole thing. I tried it anyways, but unfortunately, it didn''t work.

This topic is closed to new replies.

Advertisement