Beispiel #1
0
        private bool InitializeBuffers(Device device)
        {
            try
            {
                // Calculate the number of the vertices in the terrain mesh.
                VertexCount = (TerrainWidth - 1) * (TerrainHeight - 1) * 8;
                // Set the index count to the same as the vertex count.
                IndexCount = VertexCount;

                // Create the vertex array.
                var vertices = new ColorShader.Vertex[VertexCount];
                // Create the index array.
                var indices = new int[IndexCount];
                var index = 0;

                for (var j = 0; j < TerrainHeight - 1; j++)
                {
                    for (var i = 0; i < TerrainWidth - 1; i++)
                    {
                        // LINE 1
                        // Upper left
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i, 0, j + 1),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // Upper right
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i + 1, 0, j + 1),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // LINE 2
                        // Upper right
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i + 1, 0, j + 1),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // Bottom right
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i + 1, 0, j),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // LINE 3
                        // Bottom right
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i + 1, 0, j),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // Bottom left
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i, 0, j),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // LINE 4
                        // Bottom left
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i, 0, j),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;

                        // Upper left
                        vertices[index] = new ColorShader.Vertex()
                        {
                            position = new Vector3(i, 0, j + 1),
                            color = new Vector4(1, 1, 1, 1)
                        };
                        indices[index] = index++;
                    }
                }

                // Create the vertex buffer.
                VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertices);

                // Create the index buffer.
                IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices);

                return true;
            }
            catch
            {
                return false;
            }
        }
Beispiel #2
0
        protected virtual void FillArrays(out ColorShader.Vertex[] vertices, out int[] indices)
        {
            // Create the vertex array.
            vertices = new ColorShader.Vertex[VertexCount];
            // Create the index array.
            indices = new int[IndexCount];
            var index = 0;

            for (var j = 0; j < TerrainHeight - 1; j++)
            {
                for (var i = 0; i < TerrainWidth - 1; i++)
                {
                    // LINE 1
                    // Upper left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i, 0, j + 1),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i + 1, 0, j + 1),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // LINE 2
                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i + 1, 0, j + 1),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // Bottom right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i + 1, 0, j),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // LINE 3
                    // Bottom right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i + 1, 0, j),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // Bottom left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i, 0, j),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // LINE 4
                    // Bottom left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i, 0, j),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;

                    // Upper left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = new Vector3(i, 0, j + 1),
                        color = new Vector4(1, 1, 1, 1)
                    };
                    indices[index] = index++;
                }
            }
        }
        protected override void FillArrays(out ColorShader.Vertex[] vertices, out int[] indices)
        {
            // Create the vertex array.
            vertices = new ColorShader.Vertex[VertexCount];
            // Create the index array.
            indices = new int[IndexCount];
            var index = 0;

            for (var j = 0; j < TerrainHeight - 1; j++)
            {
                for (var i = 0; i < TerrainWidth - 1; i++)
                {
                    var indexBottomLeft = TerrainHeight * j + i;
                    var indexBottomRight = TerrainHeight * j + (i + 1);
                    var indexUpperLeft = TerrainHeight * (j + 1) + i;
                    var indexUpperRight = TerrainHeight * (j + 1) + (i + 1);

                    #region First Triangle
                    // Upper left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Bottom Left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Bottom Left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Upper left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;
                    #endregion

                    #region Second Triangle
                    // Bottom Left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Upper right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexUpperRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Bottom right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Bottom right
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomRight],
                        color = Vector4.One
                    };
                    indices[index] = index++;

                    // Bottom Left
                    vertices[index] = new ColorShader.Vertex()
                    {
                        position = HeightMap[indexBottomLeft],
                        color = Vector4.One
                    };
                    indices[index] = index++;
                    #endregion
                }
            }
        }