Ejemplo n.º 1
0
 /// <summary>
 /// Extends the box to contain the supplied box.
 /// </summary>
 public static Box3 ExtendBy(this Box3 thisBox, Box3 box)
 {
     if (box.Minimum.X < thisBox.Minimum.X)
     {
         thisBox.Minimum.X = box.Minimum.X;
     }
     if (box.Maximum.X > thisBox.Maximum.X)
     {
         thisBox.Maximum.X = box.Maximum.X;
     }
     if (box.Minimum.Y < thisBox.Minimum.Y)
     {
         thisBox.Minimum.Y = box.Minimum.Y;
     }
     if (box.Maximum.Y > thisBox.Maximum.Y)
     {
         thisBox.Maximum.Y = box.Maximum.Y;
     }
     if (box.Minimum.Z < thisBox.Minimum.Z)
     {
         thisBox.Minimum.Z = box.Minimum.Z;
     }
     if (box.Maximum.Z > thisBox.Maximum.Z)
     {
         thisBox.Maximum.Z = box.Maximum.Z;
     }
     return(thisBox);
 }
Ejemplo n.º 2
0
 private void UpdateGridLines(BoundingBox bb)
 {
     GridWidth  = bb.Width + 16;
     GridLength = bb.Height + 16;
     RaisePropertyChanged("GridWidth");
     RaisePropertyChanged("GridLength");
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Extends the box to contain the supplied value.
 /// </summary>
 public static Box3 ExtendBy(this Box3 thisBox, Vector3 point)
 {
     if (point.X < thisBox.Minimum.X)
     {
         thisBox.Minimum.X = point.X;
     }
     if (point.X > thisBox.Maximum.X)
     {
         thisBox.Maximum.X = point.X;
     }
     if (point.Y < thisBox.Minimum.Y)
     {
         thisBox.Minimum.Y = point.Y;
     }
     if (point.Y > thisBox.Maximum.Y)
     {
         thisBox.Maximum.Y = point.Y;
     }
     if (point.Z < thisBox.Minimum.Z)
     {
         thisBox.Minimum.Z = point.Z;
     }
     if (point.Z > thisBox.Maximum.Z)
     {
         thisBox.Maximum.Z = point.Z;
     }
     return(thisBox);
 }
Ejemplo n.º 4
0
        private BoundingBox GetBoundingBox(Scene scene)
        {
            SharpDX.BoundingBox box = new SharpDX.BoundingBox();
            if (scene.HasMeshes)
            {
                foreach (Mesh mesh in scene.Meshes)
                {
                    SharpDX.BoundingBox meshBox = GetBoundingBox(mesh);
                    box.Minimum.X = Math.Min(box.Minimum.X, meshBox.Minimum.X);
                    box.Minimum.Y = Math.Min(box.Minimum.Y, meshBox.Minimum.Y);
                    box.Minimum.Z = Math.Min(box.Minimum.Z, meshBox.Minimum.Z);

                    box.Maximum.X = Math.Max(box.Maximum.X, meshBox.Maximum.X);
                    box.Maximum.Y = Math.Max(box.Maximum.Y, meshBox.Maximum.Y);
                    box.Maximum.Z = Math.Max(box.Maximum.Z, meshBox.Maximum.Z);
                }
            }

            BoundingBox newBox = new BoundingBox();

            newBox.Center.X  = (box.Minimum.X + box.Maximum.X) / 2.0f;
            newBox.Center.Y  = (box.Minimum.Y + box.Maximum.Y) / 2.0f;
            newBox.Center.Z  = (box.Minimum.Z + box.Maximum.Z) / 2.0f;
            newBox.Extents.X = Math.Abs(box.Maximum.X - newBox.Center.X);
            newBox.Extents.Y = Math.Abs(box.Maximum.Y - newBox.Center.Y);
            newBox.Extents.Z = Math.Abs(box.Maximum.Z - newBox.Center.Z);

            return(newBox);
        }
Ejemplo n.º 5
0
 private void ClampBoundingBox(ref BoundingBox bb)
 {
     bb.Minimum.X = Math.Max(bb.Minimum.X, -MAX_GRID_LENGTH);
     bb.Minimum.Y = Math.Max(bb.Minimum.Y, -MAX_GRID_LENGTH);
     bb.Minimum.Z = Math.Max(bb.Minimum.Z, -MAX_GRID_LENGTH);
     bb.Maximum.X = Math.Min(bb.Maximum.X, MAX_GRID_LENGTH);
     bb.Maximum.Y = Math.Min(bb.Maximum.Y, MAX_GRID_LENGTH);
     bb.Maximum.Z = Math.Min(bb.Maximum.Z, MAX_GRID_LENGTH);
 }
Ejemplo n.º 6
0
        public bool Contains(BoundingBox box)
        {
            var min      = box.Minimum.ToSharpDX() / FRUSTUM_DIVISOR;
            var max      = box.Maximum.ToSharpDX() / FRUSTUM_DIVISOR;
            var sharpBox = new SharpDX.BoundingBox(min, max);
            var contains = _frustum.Contains(ref sharpBox);

            return(contains == ContainmentType.Contains || contains == ContainmentType.Intersects);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Extends the box to contain the supplied value.
 /// </summary>
 public static Box3 ExtendZBy(this Box3 thisBox, float z)
 {
     if (z < thisBox.Minimum.Z)
     {
         thisBox.Minimum.Z = z;
     }
     if (z > thisBox.Maximum.Z)
     {
         thisBox.Maximum.Z = z;
     }
     return(thisBox);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Extends the box to contain the supplied value.
 /// </summary>
 public static Box3 ExtendXBy(this Box3 thisBox, float x)
 {
     if (x < thisBox.Minimum.X)
     {
         thisBox.Minimum.X = x;
     }
     if (x > thisBox.Maximum.X)
     {
         thisBox.Maximum.X = x;
     }
     return(thisBox);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Extends the box to contain the supplied value.
 /// </summary>
 public static Box3 ExtendYBy(this Box3 thisBox, float y)
 {
     if (y < thisBox.Minimum.Y)
     {
         thisBox.Minimum.Y = y;
     }
     if (y > thisBox.Maximum.Y)
     {
         thisBox.Maximum.Y = y;
     }
     return(thisBox);
 }
Ejemplo n.º 10
0
        public GTADetection(Entity e, DetectionType type)
        {
            Type     = type;
            Pos      = new GTAVector(e.Position);
            Distance = Game.Player.Character.Position.DistanceTo(e.Position);
            BBox     = GTAData.ComputeBoundingBox(e, e.Position);
            Handle   = e.Handle;

            Rot = new GTAVector(e.Rotation);
            cls = DetectionClass.Unknown;
            Vector3 gmin;
            Vector3 gmax;

            e.Model.GetDimensions(out gmin, out gmax);
            BBox3D = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax));
        }
Ejemplo n.º 11
0
        public bool InitializeGraphics(IRenderManager manager, Device device)
        {
            // Compute the bounding box extents.
            SharpDX.BoundingBox box = new SharpDX.BoundingBox(this.BoundMin.ToVector3(), this.BoundMax.ToVector3());

            // Allocate the vertex array.
            this.vertices = new D3DColoredVertex[8];

            // Calculate the half-widths of the box.
            float halfWidth  = box.Width / 2.0f;
            float halfHeight = box.Height / 2.0f;
            float halfDepth  = box.Depth / 2.0f;

            // Build the vertex array from the bounding box info.
            this.vertices[0].Position = new Vector3(box.Center.X - halfWidth, box.Center.Y - halfHeight, box.Center.Z + halfDepth);
            this.vertices[1].Position = new Vector3(box.Center.X + halfWidth, box.Center.Y - halfHeight, box.Center.Z + halfDepth);
            this.vertices[2].Position = new Vector3(box.Center.X + halfWidth, box.Center.Y - halfHeight, box.Center.Z - halfDepth);
            this.vertices[3].Position = new Vector3(box.Center.X - halfWidth, box.Center.Y - halfHeight, box.Center.Z - halfDepth);

            this.vertices[4].Position = new Vector3(box.Center.X - halfWidth, box.Center.Y + halfHeight, box.Center.Z + halfDepth);
            this.vertices[5].Position = new Vector3(box.Center.X + halfWidth, box.Center.Y + halfHeight, box.Center.Z + halfDepth);
            this.vertices[6].Position = new Vector3(box.Center.X + halfWidth, box.Center.Y + halfHeight, box.Center.Z - halfDepth);
            this.vertices[7].Position = new Vector3(box.Center.X - halfWidth, box.Center.Y + halfHeight, box.Center.Z - halfDepth);

            // Setup the vertex buffer using the vertex data stream.
            BufferDescription desc = new BufferDescription();

            desc.BindFlags           = BindFlags.VertexBuffer;
            desc.CpuAccessFlags      = CpuAccessFlags.Read | CpuAccessFlags.Write;
            desc.StructureByteStride = 28;
            desc.Usage        = ResourceUsage.Default;
            this.vertexBuffer = SharpDX.Direct3D11.Buffer.Create <D3DColoredVertex>(device, BindFlags.VertexBuffer, this.vertices);

            // Setup the index buffer using the indice data.
            desc                     = new BufferDescription();
            desc.BindFlags           = BindFlags.IndexBuffer;
            desc.CpuAccessFlags      = CpuAccessFlags.Read | CpuAccessFlags.Write;
            desc.StructureByteStride = 4;
            desc.Usage               = ResourceUsage.Default;
            this.indexBuffer         = SharpDX.Direct3D11.Buffer.Create <int>(device, BindFlags.IndexBuffer, this.BoxIndices);

            // Get the wireframe shader.
            this.shader = manager.GetBuiltInShader(BuiltInShaderType.Wireframe);

            return(true);
        }
Ejemplo n.º 12
0
        private SharpDX.BoundingBox GetBoundingBox(Mesh sceneMesh)
        {
            SharpDX.BoundingBox box = new SharpDX.BoundingBox();
            if (sceneMesh.HasVertices)
            {
                foreach (Vector3D v in sceneMesh.Vertices)
                {
                    box.Minimum.X = Math.Min(v.X, box.Minimum.X);
                    box.Minimum.Y = Math.Min(v.Y, box.Minimum.Y);
                    box.Minimum.Z = Math.Min(v.Z, box.Minimum.Z);

                    box.Maximum.X = Math.Max(v.X, box.Maximum.X);
                    box.Maximum.Y = Math.Max(v.Y, box.Maximum.Y);
                    box.Maximum.Z = Math.Max(v.Z, box.Maximum.Z);
                }
            }
            return(box);
        }
Ejemplo n.º 13
0
        public void SetRenderMeshes(params Mesh[] meshes)
        {
            Models.Children.Clear();
            bool        firstMesh = true;
            BoundingBox bb        = new BoundingBox();

            _centerOfMass     = new Point3D();
            _centerOfGeometry = new Point3D();
            foreach (var mesh in meshes)
            {
                var bakedMesh = ResourceCache.GetModel(mesh);
                Models.Children.Add(bakedMesh.Mesh);
                _centerOfMass = Point3D.Add(_centerOfMass, bakedMesh.CenterOfMass.ToVector3D());
                if (firstMesh)
                {
                    bb        = bakedMesh.BoundingBox;
                    firstMesh = false;
                }
                else
                {
                    bb = BoundingBox.Merge(bb, bakedMesh.BoundingBox);
                }
                //bb = BoundingBox.Merge(bb, mesh.BoundingBox.ToSharpDxBoundingBox());
            }

            if (meshes.Length > 0)
            {
                _centerOfMass.X /= meshes.Length;
                _centerOfMass.Y /= meshes.Length;
                _centerOfMass.Z /= meshes.Length;
            }

            ClampBoundingBox(ref bb);
            _modelBoundingBox = bb;
            _centerOfGeometry = _modelBoundingBox.Center.ToPoint3D();
            ReferenceScale    = CalculateReferenceScale(_modelBoundingBox);
            //UpdateGridLines(bb);

            RaisePropertyChanged("Models");
            RefocusCenter();
            RaisePropertyChanged("ReferenceScale");
        }
Ejemplo n.º 14
0
        public bool DrawFrame(IRenderManager manager, Device device)
        {
            // Compute the bounding box extents.
            SharpDX.BoundingBox box = new SharpDX.BoundingBox(this.BoundMin.ToVector3(), this.BoundMax.ToVector3());

            // Compute the transformation matrix that will be used to transform the box vertices.
            Matrix transMatrix = Matrix.Scaling(box.Width, box.Height, box.Depth);

            transMatrix.Row4 = new Vector4(box.Center, transMatrix.Row4.W);

            // Loop and transform the vertices.
            for (int i = 0; i < this.vertices.Length; i++)
            {
                // Transform the current vertex.
                //this.vertices[i].Position = Vector4.Transform(this.BoxVertices[i], transMatrix).ToVector3();
                this.vertices[i].Color = this.Color;
            }

            // Update the vertex buffer.
            device.ImmediateContext.UpdateSubresource(this.vertices, this.vertexBuffer);

            // Set the primitive type to line list.
            device.ImmediateContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList;

            // Set the vertex and index buffers.
            device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.vertexBuffer, 28, 0));
            device.ImmediateContext.InputAssembler.SetIndexBuffer(this.indexBuffer, SharpDX.DXGI.Format.R32_UInt, 0);

            // Setup the wireframe shader.
            this.shader.DrawFrame(manager, device);

            // Draw the cube.
            device.ImmediateContext.DrawIndexed(24, 0, 0);

            return(true);
        }
Ejemplo n.º 15
0
 public AxisAlignedBoundingBox(Vector3 min, Vector3 max)
 {
     this.sbb = new SharpDX.BoundingBox(min, max);
 }
Ejemplo n.º 16
0
 private double CalculateReferenceScale(BoundingBox bb)
 {
     return(Math.Max(bb.Size.Length(), 0.001));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.BoundingBox"/>.
 /// </summary>
 /// <param name="box">The box to test.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref BoundingBox box)
 {
     return(Collision.BoxIntersectsSphere(ref box, ref this));
 }
Ejemplo n.º 18
0
        public static GTABoundingBox2 ComputeBoundingBox(Entity e, Vector3 offset, float scale = 0.5f)
        {
            var m  = e.Model;
            var rv = new GTABoundingBox2
            {
                Min = new GTAVector2(float.PositiveInfinity, float.PositiveInfinity),
                Max = new GTAVector2(float.NegativeInfinity, float.NegativeInfinity)
            };
            Vector3 gmin;
            Vector3 gmax;

            m.GetDimensions(out gmin, out gmax);
            var bbox = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax));
            //Console.WriteLine(bbox.GetCorners()[0]);

            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i) {
             *  for (int j = 0; j < bbox.GetCorners().Length; ++j) {
             *      if (j == i) continue;
             *      var c1 = bbox.GetCorners()[i];
             *      var c2 = bbox.GetCorners()[j];
             *      HashFunctions.Draw3DLine(e.GetOffsetInWorldCoords(new Vector3(c1.X, c1.Y, c1.Z)), e.GetOffsetInWorldCoords(new Vector3(c2.X, c2.Y, c2.Z)), 0,0);
             *  }
             * }
             */
            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i)
             * {
             *  var corner = bbox.GetCorners()[i];
             *  var cornerinworld = e.GetOffsetInWorldCoords(new Vector3(corner.X, corner.Y, corner.Z));
             *
             *
             * }*/
            //UI.Notify(e.HeightAboveGround.ToString());
            var sp = HashFunctions.Convert3dTo2d(e.GetOffsetInWorldCoords(e.Position));

            foreach (var corner in bbox.GetCorners())
            {
                var c = new Vector3(corner.X, corner.Y, corner.Z);

                c = e.GetOffsetInWorldCoords(c);
                var s = HashFunctions.Convert3dTo2d(c);
                if (s.X == -1f || s.Y == -1f)
                {
                    rv.Min.X = float.PositiveInfinity;
                    rv.Max.X = float.NegativeInfinity;
                    rv.Min.Y = float.PositiveInfinity;
                    rv.Max.Y = float.NegativeInfinity;
                    return(rv);
                }

                /*
                 * if(s.X == -1) {
                 *  if (sp.X < 0.5) s.X = 0f;
                 *  if (sp.X >= 0.5) s.X = 1f;
                 * }
                 * if(s.Y == -1) {
                 *  if (sp.Y < 0.5) s.Y = 0f;
                 *  if (sp.Y >= 0.5) s.Y = 1f;
                 * }
                 */
                rv.Min.X = Math.Min(rv.Min.X, s.X);
                rv.Min.Y = Math.Min(rv.Min.Y, s.Y);
                rv.Max.X = Math.Max(rv.Max.X, s.X);
                rv.Max.Y = Math.Max(rv.Max.Y, s.Y);
            }

//            int width = 1280;
//            int height = 960;
//            int x = (int)(rv.Min.X * width);
//            int y = (int)(rv.Min.Y * height);
//            int x2 = (int)(rv.Max.X * width);
//            int y2 = (int)(rv.Max.Y * height);
//            float w = rv.Max.X - rv.Min.X;
//            float h = rv.Max.Y - rv.Min.Y;
//            HashFunctions.DrawRect(rv.Min.X + w/2, rv.Min.Y + h/2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y, 255, 255, 255, 100);
//            new UIRectangle(new Point((int)(rv.Min.X * 1920), (int)(rv.Min.Y * 1080)), rv.)
            return(rv);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Determines whether the current objects contains a <see cref="SharpDX.BoundingBox"/>.
 /// </summary>
 /// <param name="box">The box to test.</param>
 /// <returns>The type of containment the two objects have.</returns>
 public ContainmentType Contains(ref BoundingBox box)
 {
     return(Collision.SphereContainsBox(ref this, ref box));
 }
Ejemplo n.º 20
0
 public AxisAlignedBoundingBox(SharpDX.BoundingBox bb)
 {
     this.sbb = bb;
 }
Ejemplo n.º 21
0
        public static GTABoundingBox2 ComputeBoundingBox(Entity e, Vector3 offset, float scale = 0.5f)
        {
            var m  = e.Model;
            var rv = new GTABoundingBox2
            {
                Min = new GTAVector2(float.PositiveInfinity, float.PositiveInfinity),
                Max = new GTAVector2(float.NegativeInfinity, float.NegativeInfinity)
            };
            Vector3 gmin;
            Vector3 gmax;

            m.GetDimensions(out gmin, out gmax);
            var bbox = new SharpDX.BoundingBox((SharpDX.Vector3) new GTAVector(gmin), (SharpDX.Vector3) new GTAVector(gmax));
            //Console.WriteLine(bbox.GetCorners()[0]);

            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i) {
             *  for (int j = 0; j < bbox.GetCorners().Length; ++j) {
             *      if (j == i) continue;
             *      var c1 = bbox.GetCorners()[i];
             *      var c2 = bbox.GetCorners()[j];
             *      HashFunctions.Draw3DLine(e.GetOffsetInWorldCoords(new Vector3(c1.X, c1.Y, c1.Z)), e.GetOffsetInWorldCoords(new Vector3(c2.X, c2.Y, c2.Z)), 0,0);
             *  }
             * }
             */
            /*
             * for (int i = 0; i < bbox.GetCorners().Length; ++i)
             * {
             *  var corner = bbox.GetCorners()[i];
             *  var cornerinworld = e.GetOffsetInWorldCoords(new Vector3(corner.X, corner.Y, corner.Z));
             *
             *
             * }*/
            //UI.Notify(e.HeightAboveGround.ToString());
            var sp = HashFunctions.Convert3dTo2d(e.GetOffsetInWorldCoords(e.Position));

            foreach (var corner in bbox.GetCorners())
            {
                var c = new Vector3(corner.X, corner.Y, corner.Z);

                c = e.GetOffsetInWorldCoords(c);
                var s = HashFunctions.Convert3dTo2d(c);
                if (s.X == -1f || s.Y == -1f)
                {
                    rv.Min.X = float.PositiveInfinity;
                    rv.Max.X = float.NegativeInfinity;
                    rv.Min.Y = float.PositiveInfinity;
                    rv.Max.Y = float.NegativeInfinity;
                    return(rv);
                }

                /*
                 * if(s.X == -1) {
                 *  if (sp.X < 0.5) s.X = 0f;
                 *  if (sp.X >= 0.5) s.X = 1f;
                 * }
                 * if(s.Y == -1) {
                 *  if (sp.Y < 0.5) s.Y = 0f;
                 *  if (sp.Y >= 0.5) s.Y = 1f;
                 * }
                 */
                rv.Min.X = Math.Min(rv.Min.X, s.X);
                rv.Min.Y = Math.Min(rv.Min.Y, s.Y);
                rv.Max.X = Math.Max(rv.Max.X, s.X);
                rv.Max.Y = Math.Max(rv.Max.Y, s.Y);
            }

            float w = rv.Max.X - rv.Min.X;
            float h = rv.Max.Y - rv.Min.Y;

//            just for debug purposes, show visible and not visible entities in other color
//            if (CheckVisible(e))
//            {
//                HashFunctions.DrawRect(rv.Min.X + w / 2, rv.Min.Y + h / 2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y,
//                    Color.White, 100);
//            }
//            else
//            {
//                HashFunctions.DrawRect(rv.Min.X + w / 2, rv.Min.Y + h / 2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y,
//                    Color.Red, 100);
//            }
//            HashFunctions.DrawRect(rv.Min.X + w/2, rv.Min.Y + h/2, rv.Max.X - rv.Min.X, rv.Max.Y - rv.Min.Y, 255, 255, 255, 100);
            return(rv);
        }
Ejemplo n.º 22
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.BoundingBox"/>.
 /// </summary>
 /// <param name="box">The box to test.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(BoundingBox box)
 {
     return(Intersects(ref box));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.BoundingBox"/>.
 /// </summary>
 /// <param name="box">The box to test.</param>
 /// <param name="distance">When the method completes, contains the distance of the intersection,
 /// or 0 if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref BoundingBox box, out float distance)
 {
     return(Collision.RayIntersectsBox(ref this, ref box, out distance));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.BoundingBox"/>.
 /// </summary>
 /// <param name="box">The box 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 BoundingBox box, out Vector3 point)
 {
     return(Collision.RayIntersectsBox(ref this, ref box, out point));
 }
Ejemplo n.º 25
0
 public AxisAlignedBoundingBox(SharpDX.BoundingBox bb)
 {
     this.sbb = bb;
 }