Пример #1
0
        /// <summary>
        /// Set colors on points on the plane
        /// </summary>
        private void SetColors(PrimitivePlane plane)
        {
            //fUpdateColors = false;

            int max = plane.Segments;

            Color color1 = Color.SkyBlue;
            Color color2 = Color.DeepSkyBlue;

            bool useColor1 = true;

            for (int x = 0; x <= max; x++)
            {
                Color color;

                for (int z = 0; z <= max; z++)
                {
                    VertexPositionNormalColor p1 = plane.GetPoint(x, z);
                    if (useColor1)
                    {
                        color = color1;
                    }
                    else
                    {
                        color = color2;
                    }
                    if (useColor1)
                    {
                        color.R = (byte)(255 * z / max);
                    }
                    useColor1 = !useColor1;


                    p1.Color = color;
                    plane.SetPoint(x, z, p1);
                }
                useColor1 = !useColor1;
            }
        }
Пример #2
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (this.Manager.KeyPressed(Keys.M))
            {
                switch (fDrawMode)
                {
                case DrawMode.Grid:
                    CurrentDrawMode = DrawMode.Solid;
                    break;

                case DrawMode.Solid:
                    CurrentDrawMode = DrawMode.WireFrame;
                    break;

                case DrawMode.WireFrame:
                    CurrentDrawMode = DrawMode.Grid;
                    break;
                }
            }
            else if (this.Manager.KeyPressed(Keys.C))
            {
                switch (fColorMode)
                {
                case ColorMode.Points:
                    CurrentColorMode = ColorMode.SkyBlue;
                    break;

                case ColorMode.SkyBlue:
                    CurrentColorMode = ColorMode.Points;
                    break;
                }
            }
            else if (this.Manager.KeyPressed(Keys.N))
            {
                switch (fNormalMode)
                {
                case NormalMode.Crazy:
                    CurrentNormalMode = NormalMode.Standard;
                    break;

                case NormalMode.Standard:
                    CurrentNormalMode = NormalMode.Crazy;
                    break;
                }
            }

            fCamera.Update(gameTime);

            double seconds = gameTime.TotalGameTime.TotalSeconds;

            if (!fHalted)
            {
                fUpdateDiagram = true;
            }

            fUpdateDiagram = fUpdateDiagram || fUpdatePlane;
            fUpdateColors  = fUpdateColors || fUpdateDiagram;
            fUpdateNormals = fUpdateNormals || fUpdateDiagram;

            if (fUpdatePlane)
            {
                SetPlane();
            }
            int max = fPlane.Segments;

            if (fUpdateDiagram)
            {
                fUpdateDiagram = false;
                for (int x = 0; x <= max; x++)
                {
                    float nX = 2.0f * (float)x / max - 1.0f;// -1 to +1
                    for (int z = 0; z <= max; z++)
                    {
                        float  nZ = 2.0f * (float)z / max - 1.0f;
                        double r  = Math.Sqrt(nX * nX + nZ * nZ);
                        // function
                        double y = nX * Math.Cos(r * 7.0f - seconds * 4.2f) +
                                   nZ * Math.Sin((r - 0.5) * 10.0f - seconds * 1.9f);

                        float height = 200.0f * (float)y;
                        fPlane.SetHeight(x, z, height);
                    }
                }
            }
            if (fUpdateColors)
            {
                SetColors();
            }
            if (fUpdateNormals)
            {
                switch (fNormalMode)
                {
                case NormalMode.Crazy:
                    for (int x = 0; x <= max; x++)
                    {
                        float nX = 2.0f * (float)x / max - 1.0f;    // -1 to +1
                        for (int z = 0; z <= max; z++)
                        {
                            float nZ = 2.0f * (float)z / max - 1.0f;
                            // function
                            double r = Math.Sqrt(nX * nX + nZ * nZ);
                            VertexPositionNormalColor p = fPlane.GetPoint(x, z);
                            p.Normal = Vector3.Normalize(new Vector3(
                                                             Convert.ToSingle(Math.Cos(r * 1.0f + 0.5f - seconds * 0.1f)),
                                                             Convert.ToSingle(Math.Cos(r * 2.0f + 2.1f - seconds * 0.2f)),
                                                             Convert.ToSingle(Math.Cos(r * 5.1f * seconds))));
                            fPlane.SetPoint(x, z, p);
                        }
                    }
                    break;

                case NormalMode.Standard:
                    fPlane.GenerateNormals();
                    break;
                }
            }

            base.Update(gameTime);
        }