首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java LWJGL + Noise 3D地形生成

Java LWJGL + Noise 3D地形生成
EN

Stack Overflow用户
提问于 2013-12-03 04:42:05
回答 1查看 1.8K关注 0票数 0

我是OpenGL代码的新手,我想学习如何创建一个简单的地形,更多的是一块积木。我想使用SimplexNoise来做这件事,但我很难理解如何去做。

我将我的文件分成一个主文件和一个块文件,主文件将渲染并“绘制”所有块,块文件为自己创建每个块。如果你能帮助我更好地理解我需要做什么,我将不胜感激。

我需要帮助理解如何绘制块,并放置球员。从GL11.glVertexf和GL11.glTranslatef开始。我如何将它们组合在一起,并做我需要的事情?

提前谢谢你,格伦。

EN

回答 1

Stack Overflow用户

发布于 2014-01-07 09:46:17

我对代码进行了一些编辑,使其成为http://devmag.org.za/2009/04/25/perlin-noise/中的Java

代码语言:javascript
复制
private static Random random = new Random(new Random().nextLong());

private static float[][] generateWhiteNoise(int width, int height) {
    float[][] noise = new float[width][height];

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            noise[i][j] = (float) random.nextDouble() % 1;
        }
    }

    return noise;
}

private static float[][] generateSmoothNoise(float[][] baseNoise, int octave) {
    int width = baseNoise.length;
    int height = baseNoise[0].length;

    float[][] smoothNoise = new float[width][height];

    int samplePeriod = 1 << octave; // calculates 2 ^ k
    float sampleFrequency = 1.0f / samplePeriod;

    for (int i = 0; i < width; i++) {
        // calculate the horizontal sampling indices
        int sample_i0 = (i / samplePeriod) * samplePeriod;
        int sample_i1 = (sample_i0 + samplePeriod) % width; // wrap around
        float horizontal_blend = (i - sample_i0) * sampleFrequency;

        for (int j = 0; j < height; j++) {
            // calculate the vertical sampling indices
            int sample_j0 = (j / samplePeriod) * samplePeriod;
            int sample_j1 = (sample_j0 + samplePeriod) % height; // wrap
                                                                    // around
            float vertical_blend = (j - sample_j0) * sampleFrequency;

            // blend the top two corners
            float top = interpolate(baseNoise[sample_i0][sample_j0],
                    baseNoise[sample_i1][sample_j0], horizontal_blend);

            // blend the bottom two corners
            float bottom = interpolate(baseNoise[sample_i0][sample_j1],
                    baseNoise[sample_i1][sample_j1], horizontal_blend);

            // final blend
            smoothNoise[i][j] = interpolate(top, bottom, vertical_blend);
        }
    }

    return smoothNoise;
}

private static float interpolate(float x0, float x1, float alpha) {
    return x0 * (1 - alpha) + alpha * x1;
}

private static float[][] generatePerlinNoise(float[][] baseNoise,
        int octaveCount) {
    int width = baseNoise.length;
    int height = baseNoise[0].length;

    float[][][] smoothNoise = new float[octaveCount][][]; // an array of 2D
                                                            // arrays
                                                            // containing

    float persistance = 0.5f;

    // generate smooth noise
    for (int i = 0; i < octaveCount; i++) {
        smoothNoise[i] = generateSmoothNoise(baseNoise, i);
    }

    float[][] perlinNoise = new float[width][height];
    float amplitude = 0.0f; // the bigger, the more big mountains
    float totalAmplitude = 0.0f;

    // blend noise together
    for (int octave = octaveCount - 1; octave >= 0; octave--) {
        amplitude *= persistance;
        totalAmplitude += amplitude;

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            perlinNoise[i][j] /= totalAmplitude;
            perlinNoise[i][j] = (float) (Math.floor(perlinNoise[i][j] * 25));
        }
    }

    return perlinNoise;
}
private void generate(){
float[][] noise = generatePerlinNoise(generateWhiteNoise(width, height), 5/*octave count*/);
//...

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20337427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档