Original Post
Hi everybody, I'm trying to do a terrain renderer lately. My terrain is made of patches, which I want to generate with a mathematical formula (fractal brownian motion). I try then to simplify the mesh programmatically, using the function Mesh.Simplify. When i do this for one patch, everything works perfectly (Mesh is simplified). However, when I try to create more than one patch, the Mesh.Simplify drops a StackOverflowException (in mscorlib.dll). I'm using the April 2006 DirectX SDK, tested with the latest D3DX Util file and the first (2902 and 2911 build version). Here is the call stack, as shown in the IDE: [External Code] > Framework.dll!Framework.Patch.Patch(float Size = 16.0, Framework.NoiseAlgorithm Algorithm = {Framework.FractalMotion}, Microsoft.DirectX.Vector2 Sector = {X: 0 Y: 2 }) Line 51 + 0x12 bytes C# Framework test.exe!Framework_test.ControlPanel.Load() Line 35 + 0x40 bytes C# Framework.dll!Framework.Game.Initialize(object sender = {Framework_test.ControlPanel, Text: ControlPanel}, System.EventArgs e = {System.EventArgs}) Line 19 + 0xa bytes C# [External Code] Framework test.exe!Framework_test.Program.Main() Line 16 + 0x1a bytes C# [External Code] The documentation tells that Simplify can't drop a StackOverflow, so there is my question: what's wrong in my code? Note that this error happens randomly (ie sometimes it will happen, sometimes not, but 90% of the time it will happen). From the main program:
for(float i = -0.0f; i <= 2.0f; i++)
for (float j = -0.0f; j <= 2.0f; j++)
World.Add(new Patch(16f, TerrainAlgorithm, new Vector2(i, j)));
Patch.cs constructor
public Patch(float Size, NoiseAlgorithm Algorithm, Vector2 Sector)
{
PropertySize = Size;
PropertySector = Sector;
#region Generate the base Mesh
Mesh Mesh = new Mesh(MaximumFaceCount, MaximumVertexCount, MeshFlags.Managed, CustomVertex.PositionColored.Format , Graphics.Device);
Mesh.SetVertexBufferData(GenerateVertexes(Size, Algorithm, Sector),LockFlags.Discard);
Mesh.SetIndexBufferData(GenerateIndexes(),LockFlags.Discard);
int[] Adjacency = new int[MaximumFaceCount * 3];
Mesh.GenerateAdjacency(0.01f,Adjacency);
#endregion
Mesh SimplifiedMesh = Mesh.Simplify(Mesh, Adjacency, 100, MeshFlags.SimplifyVertex); // Here I got the StackOverflowException
}
GenerateVertex
private CustomVertex.PositionColored[] GenerateVertexes(float Size, NoiseAlgorithm Algorithm, Vector2 Sector)
{
float NoiseValue;
CustomVertex.PositionColored[] Vertices = new CustomVertex.PositionColored[(PatchSize + 1) * (PatchSize + 1)];
for (int Absciss = 0; Absciss <= PatchSize; Absciss++)
{
float LerpAbsciss = (float)(Absciss-1) / (float)(PatchSize-2);
for (int Ordonnate = 0; Ordonnate <= PatchSize; Ordonnate++)
{
float LerpOrdonnate = (float)(Ordonnate-1) / (float)(PatchSize-2);
Vector3 BasePosition = new Vector3(Size * LerpAbsciss - Size / 2f, 0, Size * LerpOrdonnate - Size / 2f);
// Miniskirt
if(Absciss==0 || Absciss==PatchSize || Ordonnate==0 || Ordonnate==PatchSize)
NoiseValue = -1.0f;
else
NoiseValue = Algorithm.Noise(Vector3.Multiply(new Vector3(BasePosition.X + Sector.X*Size, 0.0f, BasePosition.Z + Sector.Y*Size), 0.01f));
Color HeightColor = Color.White;
Vertices[Absciss + Ordonnate * (PatchSize + 1)] = new CustomVertex.PositionColored(new Vector3(BasePosition.X, (NoiseValue + 1.0f) * 5.0f, BasePosition.Z), HeightColor.ToArgb());
}
}
return (Vertices);
}
GenerateIndexes
private short[] GenerateIndexes()
{
short[] VertexIndices = new short[MaximumFaceCount*3 + 6];
short Iterator = 0;
for (int Absciss = 0; Absciss < PatchSize; Absciss++)
for (int Ordonnate = 0; Ordonnate < PatchSize; Ordonnate++)
{
VertexIndices[Iterator++] = (short)(Absciss + (Ordonnate + 1) * (PatchSize + 1));
VertexIndices[Iterator++] = (short)((Absciss + 1) + Ordonnate * (PatchSize + 1));
VertexIndices[Iterator++] = (short)(Absciss + Ordonnate * (PatchSize + 1));
VertexIndices[Iterator++] = (short)((Absciss + 1) + Ordonnate * (PatchSize + 1));
VertexIndices[Iterator++] = (short)(Absciss + (Ordonnate + 1) * (PatchSize + 1));
VertexIndices[Iterator++] = (short)((Absciss + 1) + (Ordonnate + 1) * (PatchSize + 1));
}
return (VertexIndices);
}
[Edited by - PERECil on July 7, 2006 1:19:49 AM]