Original Post
Ok, I've been playing around with this to understand how my lighting is working and I decided to post it cause when you set bump to true and give it a magnitude it's kinda cool. Anyways thought someone might enjoy it. It could be the start of a cool terrain mesh. Give it a rainbow texture and it's kinda cool. typical call is: CreatePlatform(200,50,0,true,10);
[source lang=c#]
public Mesh CreatePlatform(int HalfWidth, int NumberOfSegments, float BaseElevation, bool Bump, float BumpMag)
{
int numverts = (NumberOfSegments + 1) * (NumberOfSegments + 1);//21x by 21y two dimensional array represented by a one dimensional array
float elev = BaseElevation;
float step = (HalfWidth * 2) / NumberOfSegments;
Random rnd = new Random();
//create one long array for 2 dimensional array representation
CustomVertex.PositionNormalTextured[] verts = new CustomVertex.PositionNormalTextured[numverts];
for (float i = -HalfWidth; i <= HalfWidth; i += step)
for (float j = -HalfWidth; j <= HalfWidth; j += step)
{
if (Bump) elev = BaseElevation + (float)(rnd.NextDouble() * BumpMag);
int ind = Convert.ToUInt16((((i + HalfWidth) / step) * (NumberOfSegments + 1)) + ((j + HalfWidth) / step));
verts[ind] =
new CustomVertex.PositionNormalTextured((float)i, (float)elev, (float)j, 0f, 1f, 0f,
(float)((j + HalfWidth) / (HalfWidth * 2)), (float)((i + HalfWidth) / (HalfWidth * 2)));
}
short[] indices = new short[NumberOfSegments * NumberOfSegments * 2 * 3];//20x20 grid with 2 triangles per grid square and 3 vertices per triangle.
int c = 0;
for (int i = 0; i < NumberOfSegments; i++)
for (int j = 0; j < NumberOfSegments; j++)
{
//first triangle
indices[c] = (short)((i * (NumberOfSegments + 1)) + j);//first this row
indices[c + 1] = (short)(((i + 1) * (NumberOfSegments + 1)) + j);//first next row
indices[c + 2] = (short)(((i + 1) * (NumberOfSegments + 1)) + j + 1);//second next row
//second triangle
indices[c + 3] = (short)((i * (NumberOfSegments + 1)) + j);//first this row
indices[c + 4] = (short)(((i + 1) * (NumberOfSegments + 1)) + j + 1);//second next row
indices[c + 5] = (short)((i * (NumberOfSegments + 1)) + j + 1);//second first row
//advance c
c += 6;
}
Mesh platform = new Mesh((NumberOfSegments * NumberOfSegments * 2), numverts, MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, device);
using (VertexBuffer vb = platform.VertexBuffer)
{
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
data.Write(verts);
vb.Unlock();
}
using (IndexBuffer ib = platform.IndexBuffer)
{
ib.SetData(indices, 0, LockFlags.None);
}
platform.ComputeNormals();
return platform;
}
