Esempio n. 1
0
        private void add_normal_to_vertices(List <VPCN> vertices, int indexA, int indexB, int indexC)
        {
            Vector3 normal =
                Vector3.Cross(vertices[indexB].Position -
                              vertices[indexA].Position,
                              vertices[indexC].Position -
                              vertices[indexA].Position);

            normal.Normalize();

            vertices[indexA] = new VPCN(vertices[indexA], normal);
            vertices[indexB] = new VPCN(vertices[indexB], normal);
            vertices[indexC] = new VPCN(vertices[indexC], normal);
        }
Esempio n. 2
0
        /// <summary>
        /// Make the terrain in the shape of a circle.
        /// </summary>
        public void make_it_circle()
        {
            float   radius = Width / 2;
            Vector3 center = new Vector3(Width, 0, Height) / 2;

            List <int>  nIndices  = new List <int>();
            List <VPCN> nVertices = new List <VPCN>();

            nIndices.AddRange(indices);
            nVertices.AddRange(vertices);

            for (int i = nIndices.Count - 1; i >= 0; i -= 3)
            {
                Vector3 a = get_xz_vector_from_index(i);
                Vector3 b = get_xz_vector_from_index(i - 1);
                Vector3 c = get_xz_vector_from_index(i - 2);

                if ((a - center).Length() > radius ||
                    (b - center).Length() > radius ||
                    (c - center).Length() > radius)
                {
                    nIndices.RemoveAt(i);
                    nIndices.RemoveAt(i - 1);
                    nIndices.RemoveAt(i - 2);
                }
            }

            for (int i = 0; i < nVertices.Count; i++)
            {
                VPCN    v     = nVertices[i];
                Vector3 xzPos = v.Position * (Vector3.UnitX + Vector3.UnitZ);

                xzPos += Vector3.Normalize(xzPos - center) * (10 +
                                                              (float)S.rnd.NextDouble() * 4);

                xzPos.Y -= 5;

                float distFromCenter = (xzPos - center).Length();

                if (distFromCenter > radius - 1.5f)
                {
                    nVertices[i] = new VPCN(xzPos, v.Color, v.Normal);
                }
            }

            indices  = nIndices.ToArray();
            vertices = nVertices.ToArray();

            init_buffers();
        }
Esempio n. 3
0
        public void apply_color_texs(params Texture2D[] texs)
        {
            this.frames = texs.Length;

            List <VPCN[]> frames = new List <VPCN[]>();

            for (int i = 0; i < texs.Length; i++)
            {
                VPCN[] vs = new VPCN[vertices.Length];
                vertices.CopyTo(vs, 0);

                apply_color_tex(texs[i], vs);

                frames.Add(vs);
            }

            vertices = new VPCN[this.frames * vertices.Length];

            for (int i = 0; i < frames.Count; i++)
            {
                frames[i].CopyTo(vertices, frames[i].Length * i);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Used to get the plane in a certain XZ coordinate on the terrain.
        /// </summary>
        /// <param name="pos">The XZ coordinate.</param>
        /// <param name="whichTriangle">Which triangle in the rectangle was used.</param>
        /// <returns>The plane.</returns>
        private Plane get_plane(Vector2 pos, out bool whichTriangle, bool considerDelta)
        {
            //here vector2 represents X Z coordinates (not X Y)
            int ix = (int)pos.X;
            int iz = (int)pos.Y;

            if (ix + 1 + (iz + 1) * Width > Width * Height)
            // if out of bounds, returns default plane.
            {
                whichTriangle = true;
                return(new Plane(Vector3.UnitY, 0));
            }

            VPCN topLeft     = vertices[ix + iz * Width];
            VPCN bottomRight = vertices[ix + 1 + (iz + 1) * Width];

            Vector2 tlxz = new Vector2(topLeft.Position.X,
                                       topLeft.Position.Z);
            Vector2 brxz = new Vector2(bottomRight.Position.X,
                                       bottomRight.Position.Z);

            Plane p;

            if ((pos - tlxz).Length() < (pos - brxz).Length())
            {
                if (considerDelta && ix + 1 < Width && iz + 1 < Height)
                {
                    p = new Plane(
                        vertices[ix + iz * Width].Position + Vector3.UnitY * deltaHeight[ix, iz],
                        vertices[ix + 1 + iz * Width].Position + Vector3.UnitY * deltaHeight[ix + 1, iz],
                        vertices[ix + (iz + 1) * Width].Position + Vector3.UnitY * deltaHeight[ix, iz + 1]);
                }
                else
                {
                    p = new Plane(
                        vertices[ix + iz * Width].Position,
                        vertices[ix + 1 + iz * Width].Position,
                        vertices[ix + (iz + 1) * Width].Position);
                }

                whichTriangle = true;
            }
            else
            {
                if (considerDelta && ix + 1 < Width && iz + 1 < Height)
                {
                    p = new Plane(
                        vertices[ix + (iz + 1) * Width].Position + Vector3.UnitY * deltaHeight[ix, iz],
                        vertices[ix + 1 + iz * Width].Position + Vector3.UnitY * deltaHeight[ix, iz],
                        vertices[ix + 1 + (iz + 1) * Width].Position + Vector3.UnitY * deltaHeight[ix, iz]);
                }
                else
                {
                    p = new Plane(
                        vertices[ix + (iz + 1) * Width].Position,
                        vertices[ix + 1 + iz * Width].Position,
                        vertices[ix + 1 + (iz + 1) * Width].Position);
                }

                whichTriangle = false;
            }
            return(p);
        }