正在努力让这个自定义网格工作。GUI表单上的结果是白色背景上的红十字(DirectX设备错误)。我可以很容易地让Mesh.Box或Mesh.Sphere工作,但创建自定义网格的尝试失败了。我看了无数的例子,但仍然没有快乐。
希望你能帮忙。
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();
}
网格对象似乎需要在OnDeviceReset中恢复,所以我克隆了它。我在互联网上没有看到解决方案,只是预感。在OnDeviceReset中,我写道。。。meshSubject=meshSubject.Clone(meshSubject.Options.Value,meshSubject.VertexFormat|VertexFormats.Normal,device);