Rendering Heightmaps in (C#) DirectX9 : Problems

zeldafreak

Newcomer
Joined
Nov 2, 2005
Messages
4
After three weeks of toil, I have finally decided to share my misery with you. I have gone through several Algorithms, consisting of my own C#-ized ROAM, two quadtree algorithms, and a point rendering algorithm (this one worked, but only render points and lines). My last, and final hope, the following quad-based algorithm.

(The code is in C#)
C#:
public QuadHeightMap(ref Device d, ref Caps c, ref HeightMap hm) : base(ref d, ref c)
		{
			int sideLength = (int)Common.FSqrt(hm.height.Length);
			quadsX = sideLength-1;
			quadsY = sideLength-1;

			//Vector3 scale = new Vector3(1.0f, 1.0f, 1.0f);
			int byteOffset = 0;

			Vector3[] v = new Vector3[quadsX*quadsY];

			byte h = 0;

			for (int y=0;y<quadsY;y++) 
			{
				for (int x=0;x<quadsX;x++) 
				{
					byteOffset = (y*quadsY) + x;
					h = hm.height[byteOffset].Value;

					v[(y*quadsY)+x] = Vector3.Scale(new Vector3(x - (quadsX*0.5f),h, y - (quadsY*0.5f)), 1.0f);
				}
			}

			int[] ind = new int[(quadsX*quadsY)*6];
			int offset = 0;

			for (int y=0;y<quadsY;y++)
			{
				for (int x=0;x<quadsX;x++)
 				{
					ind[offset] = (x + y*quadsY);
					ind[offset+1] = (x + 1 + y*quadsY);
					ind[offset+2] = (x + (y+1)*quadsY);
					ind[offset+3] = (x + 1 + y*quadsY);
					ind[offset+4] = (x + 1 + ((y+1)*quadsY));
					ind[offset+5] = (x + ((y+1)*quadsY));
					LogFile.WriteLine(ind[offset] + ", " + ind[offset+1] + ", " + ind[offset+2] + "\r\n" +
						ind[offset+3] + ", " + ind[offset+4] + ", " + ind[offset+5]);
					offset += 6;
				}
			}
			//LogFile.Close();

			//Mesh m = new Mesh((quadsX*quadsY)*2, (quadsX*quadsY)*6, MeshFlags.Managed, CustomVertex.PositionNormal.Format, device);

			LogFile.WriteLine("Initializing index buffer...");
			LogFile.Close();

			ib = new IndexBuffer(typeof(int), ind.Length+1, device, Usage.None, Pool.Managed);
			ib.SetData(ind, 0, LockFlags.None);

			LogFile.WriteLine("Initializing vertex buffer...");
			vb = new VertexBuffer(typeof(CustomVertex.PositionOnly), quadsX*quadsY*6, device, Usage.Dynamic, CustomVertex.PositionOnly.Format, Pool.Default);

			LogFile.WriteLine("Filling vertex buffer data...");
			GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
			for (int y=0;y<quadsY;y++)
			{
				for (int x=0;x<quadsX;x++)
				{
					data.Write(new CustomVertex.PositionOnly(v[(y*quadsY)+x]));
				}
			}
			vb.Unlock();
		}

This algorithm worked for me once, but now does not. I have no idea why it stopped, but I believe that it has something to do with memory corruption/lack thereof. Currently, my computer hangs on me on the vb.Unlock() call. Wondering if there was a problem, I switched the IndexBuffer.SetData call and the vb.Unlock call and it froze on the SetData call.

Any help will be GREATLY appreciated.
~David

Computer Info:
DirectX 9c, October 2005 SDK
MOBILITY Radeon 7500 (this is a laptop)
Pentium 4, 2.0ghz SpeedStep Processor

If you need anymore information please let me know.
 
Last edited by a moderator:
Back
Top