예제 #1
0
        public bool IsPlanePicked(int mouseX, int mouseY)
        {
            // Construct our ray
            Ray ray = Utilitys.CalculateRayFromCursor(Game.GraphicsDevice, Camera, mouseX, mouseY);

            Vector3 min = new Vector3(Vertices[0].Position.X, Vertices[0].Position.Y, Vertices[0].Position.Z);
            Vector3 max = new Vector3(Vertices[Vertices.Length - 1].Position.X,
                                      Vertices[Vertices.Length - 1].Position.Y,
                                      Vertices[Vertices.Length - 1].Position.Z);

            BoundingBox bb = new BoundingBox(min, max);

            if (bb.Intersects(ray) > 0)
            {
                // TODO: This needs reworking because we should almost segregate the difference between a Vertice Picking operation and the whole Object Picking operation!

                // Invoke our delegate so we can manipulate this from outside our class
                if (PickingOperation != null)
                {
                    PickingOperation.Invoke(0);
                }
            }

            return(false);
        }
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            MouseState state = Mouse.GetState();

            Ray ray = Utilitys.CalculateRayFromCursor(GraphicsDevice, Camera, state.X, state.Y);

            CursorPosition = new Vector3(ray.Position.X, ray.Position.Y, ray.Position.Z);

            Status.Text = "Triangles : " + Landscape.TotalVisibleTriangles;

            if (InputHandler.IsActionPressed("Exit"))
            {
                Exit();
            }

            //if (InputHandler.IsActionPressed("Forward"))
            //{
            //  Camera.MoveForward(4f);
            //}

            //if (InputHandler.IsActionPressed("Backward"))
            //{
            //  Camera.MoveBackward(4f);
            //}

            //if (InputHandler.IsActionPressed("Left"))
            //{
            //  Camera.StrafeLeft(4f);
            //}

            //if (InputHandler.IsActionPressed("Right"))
            //{
            //  Camera.StrafeRight(4f);
            //}

            if (InputHandler.IsActionPressed("Down"))
            {
                Camera.Position = Vector3.Transform(Camera.Position, Matrix.CreateTranslation(0, -2f, 0));
            }

            if (InputHandler.IsActionPressed("Up"))
            {
                Camera.Position = Vector3.Transform(Camera.Position, Matrix.CreateTranslation(0, 2f, 0));
            }

            if (InputHandler.IsActionPressed("F1"))
            {
                Landscape.CustomEffect.CurrentTechnique = Landscape.CustomEffect.Techniques["RenderSolid"];
            }

            if (InputHandler.IsActionPressed("F2"))
            {
                Landscape.CustomEffect.CurrentTechnique = Landscape.CustomEffect.Techniques["RenderWireframe"];
            }
        }
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (InputHandler.IsActionPressed("Exit"))
            {
                Exit();
            }

            MouseState state = Mouse.GetState();
            Ray        ray   = Utilitys.CalculateRayFromCursor(GraphicsDevice, Camera, state.X, state.Y);

            for (int x = 0; x < Landscape.Blocks.Length; x++)
            {
                for (int y = 0; y < Landscape.Blocks[x].Length; y++)
                {
                    if (Landscape.Blocks[x][y].Intersects(ray))
                    {
                        for (int v = 0; v < Landscape.Blocks[x][y].Verts.Length; v++)
                        {
                            // Get the position of the vertice
                            Vector3 pos = Landscape.Blocks[x][y].Verts[v].Position;

                            // Create a bounding box around the vertice padded with 1 unit in each direction
                            BoundingBox box = new BoundingBox(new Vector3(pos.X - 1, pos.Y - 1, pos.Z - 1),
                                                              new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1));

                            // Check to see if the
                            if (ray.Intersects(box) != null)
                            {
                                if (state.LeftButton == ButtonState.Pressed)
                                {
                                    RaiseVertices(box, Landscape.Blocks[x][y].Verts, BrushSize, BrushSize);

                                    // Dispose the VB or memory will leak all over the place!
                                    Landscape.Blocks[x][y].VertexBuffer.Dispose();

                                    // Create the new VB
                                    Landscape.Blocks[x][y].VertexBuffer = new VertexBuffer(GraphicsDevice, Landscape.Blocks[x][y].Declaration,
                                                                                           Landscape.Blocks[x][y].Verts.Length, BufferUsage.None);

                                    Landscape.Blocks[x][y].VertexBuffer.SetData(Landscape.Blocks[x][y].Verts);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        public bool IsVertPicked(int mouseX, int mouseY)
        {
            // Construct our ray
            Ray ray = Utilitys.CalculateRayFromCursor(Game.GraphicsDevice, Camera, mouseX, mouseY);

            // Calculate our Vertice Array
            for (int x = 0; x < _width; x++)
            {
                for (int z = 0; z < _height; z++)
                {
                    // Calc our Bounding Boxes
                    Vector3 min = new Vector3(Vertices[x + z * _width].Position.X,
                                              Vertices[x + z * _width].Position.Y,
                                              Vertices[x + z * _width].Position.Z);

                    Vector3 max = new Vector3(Vertices[x + z * _width].Position.X + 1,
                                              Vertices[x + z * _width].Position.Y + 1,
                                              Vertices[x + z * _width].Position.Z + 1);

                    BoundingBox bb = new BoundingBox(min, max);

                    // If we found a hit return it
                    if (bb.Intersects(ray) > 0)
                    {
                        // Invoke our delegate so we can manipulate this from outside our class
                        if (PickingOperation != null)
                        {
                            PickingOperation.Invoke(x + z * _width);
                        }

                        // Rebuild our vert array
                        VertexBuffer = new VertexBuffer(Game.GraphicsDevice, typeof(VertexPositionColorTexture), _width * _height, BufferUsage.WriteOnly);
                        VertexBuffer.SetData(Vertices);

                        return(true);
                    }
                }
            }

            return(false);
        }