Пример #1
0
        /// <summary>
        /// Initialize a new instance of the <see cref="BezierPatch"/> class.
        /// </summary>
        /// <param name="controlPoints">A 4x4 array containing the control points.</param>
        public BezierPatch(Vector3D[,] controlPoints)
        {
            double[,] bezierMat = new double[4, 4];

            double step = 0.01;
            int    size = 100;

            Point3D[,] latticeVerts = new Point3D[size + 1, size + 1];

            double u = 0;
            double v = 0;

            for (int width = 0; width <= size; ++width)
            {
                v = 0;
                for (int height = 0; height <= size; ++height)
                {
                    // Calculate our matrix given our interpolation.
                    bezierMat[0, 0] = B0(u) * B0(v);
                    bezierMat[0, 1] = B0(u) * B1(v);
                    bezierMat[0, 2] = B0(u) * B2(v);
                    bezierMat[0, 3] = B0(u) * B3(v);

                    bezierMat[1, 0] = B1(u) * B0(v);
                    bezierMat[1, 1] = B1(u) * B1(v);
                    bezierMat[1, 2] = B1(u) * B2(v);
                    bezierMat[1, 3] = B1(u) * B3(v);

                    bezierMat[2, 0] = B2(u) * B0(v);
                    bezierMat[2, 1] = B2(u) * B1(v);
                    bezierMat[2, 2] = B2(u) * B2(v);
                    bezierMat[2, 3] = B2(u) * B3(v);

                    bezierMat[3, 0] = B3(u) * B0(v);
                    bezierMat[3, 1] = B3(u) * B1(v);
                    bezierMat[3, 2] = B3(u) * B2(v);
                    bezierMat[3, 3] = B3(u) * B3(v);

                    Point3D result = new Point3D();

                    // Generate the resulting point used in the final mesh.
                    for (int i = 0; i < 4; ++i)
                    {
                        for (int j = 0; j < 4; ++j)
                        {
                            result += bezierMat[i, j] * controlPoints[i, j];
                        }
                    }

                    latticeVerts[width, height] = result;

                    v += step;
                }

                u += step;
            }

            // Generate the actual mesh from the lattice structure.
            Model3DGroup bezierPatch = new Model3DGroup();
            Material     material    = ShapeGenerator.GetSimpleMaterial(Color.FromArgb(255, 255, 0, 0));

            for (int col = 0; col < size; ++col)
            {
                for (int row = 0; row < size; ++row)
                {
                    Model3DGroup quad = ShapeGenerator.CreateQuad(
                        latticeVerts[row, col],
                        latticeVerts[row + 1, col],
                        latticeVerts[row + 1, col + 1],
                        latticeVerts[row, col + 1],
                        material);

                    bezierPatch.Children.Add(quad);
                }
            }

            Model         = new ModelVisual3D();
            Model.Content = bezierPatch;
        }
Пример #2
0
        public MainWindow()
        {
            InitializeComponent();

            MaterialGroup material = new MaterialGroup();

            material.Children.Add(ShapeGenerator.GetSimpleMaterial(Colors.LightGreen));

            // Create Bezier patch.
            Vector3D[,] controlPoints = new Vector3D[, ]
            {
                { new Vector3D(0, 0, 0), new Vector3D(10, 0, 0), new Vector3D(20, 0, 0), new Vector3D(30, 0, 0) },
                { new Vector3D(0, 0, 10), new Vector3D(10, 0, 10), new Vector3D(20, 0, 10), new Vector3D(30, 0, 10) },
                { new Vector3D(0, 0, 20), new Vector3D(10, 0, 20), new Vector3D(20, 0, 20), new Vector3D(30, 0, 20) },
                { new Vector3D(0, 0, 30), new Vector3D(10, 0, 30), new Vector3D(20, 0, 30), new Vector3D(30, 0, 30) },
            };

            // Add the control points to the model.
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    Transform3DGroup transformGroup = new Transform3DGroup();
                    ModelVisual3D    sphere         = ShapeGenerator.GenerateUnitSphere(30, 30, material);
                    transformGroup.Children.Add(new ScaleTransform3D(0.2, 0.2, 0.2));
                    transformGroup.Children.Add(new TranslateTransform3D(controlPoints[i, j].X, controlPoints[i, j].Y, controlPoints[i, j].Z));
                    sphere.Transform = transformGroup;
                    mainViewport.Children.Add(sphere);
                }
            }

            BezierPatch patch = new BezierPatch(controlPoints);

            mainViewport.Children.Add(patch.Model);

            // Create Bezier patch.
            Vector3D[,] controlPoints1 = new Vector3D[, ]
            {
                { new Vector3D(0, 0, 30), new Vector3D(10, 0, 30), new Vector3D(20, 0, 30), new Vector3D(30, 0, 30) },
                { new Vector3D(0, 0, 40), new Vector3D(10, 0, 40), new Vector3D(20, 0, 40), new Vector3D(30, 0, 40) },
                { new Vector3D(0, 0, 50), new Vector3D(10, 0, 50), new Vector3D(20, 0, 50), new Vector3D(30, 0, 50) },
                { new Vector3D(0, 0, 60), new Vector3D(10, 0, 60), new Vector3D(20, 0, 60), new Vector3D(30, 0, 60) },
            };

            // Add the control points to the model.
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    Transform3DGroup transformGroup = new Transform3DGroup();
                    ModelVisual3D    sphere         = ShapeGenerator.GenerateUnitSphere(30, 30, material);
                    transformGroup.Children.Add(new ScaleTransform3D(0.2, 0.2, 0.2));
                    transformGroup.Children.Add(new TranslateTransform3D(controlPoints1[i, j].X, controlPoints1[i, j].Y, controlPoints1[i, j].Z));
                    sphere.Transform = transformGroup;
                    mainViewport.Children.Add(sphere);
                }
            }

            BezierPatch patch1 = new BezierPatch(controlPoints1);

            mainViewport.Children.Add(patch1.Model);

            // Create ground.
            ModelVisual3D ground = ShapeGenerator.CreatePlane(50, ShapeGenerator.GetSimpleMaterial(Colors.LightGray));

            ground.Transform = new TranslateTransform3D(0, -50, 0);
            mainViewport.Children.Add(ground);

            // Defines the camera used to view the 3D object. In order to view the 3D object,
            // the camera must be positioned and pointed such that the object is within view
            // of the camera.
            PerspectiveCamera myPCamera = new PerspectiveCamera();

            // Define camera's horizontal field of view in degrees.
            myPCamera.FieldOfView = 60;

            // Asign the camera to the viewport
            mainViewport.Camera = myPCamera;

            MouseDown += MouseDownHandler;
            MouseMove += MouseMoveEventHandler;

            MouseWheel += MouseWheelHandler;

            UpdateCameraVectors();
        }