DirectX C# 自定义网格

DirectX C# Custom Mesh

正在努力让这个自定义网格正常工作。 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);