DirectX C#Custom Mesh

时间:2015-03-27 13:08:22

标签: c# directx mesh managed

努力让这个自定义网格工作。 GUI表单上的结果是白色背景上的红叉(DirectX设备错误)。我可以很容易地使Mesh.Box或Mesh.Sphere工作但是创建自定义Mesh的尝试失败了。 我看了很多例子,但仍然没有快乐。

希望你能提供帮助。

    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();
}

1 个答案:

答案 0 :(得分:0)

网格对象似乎需要在OnDeviceReset中恢复,所以我克隆了它。我没有在互联网上看到这个解决方案,但只是一个预感。在OnDeviceReset中我写了...... meshSubject = meshSubject.Clone(meshSubject.Options.Value,meshSubject.VertexFormat | VertexFormats.Normal,device);