Struggling to get this custom mesh working. The result on the GUI form is a red cross on a white background (DirectX device error). I can easily get Mesh.Box or Mesh.Sphere working but the attempt to create a custom Mesh fails. I have looked through numerous examples, but still no joy.
Hope you can help.
private Mesh meshSubject = null;
int numberVerts = 36;
short[] indices = {
0,1,2, // Front Face
1,3,2, // Front Face
4,5,6, // Back Face
6,5,7, // Back Face
0,5,4, // Top Face
0,2,5, // Top Face
1,6,7, // Bottom Face
1,7,3, // Bottom Face
0,6,1, // Left Face
4,6,0, // Left Face
2,3,7, // Right Face
5,2,7 // Right Face
};
private void OnDeviceReset(object sender, EventArgs e)
{
// THESE TEST MESHES WORK
//meshSubject = Mesh.Box(device, 0.8f, 0.18f, 2.2f);
//meshSubject = Mesh.Sphere(device, 0.5f, 8,1000);
// **** start of custom mesh - THIS CUSTOM MESH DOES NOT WORK :-(
Mesh meshSubject = new Mesh(indices.Length / 3, numberVerts, MeshFlags.Managed, CustomVertex.PositionColored.Format, device);
IndexBuffer indicesBuff = meshSubject.IndexBuffer;
VertexBuffer verticesBuff = meshSubject.VertexBuffer;
GraphicsStream data = verticesBuff.Lock(0, 0, LockFlags.None);
data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, 0x00ff00ff));
verticesBuff.Unlock();
indicesBuff.SetData(indices, 0, LockFlags.None);
device.SetStreamSource(0, verticesBuff, 0);
//**** end of custom mesh *******************
device.RenderState.Ambient = Color.White;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(0.3f, -0.5f, 0.2f);
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Update();
device.Lights[1].Type = LightType.Directional;
device.Lights[1].Direction = new Vector3(0.0f, 1.0f, -3.0f);
device.Lights[1].Diffuse = Color.White;
device.Lights[1].Update();
device.Lights[0].Enabled = true;
device.Lights[1].Enabled = true;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 5.0F, (float)this.ClientSize.Width / (float)this.ClientSize.Height, 2.0f, 80.0f);
}
protected override void OnPaint(PaintEventArgs e)
{
//Render();
Form.ActiveForm.Update();
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
Color meshColor = Color.White;
Material material = new Material();
// Begin the scene and clear the back buffer to black.
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
SetupMatrices();
meshSubject.DrawSubset(0);
device.VertexFormat = CustomVertex.PositionNormal.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
device.Lights[2].Enabled = true;
material.Diffuse = Color.Olive;
device.Material = material;
device.EndScene();
device.Present();
this.Invalidate();
}
The mesh object seems to need reinstating inside the OnDeviceReset, so I cloned it. I did not see a solution for this on the internet but was just a hunch. Within OnDeviceReset I wrote... meshSubject = meshSubject.Clone(meshSubject.Options.Value,meshSubject.VertexFormat | VertexFormats.Normal,device);