Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm coding a procedural terrain generation based on this article from GPU Gems 1.

But using CPU (not GPU). I'm stuck at generating the procedural terrain. I just can't figure out how make a 3D texture the way it looks in the article (fig. 1-10). Am I missing something big here? Here is the code:

float CTerrain::DensityFunction(float x, float y, float z) {
    CPerlin3DTexture perl_tex;
    float noise;
    uint32_t octaves = 3;
    float amplitude[10] = {0.25,0.5,1.0,1.9,4.1,8.1,16.1,9,10};
    float lacunarity = 1.5;
    float freq[10] = {4.03,1.96,1.01,0.51,0.2487,0.12563,0.8013,0.39913, 0, 0};
    uint32_t i;

noise = 0.0;
    for(i=0; i < octaves; ++i ) {
        noise += perl_tex.noise(x * freq[i],
                                y * freq[i],
                                z * freq[i]) * amplitude[i];
        x *= lacunarity;
        y *= lacunarity;
        z *= lacunarity;

    return noise;

Values (nx,ny,nz) = (128,128,10)

float ***CTerrain::GenerateDensityTexture(uint32_t nx, uint32_t ny, uint32_t nz) {
float step[3];
int32_t i, j, k;

step[0] = 1.0 / (float)nx;
step[1] = 1.0 / (float)ny;
step[2] = 1.0 / (float)nz;
for(i = 1; i < nx-1; i++) {
    for(j = 1; j < ny-1; j++) {
        for(k = 1; k < nz-1; k++) {
            tex[i][j][k] = DensityFunction((float)i * step[0], 
                                           (float)j * step[1], 
                                           (float)k * step[2]);

return tex;

Help, anybody? This is what I get from the above code:

enter image description here

share|improve this question
J/K: It's good, now adapt and make a game that utilizes it! –  joltmode Jul 23 '12 at 16:04
It's not good. There should be no caves and overhangs when using 3 octaves. –  kafka Jul 23 '12 at 16:15
J/K = Just Kidding. –  Byte56 Jul 23 '12 at 16:34
Why should there be no caves and overhangs when using 3 octaves? You're using 3D noise, so there will generally be caves and overhangs even with 1 octave. Also, to my eyes the biggest problem with the screenshots you show looks like just not enough resolution in the polygonization. –  Nathan Reed Jul 23 '12 at 17:52
Ok, here's another screenshot, same code but with nz=20 instead of 10 (a bit thicker). There's enough resolution, it's just that lacuranity is set very high (a typo, should be 5). If it's lower (1.5), the problem is even more obvious (right pic). link –  kafka Jul 23 '12 at 19:47
add comment

1 Answer

up vote 2 down vote accepted

In the article, the base density function is float density = -ws.y; This means that at high altitudes, the density will definitely >= 0 producing completely air, and at low altitudes density < 1 producing solid land.

From your screenshot, it looks like you just generated a solid block of uniformly 50% dense terrain instead of the gradient the article used.

share|improve this answer
Excellent, it works like a charm. noise should be initialized to -y and the step[] variables should be 1.0 or actually shouldn't be there. Also lacunarity in the density function is not necessary. –  kafka Jul 24 '12 at 12:42
Also the 7th and 8th values in the freq[] array are miscalculated and should be 0.06 and 0.03. –  kafka Jul 24 '12 at 13:22
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.