Example #1
0
 public Terrain(int gridX, int gridZ, Loader loader, TerrainTexturePack texturePack, TerrainTexture blendMap)
 {
     TexturePack = texturePack;
     BlendMap    = blendMap;
     X           = gridX * SIZE;
     Z           = gridZ * SIZE;
     generator   = new HeightsGenerator(gridX, gridZ, vertexCount, seed);
     Model       = GenerateTerrain(loader);
 }
Example #2
0
        private RawModel GenerateTerrain(Loader loader)
        {
            HeightsGenerator generator = new HeightsGenerator();
            int count = vertexCount * vertexCount;

            heights = new float[vertexCount, vertexCount];
            float[] vertices      = new float[count * 3];
            float[] normals       = new float[count * 3];
            float[] textureCoords = new float[count * 2];
            uint[]  indices       = new uint[6 * (vertexCount - 1) * (vertexCount * 1)];
            int     vertexPointer = 0;

            for (int i = 0; i < vertexCount; i++)
            {
                for (int j = 0; j < vertexCount; j++)
                {
                    vertices[vertexPointer * 3] = j / ((float)vertexCount - 1) * SIZE;
                    float height = GetHeight(j, i);
                    vertices[vertexPointer * 3 + 1] = height;
                    heights[j, i] = height;
                    vertices[vertexPointer * 3 + 2] = i / ((float)vertexCount - 1) * SIZE;
                    Vector3 normal = CalculateNormal(j, i);
                    normals[vertexPointer * 3]           = normal.X;
                    normals[vertexPointer * 3 + 1]       = normal.Y;
                    normals[vertexPointer * 3 + 2]       = normal.Z;
                    textureCoords[vertexPointer * 2]     = j / ((float)vertexCount - 1);
                    textureCoords[vertexPointer * 2 + 1] = i / ((float)vertexCount - 1);
                    vertexPointer++;
                }
            }
            int pointer = 0;

            for (int gz = 0; gz < vertexCount - 1; gz++)
            {
                for (int gx = 0; gx < vertexCount - 1; gx++)
                {
                    uint topLeft     = (uint)Math.Abs((gz * vertexCount) + gx);
                    uint topRight    = topLeft + 1;
                    uint bottomLeft  = (uint)Math.Abs(((gz + 1) * vertexCount) + gx);
                    uint bottomRight = bottomLeft + 1;
                    indices[pointer++] = topLeft;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = topRight;
                    indices[pointer++] = topRight;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = bottomRight;
                }
            }
            return(loader.LoadToVao(vertices, textureCoords, normals, indices));
        }