Ejemplo n.º 1
0
        public override void Update(double delta)
        {
            _position = new Vector3(
                (float)_playerAirplane.CurrentState.Position.Y,
                _playerAirplane.Altitude,
                (float)_playerAirplane.CurrentState.Position.X);

            Vector3 orientation = new Vector3((float)_playerAirplane.CurrentState.AngularPosition.X,
                                              (float)_playerAirplane.CurrentState.AngularPosition.Y,
                                              (float)_playerAirplane.CurrentState.AngularPosition.Z);
            // Create the rotation matrix from the yaw, pitch, and roll values (in radians).
            Matrix rotationMatrix = Matrix.RotationYawPitchRoll(orientation.X, orientation.Y, orientation.Z);

            // Get the direction that the camera is pointing to and the up direction
            Vector3 lookAt = Vector3.TransformCoordinate(Vector3.UnitZ, rotationMatrix);
            Vector3 up     = Vector3.TransformCoordinate(Vector3.UnitY, rotationMatrix);

            Vector3 positionDisplacement = Vector3.TransformCoordinate(new Vector3(0, 10, -60), rotationMatrix);

            // Finally create the view matrix from the three updated vectors.
            _viewMatrix = Matrix.LookAtLH(_position + positionDisplacement, _position + positionDisplacement + lookAt, up);

            _uiMatrix = Matrix.LookAtLH(new Vector3(0, 0, -50), Vector3.UnitZ, Vector3.UnitY);

            _reflectionMatrix = Matrix.LookAtLH(new Vector3(_position.X, -_position.Y, _position.Z),
                                                new Vector3(_position.X + lookAt.X, -_position.Y /*- lookAt.Y*/, _position.Z + lookAt.Z), up);
        }
    private void Update3dAudioPosition(FrameUpdateParameters updateParameters, ChannelInputs inputs)
    {
        TrackedDevicePose_t gamePose = updateParameters.GamePoses[OpenVR.k_unTrackedDeviceIndex_Hmd];
        Matrix hmdToWorldTransform   = gamePose.mDeviceToAbsoluteTracking.Convert();

        hmdToWorldTransform.Invert();

        var outputs            = channelSystem.Evaluate(null, inputs);
        var headTotalTransform = headBone.GetChainedTransform(outputs);

        var     headBindPoseCenter = headBone.CenterPoint.GetValue(outputs);
        Vector3 headWorldPosition  = headTotalTransform.Transform(headBindPoseCenter) / 100;

        Vector3 headHmdPosition = Vector3.TransformCoordinate(headWorldPosition, hmdToWorldTransform);

        phononStream.HeadRelativePosition = headHmdPosition;
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Determines whether there is an intersection between a <see cref="SharpDX.Ray"/> and a <see cref="SharpDX.OrientedBoundingBox"/>.
        /// </summary>
        /// <param name="ray">The ray to test.</param>
        /// <param name="point">When the method completes, contains the point of intersection,
        /// or <see cref="SharpDX.Vector3.Zero"/> if there was no intersection.</param>
        /// <returns>Whether the two objects intersected.</returns>
        public bool Intersects(ref Ray ray, out Vector3 point)
        {
            // Put ray in box space
            Matrix invTrans;

            Matrix.Invert(ref Transformation, out invTrans);

            Ray bRay;

            Vector3.TransformNormal(ref ray.Direction, ref invTrans, out bRay.Direction);
            Vector3.TransformCoordinate(ref ray.Position, ref invTrans, out bRay.Position);

            //Perform a regular ray to BoundingBox check
            var bb         = new BoundingBox(-Extents, Extents);
            var intersects = Collision.RayIntersectsBox(ref bRay, ref bb, out point);

            //Put the result intersection back to world
            if (intersects)
            {
                Vector3.TransformCoordinate(ref point, ref Transformation, out point);
            }

            return(intersects);
        }
Ejemplo n.º 4
0
        public static void MakeGlobalShadowMatrix(ref Camera camera, out Matrix globalShadowMatrix)
        {
            var cameraCb = camera.Constants;

            ResetFrustumCorners(ref _frustumCorners);
            var frustumCenter = DxVector3.Zero;
            for (int i = 0; i < 8; ++i)
            {
                DxVector3.TransformCoordinate(ref _frustumCorners[i], ref cameraCb.Data.InverseViewProjection,
                    out _frustumCorners[i]);
                DxVector3.Add(ref frustumCenter, ref _frustumCorners[i], out frustumCenter);
            }
            DxVector3.Divide(ref frustumCenter, 8.0f, out frustumCenter);

            var upDir = DxVector3.Up;

            var lightCameraPos = frustumCenter;
            DxVector3 lookAt;
            DxVector3.Subtract(ref frustumCenter, ref Direction, out lookAt);
            Matrix lightView;
            Matrix.LookAtLH(ref lightCameraPos, ref lookAt, ref upDir, out lightView);

            DxVector3 shadowCameraPos;
            //DxVector3.Multiply(ref Lighting.LightDirection, -0.5f, out shadowCameraPos);
            //DxVector3.Add(ref frustumCenter, ref shadowCameraPos, out shadowCameraPos);
            shadowCameraPos = frustumCenter + Direction * -0.5f;

            Matrix viewMatrix;
            Matrix projMatrix;
            Matrix viewProjMatrix;
            Matrix.OrthoOffCenterLH(-0.5f, 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, out projMatrix);
            Matrix.LookAtLH(ref shadowCameraPos, ref frustumCenter, ref upDir, out viewMatrix);
            Matrix.Multiply(ref viewMatrix, ref projMatrix, out viewProjMatrix);

            Matrix.Multiply(ref viewProjMatrix, ref _texScaleBias, out globalShadowMatrix);
        }
Ejemplo n.º 5
0
        private void Draw3DBox(D3DMatrix viewMatrix, PlayerData player, ShpVector3 playersc, int winWidth, int winHeight, float hei = 180f)
        {
            float num  = 70f;
            float num2 = 60f;
            float num3 = 50f;
            float num4 = 85f;

            hei = 180f;
            ShpVector3 vector  = new ShpVector3(num3, -num2 / 2f, 0f);
            ShpVector3 vector2 = new ShpVector3(num3, num2 / 2f, 0f);
            ShpVector3 vector3 = new ShpVector3(num3 - num, num2 / 2f, 0f);
            ShpVector3 vector4 = new ShpVector3(num3 - num, -num2 / 2f, 0f);

            Matrix     matrix  = Matrix.RotationZ((6.28318548f * (player.Position.Y) / 180f / 2f));
            ShpVector3 vector5 = new ShpVector3(player.Position.X, player.Position.Y, player.Position.Z - num4);

            vector  = ShpVector3.TransformCoordinate(vector, matrix) + vector5;
            vector2 = ShpVector3.TransformCoordinate(vector2, matrix) + vector5;
            vector3 = ShpVector3.TransformCoordinate(vector3, matrix) + vector5;
            vector4 = ShpVector3.TransformCoordinate(vector4, matrix) + vector5;
            ShpVector2 vector6;

            if (!Algorithms.WorldToScreen3DBox(viewMatrix, vector, out vector6, winWidth, winHeight))
            {
                return;
            }
            ShpVector2 vector7;

            if (!Algorithms.WorldToScreen3DBox(viewMatrix, vector2, out vector7, winWidth, winHeight))
            {
                return;
            }
            ShpVector2 vector8;

            if (!Algorithms.WorldToScreen3DBox(viewMatrix, vector3, out vector8, winWidth, winHeight))
            {
                return;
            }
            ShpVector2 vector9;

            if (!Algorithms.WorldToScreen3DBox(viewMatrix, vector4, out vector9, winWidth, winHeight))
            {
                return;
            }

            RawVector2[] array = new RawVector2[]
            {
                vector6,
                vector7,
                vector8,
                vector9,
                vector6
            };
            DrawLines(array, _boxBrush);
            vector.Z += hei;

            bool arg_240_0 = Algorithms.WorldToScreen3DBox(viewMatrix, vector, out vector6, winWidth, winHeight);

            vector2.Z += hei;
            bool flag = Algorithms.WorldToScreen3DBox(viewMatrix, vector2, out vector7, winWidth, winHeight);

            vector3.Z += hei;
            bool flag2 = Algorithms.WorldToScreen3DBox(viewMatrix, vector3, out vector8, winWidth, winHeight);

            vector4.Z += hei;
            bool flag3 = Algorithms.WorldToScreen3DBox(viewMatrix, vector4, out vector9, winWidth, winHeight);

            if (!arg_240_0 || !flag || !flag2 || !flag3)
            {
                return;
            }
            RawVector2[] array2 = new RawVector2[]
            {
                vector6,
                vector7,
                vector8,
                vector9,
                vector6
            };
            DrawLines(array2, _boxBrush);
            DrawLine(new RawVector2(array[0].X, array[0].Y), new RawVector2(array2[0].X, array2[0].Y), _boxBrush);
            DrawLine(new RawVector2(array[1].X, array[1].Y), new RawVector2(array2[1].X, array2[1].Y), _boxBrush);
            DrawLine(new RawVector2(array[2].X, array[2].Y), new RawVector2(array2[2].X, array2[2].Y), _boxBrush);
            DrawLine(new RawVector2(array[3].X, array[3].Y), new RawVector2(array2[3].X, array2[3].Y), _boxBrush);
        }
Ejemplo n.º 6
0
        public static void RenderShadowMap(ref DeviceContext context, ref IWorld world, ref Camera camera)
        {
            PixHelper.BeginEvent(default(RawColorBGRA), "Shadow Rendering");
            {
                if (ShadowMapsNeedRemade)
                    CreateShadowMaps();

                var shadowMapSize = ShadowMap.Width;

                if (AreSplitsDirty || camera != _lastCamera)
                    ComputeSplits(ref camera);

                _lastCamera = camera;
                
                MakeGlobalShadowMatrix(ref camera, out ReceiverConstants.Data.ShadowMatrix);

                context.Rasterizer.SetViewport(_viewport);

                for (int cascadeIndex = 0; cascadeIndex < CascadeCount; ++cascadeIndex)
                {
                    PixHelper.BeginEvent(default(RawColorBGRA), "Cascade");
                    {
                        ResetFrustumCorners(ref _frustumCorners);

                        var prevSplitDist = cascadeIndex == 0 ? MinCascadeDistance : _cascadeSplits[cascadeIndex - 1];
                        var splitDist = _cascadeSplits[cascadeIndex];

                        for (int i = 0; i < 8; i++)
                            DxVector3.TransformCoordinate(ref _frustumCorners[i],
                                ref camera.Constants.Data.InverseViewProjection, out _frustumCorners[i]);

                        for (int i = 0; i < 4; i++)
                        {
                            DxVector3 cornerRay;
                            DxVector3 nearCornerRay;
                            DxVector3 farCornerRay;
                            DxVector3.Subtract(ref _frustumCorners[i + 4], ref _frustumCorners[i], out cornerRay);
                            DxVector3.Multiply(ref cornerRay, prevSplitDist, out nearCornerRay);
                            DxVector3.Multiply(ref cornerRay, splitDist, out farCornerRay);
                            DxVector3.Add(ref _frustumCorners[i], ref farCornerRay, out _frustumCorners[i + 4]);
                            DxVector3.Add(ref _frustumCorners[i], ref nearCornerRay, out _frustumCorners[i]);
                        }

                        var frustumCenter = DxVector3.Zero;
                        for (int i = 0; i < 8; i++)
                            DxVector3.Add(ref frustumCenter, ref _frustumCorners[i], out frustumCenter);
                        DxVector3.Multiply(ref frustumCenter, 0.125F, out frustumCenter);

                        var upDir = DxVector3.Up;

                        float sphereRadius = 0.0f;
                        for (int i = 0; i < 8; ++i)
                        {
                            DxVector3 sum;
                            DxVector3.Subtract(ref _frustumCorners[i], ref frustumCenter, out sum);
                            sphereRadius = Math.Max(sphereRadius, sum.Length());
                        }

                        sphereRadius = (float)Math.Ceiling(sphereRadius * 16.0f) / 16.0f;

                        var maxExtents = new DxVector3(sphereRadius, sphereRadius, sphereRadius);
                        DxVector3 minExtents;
                        DxVector3.Negate(ref maxExtents, out minExtents);

                        DxVector3 cascadeExtents;
                        DxVector3.Subtract(ref maxExtents, ref minExtents, out cascadeExtents);

                        // TODO: optimize
                        var shadowCameraPos = frustumCenter + Direction * -minExtents.Z;

                        Matrix viewMatrix;
                        Matrix projectionMatrix;
                        Matrix viewProjMatrix;

                        Matrix.LookAtLH(ref shadowCameraPos, ref frustumCenter, ref upDir, out viewMatrix);
                        Matrix.OrthoOffCenterLH(minExtents.X, maxExtents.X, minExtents.Y,
                            maxExtents.Y, 0.0f, cascadeExtents.Z, out projectionMatrix);
                        Matrix.Multiply(ref viewMatrix, ref projectionMatrix, out viewProjMatrix);

                        var shadowOrigin = DxVector4.UnitW;
                        DxVector4.Transform(ref shadowOrigin, ref viewProjMatrix, out shadowOrigin);
                        DxVector4.Multiply(ref shadowOrigin, shadowMapSize / 2.0f, out shadowOrigin);

                        var roundedOrigin = new DxVector4(Mathf.Round(shadowOrigin.X), Mathf.Round(shadowOrigin.Y),
                            Mathf.Round(shadowOrigin.Z), Mathf.Round(shadowOrigin.W));
                        DxVector4 roundOffset;
                        DxVector4.Subtract(ref roundedOrigin, ref shadowOrigin, out roundOffset);
                        DxVector4.Multiply(ref roundOffset, 2.0f / shadowMapSize, out roundOffset);
                        roundOffset.Z = 0.0f;
                        roundOffset.W = 0.0f;

                        projectionMatrix.Row4 += roundOffset; // R[3]
                        Matrix.Multiply(ref viewMatrix, ref projectionMatrix, out viewProjMatrix);

                        // RenderDepthCPU
                        SetupRenderDepthState(ref context, ref viewProjMatrix, cascadeIndex);
                        world.RenderObjectProvider.Draw(ref context, ref camera, true);

                        var texScaleBias = new Matrix
                        {
                            Row1 = new SharpDX.Vector4(0.5f, 0.0f, 0.0f, 0.0f),
                            Row2 = new SharpDX.Vector4(0.0f, -0.5f, 0.0f, 0.0f),
                            Row3 = new SharpDX.Vector4(0.0f, 0.0f, 1.0f, 0.0f),
                            Row4 = new SharpDX.Vector4(0.5f, 0.5f, 0.0f, 1.0f)
                        };
                        Matrix.Multiply(ref viewProjMatrix, ref texScaleBias, out viewProjMatrix);

                        var clipNear = camera.ClipNear;
                        var clipFar = camera.ClipFar;
                        var clipDist = clipFar - clipNear;
                        ReceiverConstants.Data.CascadeSplits[cascadeIndex] = clipNear + splitDist * clipDist;

                        Matrix invCascadeMatrix;
                        Matrix.Invert(ref viewProjMatrix, out invCascadeMatrix);

                        var zero = DxVector3.Zero;
                        DxVector3 cascadeCorner;
                        DxVector3.TransformCoordinate(ref zero, ref invCascadeMatrix, out cascadeCorner);
                        DxVector3.TransformCoordinate(ref cascadeCorner, ref ReceiverConstants.Data.ShadowMatrix,
                            out cascadeCorner);

                        var one = DxVector3.One;
                        DxVector3 otherCorner;
                        DxVector3.TransformCoordinate(ref one, ref invCascadeMatrix, out otherCorner);
                        DxVector3.TransformCoordinate(ref otherCorner, ref ReceiverConstants.Data.ShadowMatrix, out otherCorner);

                        var cascadeScale = DxVector3.One / (otherCorner - cascadeCorner);
                        ReceiverConstants.Data.CascadeOffsets[cascadeIndex] = new DxVector4(-cascadeCorner, 0.0f);
                        ReceiverConstants.Data.CascadeScales[cascadeIndex] = new DxVector4(cascadeScale, 1.0f);

                        if (RenderSettings.UseFilterableShadows)
                            ConvertToVsm(ref context, cascadeIndex,
                                ref ReceiverConstants.Data.CascadeSplits[cascadeIndex],
                                ref ReceiverConstants.Data.CascadeScales[0]);
                    }
                    PixHelper.EndEvent();
                }
            }
            PixHelper.EndEvent();

            DataStream stream;
            context.MapSubresource(ReceiverConstants.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            stream.Write(ReceiverConstants.Data.ShadowMatrix);
            stream.WriteRange(ReceiverConstants.Data.CascadeSplits, 0, 4);
            stream.WriteRange(ReceiverConstants.Data.CascadeOffsets, 0, 4);
            stream.WriteRange(ReceiverConstants.Data.CascadeScales, 0, 4);
            stream.Write(ReceiverConstants.Data.ShadowDepthBias);
            stream.Write(ReceiverConstants.Data.ShadowOffsetScale);
            stream.Write(ReceiverConstants.Data.VisualizeCascades);

            context.UnmapSubresource(ReceiverConstants.Buffer, 0);

            //ReceiverConstants.Update(ref context);
        }