简体   繁体   中英

I am trying to draw a 3d cube, but it does not work. Code inside

EDIT: Fixed
Hello everybody, I have got the following eventhandler in my Window:


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

and this is my cube class:


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

However, when I click the button, nothing shows. Is there an error in my event handler?

I am an idiot. Sorry for bothering you. The problem was that I did not set

camera.FarPlaneDistance but only camera.NearPlaneDistance (twice).
Sorry for bothering you .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM