0

以下のコードを使用して、生成された地形をテクスチャリングする必要があります。

`

   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三角形ごとにしか描画されないため、適切にテクスチャを作成する方法がわからないことです(下の左の線は表示されたくないため、理由がわかりません)。

4

0 に答える 0