地形生成和纹理

时间:2013-01-29 20:46:03

标签: java opengl textures terrain

我需要纹理生成的地形,我使用下面的代码:

`

   private final int SIZE = 17;

float[][] heightmap = new float[SIZE*2][SIZE];

private final int MAX_HEIGHT = 12;
private final float[] MAX_COLOR_HEIGHTS = new float[] {
    (float) (MAX_HEIGHT/5.0 * 1.0),
    (float) (MAX_HEIGHT/5.0 * 2.0),
    (float) (MAX_HEIGHT/5.0 * 3.0),
    (float) (MAX_HEIGHT/5.0 * 4.0),
    (float) (MAX_HEIGHT/5.0 * 5.0)
};

float[][] noisemap = new float[SIZE*2][SIZE];
private int TerrainNumVertices = (SIZE*2-2)*(SIZE-2)*6*2;
Point4 TerrainPoints[] = new Point4[TerrainNumVertices];
Color4 TerrainColors[] = new Color4[TerrainNumVertices];
Point2 TerrainTex[] = new Point2[TerrainNumVertices];
FloatBuffer TerrainColorsBuf;
FloatBuffer TerrainPointsBuf;
FloatBuffer TerrainTexBuf;

IntBuffer indicesBuf;

private void buildMesh() {
    int index = 0;
    float height = 0.0f;

    for (int z = 0; z < SIZE*2 - 2; z++) {
        for (int x = 0; x < SIZE - 2; x++) {


            ////////////
            //TOP line
            height = heightmap[z+1][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+1, 1.0f);
            height = heightmap[z+1][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+1, 1.0f);

            //RIGHT line
            height = heightmap[z+0][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+0, 1.0f);
            height = heightmap[z+1][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+1, 1.0f);

            //diagonal
            height = heightmap[z+1][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+1, 1.0f);
            height = heightmap[z+0][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+0, 1.0f);

            ////////////
            //bottom line
            height = heightmap[z+0][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+0, 1.0f);
            height = heightmap[z+0][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+0, 1.0f);

            //left line
            height = heightmap[z+0][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+0, 1.0f);
            height = heightmap[z+1][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+1, 1.0f);

            //diagonal
            height = heightmap[z+1][x+0];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+0, height, z+1, 1.0f);
            height = heightmap[z+0][x+1];
            TerrainColors[index] = colorForHeight(height);
            TerrainPoints[index++] = new Point4(x+1, height, z+0, 1.0f);




        }
    }

    TerrainPointsBuf = VectorMath.toBuffer(TerrainPoints);
    TerrainColorsBuf = VectorMath.toBuffer(TerrainColors);
}

private Color4 colorForHeight(float height) {
    Color4 color;
    if (height < MAX_COLOR_HEIGHTS[0]) {
        color = new Color4(0.0f, 0.0f, 0.580392157f, 1.0f); // deep sea
    } else if (height >= MAX_COLOR_HEIGHTS[0] && height < MAX_COLOR_HEIGHTS[1]) {
        color = new Color4(0.0f, 0.580392157f, 1.0f, 1.0f); // sea
    } else if (height >= MAX_COLOR_HEIGHTS[1] && height < MAX_COLOR_HEIGHTS[2]) {
        color = new Color4(0.0f, 0.580392157f, 0.0f, 1.0f); // green
    } else if (height >= MAX_COLOR_HEIGHTS[2] && height < MAX_COLOR_HEIGHTS[3]) {
        color = new Color4(0.870588235f, 0.796078431f, 0.470588235f, 1.0f); // heights
    } else {
        color = new Color4(1.0f, 1.0f, 1.0f, 1.0f); // snow
    }
    return color;
}

private void perlinNoise() {
    //clear heightmap
    for (int z=0; z<SIZE*2; z++) {
        for (int x=0; x<SIZE; x++) {
            heightmap[z][x] = 0f;
        }
    }

    //start of Perlin Noise
    int iterations = (int)Math.round(Math.log(SIZE)/Math.log(2));
    int tile = 0;

    for (int k = 0; k<iterations; k++) {
        tile = SIZE/(int)(Math.pow(2, k));

        //insert noise with wavelength 'tile' and amplitude MAX_HEIGHT/Math.pow(2, k)
        for (int z=0; z<SIZE*2; z+=tile) {
            for (int x=0; x<SIZE; x+=tile) {
                if (x == 0) {
                    noisemap[z][x] = 0.0f;
                } else {
                    noisemap[z][x] = (float)(Math.random()*MAX_HEIGHT/Math.pow(2, k));
                }
            }
        }

        //interpolate values between the random points
        for (int z=0; z<SIZE*2-tile; z+=tile) {
            for (int x=0; x<SIZE-tile; x+=tile) {

                /* bilinear interpolation */
                for (int i=0; i<tile; i++) {
                    for (int j=0; j<tile; j++) {
                        float dx = (float)j/(float)tile;
                        float dz = (float)i/(float)tile;

                        heightmap[z+i][x+j] +=  noisemap[z][x]*(1-dx)*(1-dz) +
                                            noisemap[z][x+tile]*(dx)*(1-dz) +
                                            noisemap[z+tile][x]*(1-dx)*(dz) +
                                            noisemap[z+tile][x+tile]*(dx)*(dz);

                    }
                }

            }
        }


    }
}

非常抱歉语法,但需要使用我的讲师提供的框架。 问题是我不知道如何正确地纹理它,因为它只绘制每个TILE 1三角形(底部,左边的行不想显示,不知道为什么)。

0 个答案:

没有答案