我试图绘制一个三维立方体,但它不起作用。代码里面

时间:2010-07-21 18:29:19

标签: c# wpf 3d drawing

编辑:修正
各位大家好,我的窗口中有以下事件处理程序:


private void buttonView_Click(object sender, RoutedEventArgs e)
{
    //Camera
    PerspectiveCamera camera = new PerspectiveCamera();
    camera.LookDirection = new Vector3D(5, -2, -3);
    camera.Position = new Point3D(-5, 2, 3);
    camera.UpDirection = new Vector3D(0, 1, 0);
    camera.NearPlaneDistance = 1;
    camera.FarPlaneDistance = 10;
    //Lighting
    DirectionalLight light = new DirectionalLight(Colors.White, new Vector3D(-3, -4, -5));
    //Cube
    Cube cube = new Cube();
    GeometryModel3D cubeModel = new GeometryModel3D();
    cubeModel.Geometry = cube.Mesh;
    cubeModel.Material = new DiffuseMaterial(Brushes.Red);
    //ModelGroup
    Model3DGroup modelGroup = new Model3DGroup();
    modelGroup.Children.Add(light);
    modelGroup.Children.Add(cubeModel);
    //Model
    ModelVisual3D model = new ModelVisual3D();
    model.Content = modelGroup;
    //Viewport
    Viewport3D view = new Viewport3D();
    view.Camera = camera;
    view.Children.Add(model);
    //Show it all
    Frame f = new Frame();
    f.Content = view;
    grid1.Children.Add(f);
}

这是我的立方体类:


public class Cube : Primitive
{
    Point3D[] positions = new Point3D[] {
        new Point3D(0, 1, 1),
        new Point3D(0, 0, 1),
        new Point3D(1, 1, 1),
        new Point3D(1, 0, 1),
        new Point3D(1, 1, 0),
        new Point3D(0, 0, 0),
        new Point3D(0, 1, 1),
        new Point3D(0, 0, 1),
        new Point3D(0, 1, 0),
        new Point3D(0, 1, 0),
        new Point3D(1, 1, 1),
        new Point3D(0, 1, 1),
        new Point3D(1, 1, 1),
        new Point3D(1, 0, 1),
        new Point3D(1, 1, 0),
        new Point3D(1, 0, 0),
        new Point3D(1, 0, 1),
        new Point3D(0, 0, 1),
        new Point3D(1, 0, 0),
        new Point3D(0, 0, 0),
        new Point3D(1, 1, 0),
        new Point3D(1, 0, 0),
        new Point3D(0, 1, 0),
        new Point3D(0, 0, 0)
        };
    int[] vertices = new int[] {
        0,
        1,
        2,
        1,
        3,
        2,
        4,
        5,
        6,
        5,
        7,
        6,
        8,
        9,
        10,
        9,
        11,
        10,
        12,
        13,
        14,
        13,
        15,
        14,
        16,
        17,
        18,
        17,
        19,
        18,
        20,
        21,
        22,
        21,
        23,
        22
        };
    public Cube()
    {
        this.Mesh.Positions = new Point3DCollection(positions);
        this.Mesh.TriangleIndices = new Int32Collection(vertices);
    }
}

public abstract class Primitive
{
    public Primitive()
    {
        this.Mesh = new MeshGeometry3D();
    }
    public MeshGeometry3D Mesh { get; protected set; }
}

然而,当我点击按钮时,没有显示任何内容。 我的事件处理程序是否有错误?

1 个答案:

答案 0 :(得分:1)

我是个白痴。很抱歉打扰你。问题是我没有设置

camera.FarPlaneDistance但只有camera.NearPlaneDistance(两次) 很抱歉打扰你