コード例 #1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            ms = Mouse.GetState();

            Ray   pickRay   = Editor.heightmap.GetPickRay();
            float rayLength = 0f;

            if (Editor.camera.state == Camera.State.Fixed && layerID != -1 && currentTool != Tool.Select)
            {
                heightmap.bShowCursor = true;

                for (int i = 0; i < heightmap.triangle.Length; i++)
                {
                    Heightmap.Tri thisTri = heightmap.triangle[i];
                    if (MathExtra.Intersects(pickRay, thisTri.p1, thisTri.p3, thisTri.p2, thisTri.normal, false, true, out rayLength))
                    {
                        heightmap.testTriangle[2].SetNewCoordinates(thisTri.p1, thisTri.p2, thisTri.p3, Microsoft.Xna.Framework.Graphics.Color.White);

                        Vector3 rayTarget = pickRay.Position + pickRay.Direction * rayLength;
                        heightmap.groundCursorPosition.X = rayTarget.X / (heightmap.size.X * heightmap.cellSize.X);
                        heightmap.groundCursorPosition.Y = rayTarget.Y;
                        heightmap.groundCursorPosition.Z = rayTarget.Z / (heightmap.size.Y * heightmap.cellSize.Y);

                        Point pixel = new Point((int)(rayTarget.X / heightmap.cellSize.X), (int)(rayTarget.Z / heightmap.cellSize.Y));

                        if (rayLength > 0f)
                        {
                            if (ms.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                            {
                                switch (currentTool)
                                {
                                case Tool.Fill:
                                    heightmap.Paint(pixel.X, pixel.Y, heightmap.groundCursorStrength / 100f, layerID - 1);
                                    break;

                                case Tool.Unfill:
                                    heightmap.Paint(pixel.X, pixel.Y, -heightmap.groundCursorStrength / 100f, layerID - 1);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else if (heightmap.bShowCursor)
            {
                heightmap.bShowCursor = false;
            }
        }
コード例 #2
0
        public void Update(Vector3 position, Vector3 camPos)
        {
            if (!bVisible && alpha > 0f)
            {
                Fade(-0.03f);
            }
            else if (bVisible && alpha < 1f)
            {
                Fade(0.1f);
            }

            Vector3 viewNormal = Vector3.Normalize(position - camPos);

            rotationMatrix = MathExtra.MatrixFromNormal(viewNormal);

            world = Matrix.CreateScale(drawScale) * rotationMatrix * Matrix.CreateTranslation(position);
        }
コード例 #3
0
        //Brute force method
        private void CheckTerrainRay()
        {
            Ray sunSay = new Ray(Editor.camera.position, direction);

            bHasBlockedRay = false;

            if (Editor.heightmap != null && Editor.heightmap.triangle != null && Editor.heightmap.triangle.Length > 0)
            {
                for (int i = 0; i < Editor.heightmap.triangle.Length; i++)
                {
                    Heightmap.Tri triangle  = Editor.heightmap.triangle[i];
                    float         rayLength = 0f;
                    if (MathExtra.Intersects(sunSay, triangle.p1, triangle.p2, triangle.p3, triangle.normal, true, true, out rayLength))
                    {
                        bHasBlockedRay = true;
                        break;
                    }
                }
            }
        }
コード例 #4
0
        public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
        {
            graphicsDevice.RenderState.DepthBufferEnable = true;
            graphicsDevice.VertexDeclaration             = vertexDeclaration;

            Matrix WorldViewProj = world * view * projection;

            if (!bPlaneTransformed)
            {
                plane[0]          = MathExtra.TransformPlane(ref plane[0], ref world);
                plane[1]          = MathExtra.TransformPlane(ref plane[1], ref world);
                plane[2]          = MathExtra.TransformPlane(ref plane[2], ref world);
                plane[3]          = MathExtra.TransformPlane(ref plane[3], ref world);
                plane[4]          = MathExtra.TransformPlane(ref plane[4], ref world);
                bPlaneTransformed = true;
            }

            effect.Begin();

            effect.Parameters["WorldViewProj"].SetValue(WorldViewProj);
            effect.Parameters["color"].SetValue(color);

            effect.CommitChanges();

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                graphicsDevice.Indices = indexBuffer;
                graphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);
                graphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, vertex.Length, 0, 12);

                pass.End();
            }

            effect.End();
        }
コード例 #5
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            ms = Mouse.GetState();
            //List<int> TriangleList = new List<int>();

            int o = 0;

            Ray   pickRay   = Editor.heightmap.GetPickRay();
            float rayLength = 0f;
            float?rayLengthParent;

            //TODO: Use a QuadTree
            if (ms.MiddleButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                o += 1;
            }

            //if (heightmap.quadTree != null)
            heightmap.quadTree.boundingBox.Intersects(ref pickRay, out rayLengthParent);

            if (rayLengthParent == null)
            {
                o += 1;
            }
            else
            //if (MathExtra.Intersects(pickRay, heightmap.quadTree.BoundingCoordinates.LowerRight, heightmap.quadTree.BoundingCoordinates.UpperRight, heightmap.quadTree.BoundingCoordinates.UpperLeft, Vector3.Up, true, false, out rayLengthParent) || MathExtra.Intersects(pickRay, heightmap.quadTree.BoundingCoordinates.LowerRight, heightmap.quadTree.BoundingCoordinates.UpperLeft, heightmap.quadTree.BoundingCoordinates.LowerLeft, Vector3.Up, true, false, out rayLengthParent))
            {
                Vector3 rayTargetParent = pickRay.Position + pickRay.Direction * (float)rayLengthParent;
                if (rayLengthParent >= 0f)
                {
                    //Parse Quadtree based on rayTarget.Position
//                    List<int> TriangleList = heightmap.quadTree.GetTriangleIndexes(heightmap.quadTree.NodeList[0], rayTargetParent.X, rayTargetParent.Z);
                    List <int> TriangleList = new List <int>();
                    heightmap.quadTree.GetTriangleIndexes(heightmap.quadTree.NodeList[0], ref TriangleList, ref pickRay);

                    if (Editor.camera.state == Camera.State.Fixed)
                    {
                        //for (int i = 0; i < heightmap.triangle.Length; i++)
                        foreach (int i in TriangleList)
                        {
                            Heightmap.Tri thisTri = heightmap.triangle[i];
                            //heightmap.testTriangle[6].SetNewCoordinates(thisTri.p1, thisTri.p2, thisTri.p3, Microsoft.Xna.Framework.Graphics.Color.Pink);
                            if (MathExtra.Intersects(pickRay, thisTri.p1, thisTri.p2, thisTri.p3, thisTri.normal, false, true, out rayLength))
                            {
                                //heightmap.testTriangle[6].SetNewCoordinates(thisTri.p1, thisTri.p2, thisTri.p3, Microsoft.Xna.Framework.Graphics.Color.Red);

                                Vector3 rayTarget = pickRay.Position + pickRay.Direction * rayLength;
                                heightmap.groundCursorPosition.X = rayTarget.X / (heightmap.size.X * heightmap.cellSize.X);
                                heightmap.groundCursorPosition.Y = rayTarget.Y;
                                heightmap.groundCursorPosition.Z = rayTarget.Z / (heightmap.size.Y * heightmap.cellSize.Y);

                                if (rayLength > 0f)
                                {
                                    if (ms.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                                    {
                                        bEditing = true;

                                        switch (currentTool)
                                        {
                                        case Tool.Raise:
                                            heightmap.RiseHeight(thisTri.id);
                                            break;

                                        case Tool.Lower:
                                            heightmap.LowerHeight(thisTri.id);
                                            break;

                                        case Tool.Smooth:
                                            heightmap.Smooth(thisTri.id);
                                            break;

                                        case Tool.Flatten:
                                            if (!bHeightSet)
                                            {
                                                heightmap.flattenHeight = heightmap.groundCursorPosition.Y;
                                                bHeightSet = true;
                                            }
                                            heightmap.FlattenVertices(thisTri.id);
                                            break;
                                        }
                                    }
                                    else if (bHeightSet)
                                    {
                                        bHeightSet = false;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }



            if (bEditing && ms.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
            {
                bEditing = false;
                Editor.heightmap.CalculateNormals();
                //Game1.heightmap.CalculateLightmapNormals();

                //Game1.heightmap.GenerateNormals();
            }
        }
コード例 #6
0
 public virtual void Update()
 {
     rotation.Y     = -MathExtra.GetAngleFrom2DVectors(new Vector2(Editor.camera.position.X, Editor.camera.position.Z), new Vector2(position.X, position.Z), true);
     rotationMatrix = Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z);
     world          = Matrix.CreateScale(scale) * rotationMatrix * Matrix.CreateTranslation(position);
 }