コード例 #1
0
        /// <summary>
        /// Generate a flat plane.
        /// </summary>
        /// <param name="size">Size of the plane.</param>
        /// <param name="vertexCount">Number of vertex per side.</param>
        /// <param name="textureRepeate">Number of texture repeate.</param>
        /// <returns>A flat plane.</returns>
        public static Model GeneratePlane(float size, int vertexCount, int textureRepeate, IHeightMap heightMap)
        {
            int count = vertexCount * vertexCount;

            Vertex[] vertecies = new Vertex[count];
            int[]    indices   = new int[6 * (vertexCount - 1) * (vertexCount - 1)];

            int vertexPointer = 0;

            for (int y = 0; y < vertexCount; y++)
            {
                for (int x = 0; x < vertexCount; x++)
                {
                    Vector2 vertexPosition = new Vector2((size / (vertexCount - 1)) * x, (size / (vertexCount - 1)) * y);
                    vertecies[vertexPointer] = new Vertex(new Vector3(vertexPosition.X, heightMap.GetHeight(x / (float)vertexCount, y / (float)vertexCount), vertexPosition.Y),
                                                          heightMap.GetNormal(x / (float)vertexCount, y / (float)vertexCount),
                                                          new Point2D(x / ((float)vertexCount - 1) * textureRepeate, (float)y / ((float)vertexCount - 1) * textureRepeate));
                    vertexPointer++;
                }
            }
            int pointer = 0;

            for (int y = 0; y < vertexCount - 1; y++)
            {
                for (int x = 0; x < vertexCount - 1; x++)
                {
                    int topLeft     = (y * vertexCount) + x;
                    int topRight    = topLeft + 1;
                    int bottomLeft  = ((y + 1) * vertexCount) + x;
                    int bottomRight = bottomLeft + 1;
                    indices[pointer++] = topLeft;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = topRight;
                    indices[pointer++] = topRight;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = bottomRight;
                }
            }

            return(new Model(vertecies, indices));
        }
コード例 #2
0
        public static Mesh3V3N CreateFromHeightMap(int columns, int rows, IHeightMap heightMap)
        {
            var vertices = new List <Vertex3V3N>();

            for (var x = 0; x <= columns; x++)
            {
                for (var y = 0; y <= rows; y++)
                {
                    var height = heightMap.GetHeight(x, y);
                    vertices.Add(new Vertex3V3N
                    {
                        Position = new Vector3(x, (float)height, y),
                        Normal   = (Vector3)heightMap.GetNormal(x, y)
                    });
                }
            }

            var faces = CreateFaces(columns, rows);

            return(new Mesh3V3N(vertices, faces).Transformed(Matrix4.CreateTranslation(-columns / 2f, 0, -rows / 2f) * Matrix4.CreateScale((float)(1.0 / columns), 1, (float)(1.0 / rows))));
        }
コード例 #3
0
        public static Mesh3V3N CreateFromHeightMap(int columns, int rows, IHeightMap heightMap)
        {
            var vertices = new List<Vertex3V3N>();
            for (var x = 0; x <= columns; x++)
            {
                for (var y = 0; y <= rows; y++)
                {
                    var height = heightMap.GetHeight(x, y);
                    vertices.Add(new Vertex3V3N
                    {
                        Position = new Vector3(x, (float)height, y),
                        Normal = (Vector3)heightMap.GetNormal(x, y)
                    });
                }
            }

            var faces = new List<Face3>();
            for (var x = 0; x < columns; x++)
            {
                for (var y = 0; y < rows; y++)
                {
                    var verticesInColumn = rows + 1;
                    var v0 = x * verticesInColumn + y;
                    var v1 = (x + 1) * verticesInColumn + y;
                    var v2 = (x + 1) * verticesInColumn + y + 1;
                    var v3 = x * verticesInColumn + y + 1;

                    Face3 f0;
                    Face3 f1;
                    if (y % 2 == 0)
                    {
                        if (x % 2 == 0)
                        {
                            f0 = new Face3 { V0 = v0, V1 = v1, V2 = v2 };
                            f1 = new Face3 { V0 = v0, V1 = v2, V2 = v3 };
                        }
                        else
                        {
                            f0 = new Face3 { V0 = v0, V1 = v1, V2 = v3 };
                            f1 = new Face3 { V0 = v1, V1 = v2, V2 = v3 };
                        }
                    }
                    else
                    {
                        if (x % 2 == 0)
                        {
                            f0 = new Face3 { V0 = v0, V1 = v1, V2 = v3 };
                            f1 = new Face3 { V0 = v1, V1 = v2, V2 = v3 };
                        }
                        else
                        {
                            f0 = new Face3 { V0 = v0, V1 = v1, V2 = v2 };
                            f1 = new Face3 { V0 = v0, V1 = v2, V2 = v3 };
                        }
                    }

                    faces.Add(f0);
                    faces.Add(f1);
                }
            }

            return new Mesh3V3N(vertices, faces).Transformed(Matrix4.CreateTranslation(-columns / 2, 0, -rows / 2) * Matrix4.CreateScale((float)(1.0 / columns), 1, (float)(1.0 / rows)));
        }