Пример #1
0
        public static XMVector Project(
            XMVector v,
            float viewportX,
            float viewportY,
            float viewportWidth,
            float viewportHeight,
            float viewportMinZ,
            float viewportMaxZ,
            XMMatrix projection,
            XMMatrix view,
            XMMatrix world)
        {
            float halfViewportWidth  = viewportWidth * 0.5f;
            float halfViewportHeight = viewportHeight * 0.5f;

            XMVector scale  = new XMVector(halfViewportWidth, -halfViewportHeight, viewportMaxZ - viewportMinZ, 0.0f);
            XMVector offset = new XMVector(viewportX + halfViewportWidth, viewportY + halfViewportHeight, viewportMinZ, 0.0f);

            XMMatrix transform = XMMatrix.Multiply(world, view);

            transform = XMMatrix.Multiply(transform, projection);

            XMVector result = XMVector3.TransformCoord(v, transform);

            result = XMVector.MultiplyAdd(result, scale, offset);

            return(result);
        }
Пример #2
0
        public static XMVector Unproject(
            XMVector v,
            float viewportX,
            float viewportY,
            float viewportWidth,
            float viewportHeight,
            float viewportMinZ,
            float viewportMaxZ,
            XMMatrix projection,
            XMMatrix view,
            XMMatrix world)
        {
            XMVector d = XMVector.FromFloat(-1.0f, 1.0f, 0.0f, 0.0f);

            XMVector scale = new XMVector(viewportWidth * 0.5f, -viewportHeight * 0.5f, viewportMaxZ - viewportMinZ, 1.0f);

            scale = scale.Reciprocal();

            XMVector offset = new XMVector(-viewportX, -viewportY, -viewportMinZ, 0.0f);

            offset = XMVector.MultiplyAdd(scale, offset, d);

            XMMatrix transform = XMMatrix.Multiply(world, view);

            transform = XMMatrix.Multiply(transform, projection);
            transform = transform.Inverse();

            XMVector result = XMVector.MultiplyAdd(v, scale, offset);

            return(XMVector3.TransformCoord(result, transform));
        }
Пример #3
0
        static void Transform(XMMatrix target, Viewport viewport, float angle, float scale, float w, float h)
        {
            using var r = new XMMatrix();
            using var b = new XMMatrix();
            using var s = new XMMatrix();
            using var t = new XMMatrix();

            r.RotationZ((float)(angle / 180 * Math.PI));
            s.Scaling(scale, scale, 1.0f);
            t.Translation(viewport.Width / 2 - w * scale / 2, viewport.Height / 2 - h * scale / 2, 0.0f);
            b.Translation(-viewport.Width / 2, -viewport.Height / 2, 0.0f);
            b.Multiply(r);
            r.Set(b);
            b.Identity();
            b.Translation(viewport.Width / 2, viewport.Height / 2, 0.0f);
            r.Multiply(b);

            s.Multiply(t);
            s.Multiply(r);

            target.Set(s);
        }
        private void Animate(double fTime)
        {
            float t = (float)(fTime * 0.2);

            float camera0OriginX = this.cameraOrigins[0].X;
            float camera1OriginX = this.cameraOrigins[1].X;
            float camera2OriginX = this.cameraOrigins[2].X;
            float camera3OriginX = this.cameraOrigins[3].X;
            float camera3OriginZ = this.cameraOrigins[3].Z;

            // animate sphere 0 around the frustum
            {
                BoundingSphere sphere = this.secondarySpheres[0].Sphere;

                sphere.Center = new XMFloat3
                {
                    X = 10 * XMScalar.Sin(3 * t),
                    Y = 7 * XMScalar.Cos(5 * t),
                    Z = sphere.Center.Z
                };

                this.secondarySpheres[0].Sphere = sphere;
            }

            // animate oriented box 0 around the frustum
            {
                BoundingOrientedBox box = this.secondaryOrientedBoxes[0].Box;

                box.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(3.5f * t),
                    Y = 5 * XMScalar.Cos(5.1f * t),
                    Z = box.Center.Z
                };

                box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 1.4f, t * 0.2f, t);

                this.secondaryOrientedBoxes[0].Box = box;
            }

            // animate aligned box 0 around the frustum
            {
                BoundingBox box = this.secondaryAABoxes[0].Box;

                box.Center = new XMFloat3
                {
                    X = 10 * XMScalar.Sin(2.1f * t),
                    Y = 7 * XMScalar.Cos(3.8f * t),
                    Z = box.Center.Z
                };

                this.secondaryAABoxes[0].Box = box;
            }

            // animate sphere 1 around the aligned box
            {
                BoundingSphere sphere = this.secondarySpheres[1].Sphere;

                sphere.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(2.9f * t) + camera1OriginX,
                    Y = 8 * XMScalar.Cos(4.6f * t),
                    Z = 8 * XMScalar.Cos(1.6f * t)
                };

                this.secondarySpheres[1].Sphere = sphere;
            }

            // animate oriented box 1 around the aligned box
            {
                BoundingOrientedBox box = this.secondaryOrientedBoxes[1].Box;

                box.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(3.2f * t) + camera1OriginX,
                    Y = 8 * XMScalar.Cos(2.1f * t),
                    Z = 8 * XMScalar.Sin(1.6f * t)
                };

                box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.7f, t * 1.3f, t);

                this.secondaryOrientedBoxes[1].Box = box;
            }

            // animate aligned box 1 around the aligned box
            {
                BoundingBox box = this.secondaryAABoxes[1].Box;

                box.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(1.1f * t) + camera1OriginX,
                    Y = 8 * XMScalar.Cos(5.8f * t),
                    Z = 8 * XMScalar.Cos(3.0f * t)
                };

                this.secondaryAABoxes[1].Box = box;
            }

            // animate sphere 2 around the oriented box
            {
                BoundingSphere sphere = this.secondarySpheres[2].Sphere;

                sphere.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(2.2f * t) + camera2OriginX,
                    Y = 8 * XMScalar.Cos(4.3f * t),
                    Z = 8 * XMScalar.Cos(1.8f * t)
                };

                this.secondarySpheres[2].Sphere = sphere;
            }

            // animate oriented box 2 around the oriented box
            {
                BoundingOrientedBox box = this.secondaryOrientedBoxes[2].Box;

                box.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(3.7f * t) + camera2OriginX,
                    Y = 8 * XMScalar.Cos(2.5f * t),
                    Z = 8 * XMScalar.Sin(1.1f * t)
                };

                box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.9f, t * 1.8f, t);

                this.secondaryOrientedBoxes[2].Box = box;
            }

            // animate aligned box 2 around the oriented box
            {
                BoundingBox box = this.secondaryAABoxes[2].Box;

                box.Center = new XMFloat3
                {
                    X = 8 * XMScalar.Sin(1.3f * t) + camera2OriginX,
                    Y = 8 * XMScalar.Cos(5.2f * t),
                    Z = 8 * XMScalar.Cos(3.5f * t)
                };

                this.secondaryAABoxes[2].Box = box;
            }

            // triangle points in local space - equilateral triangle with radius of 2
            XMVector trianglePointA = new(0, 2, 0, 0);
            XMVector trianglePointB = new(1.732f, -1, 0, 0);
            XMVector trianglePointC = new(-1.732f, -1, 0, 0);

            XMMatrix triangleCoords;
            XMMatrix translation;

            // animate triangle 0 around the frustum
            triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t);
            translation    = XMMatrix.Translation(
                5 * XMScalar.Sin(5.3f * t) + camera0OriginX,
                5 * XMScalar.Cos(2.3f * t),
                5 * XMScalar.Sin(3.4f * t));
            triangleCoords = XMMatrix.Multiply(triangleCoords, translation);
            this.secondaryTriangles[0].PointA = XMVector3.Transform(trianglePointA, triangleCoords);
            this.secondaryTriangles[0].PointB = XMVector3.Transform(trianglePointB, triangleCoords);
            this.secondaryTriangles[0].PointC = XMVector3.Transform(trianglePointC, triangleCoords);

            // animate triangle 1 around the aligned box
            triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t);
            translation    = XMMatrix.Translation(
                8 * XMScalar.Sin(5.3f * t) + camera1OriginX,
                8 * XMScalar.Cos(2.3f * t),
                8 * XMScalar.Sin(3.4f * t));
            triangleCoords = XMMatrix.Multiply(triangleCoords, translation);
            this.secondaryTriangles[1].PointA = XMVector3.Transform(trianglePointA, triangleCoords);
            this.secondaryTriangles[1].PointB = XMVector3.Transform(trianglePointB, triangleCoords);
            this.secondaryTriangles[1].PointC = XMVector3.Transform(trianglePointC, triangleCoords);

            // animate triangle 2 around the oriented box
            triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t);
            translation    = XMMatrix.Translation(
                8 * XMScalar.Sin(5.3f * t) + camera2OriginX,
                8 * XMScalar.Cos(2.3f * t),
                8 * XMScalar.Sin(3.4f * t));
            triangleCoords = XMMatrix.Multiply(triangleCoords, translation);
            this.secondaryTriangles[2].PointA = XMVector3.Transform(trianglePointA, triangleCoords);
            this.secondaryTriangles[2].PointB = XMVector3.Transform(trianglePointB, triangleCoords);
            this.secondaryTriangles[2].PointC = XMVector3.Transform(trianglePointC, triangleCoords);

            // animate primary ray (this is the only animated primary object)
            this.primaryRay.Direction = new XMVector(XMScalar.Sin(t * 3), 0, XMScalar.Cos(t * 3), 0);

            // animate sphere 3 around the ray
            {
                BoundingSphere sphere = this.secondarySpheres[3].Sphere;

                sphere.Center = new XMFloat3(camera3OriginX - 3, 0.5f * XMScalar.Sin(t * 5), camera3OriginZ);

                this.secondarySpheres[3].Sphere = sphere;
            }

            // animate aligned box 3 around the ray
            {
                BoundingBox box = this.secondaryAABoxes[3].Box;

                box.Center = new XMFloat3(camera3OriginX + 3, 0.5f * XMScalar.Sin(t * 4), camera3OriginZ);

                this.secondaryAABoxes[3].Box = box;
            }

            // animate oriented box 3 around the ray
            {
                BoundingOrientedBox box = this.secondaryOrientedBoxes[3].Box;

                box.Center      = new XMFloat3(camera3OriginX, 0.5f * XMScalar.Sin(t * 4.5f), camera3OriginZ + 3);
                box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.9f, t * 1.8f, t);

                this.secondaryOrientedBoxes[3].Box = box;
            }

            // animate triangle 3 around the ray
            triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t);
            translation    = XMMatrix.Translation(
                camera3OriginX,
                0.5f * XMScalar.Cos(4.3f * t),
                camera3OriginZ - 3);
            triangleCoords = XMMatrix.Multiply(triangleCoords, translation);
            this.secondaryTriangles[3].PointA = XMVector3.Transform(trianglePointA, triangleCoords);
            this.secondaryTriangles[3].PointB = XMVector3.Transform(trianglePointB, triangleCoords);
            this.secondaryTriangles[3].PointC = XMVector3.Transform(trianglePointC, triangleCoords);
        }