粒子沉积地形生成

Cal*_*lie 3 c++ procedural-generation terrain

我正在使用粒子沉积来尝试在程序上创造一些类似火山的山脉,但我所能摆脱的只是金字塔状的结构.是否有人熟悉该算法可能能够揭示我可能做错的事情.我现在正在将每个粒子放在同一个地方.如果我不这样做,它们会分散在非常薄的一层,而不是任何一种山.

void TerrainClass::ParticalDeposition(int loops){
float height = 0.0;




//for(int k= 0; k <10; k++){

    int dropX = mCurrentX = rand()%(m_terrainWidth-80) + 40;
    int dropY = mCurrentZ = rand()%(m_terrainHeight-80) + 40;
    int radius = 15;
    float angle = 0;
    int tempthing = 0;
    loops = 360;

    for(int i = 0; i < loops; i++){



        mCurrentX = dropX + radius * cos(angle);
        mCurrentZ = dropY + radius * sin(angle);

        /*f(i%loops/5 == 0){
            dropX -= radius * cos(angle);
            dropY += radius * sin(angle);
            angle+= 0.005;
            mCurrentX = dropX;
            mCurrentZ = dropY;
        }*/

        angle += 360/loops;


        //dropX += rand()%5;
        //dropY += rand()%5;

        //for(int j = 0; j < loops; j++){



            float newY = 0;

            newY = (1 - (2.0f/loops)*i); 

            if(newY < 0.0f){
                newY = 0.0f;
            }



            DepositParticle(newY);
        //}
    }
//}
}

void TerrainClass::DepositParticle(float heightIncrease){

bool posFound = false;

m_lowerList.clear();

while(posFound == false){
    int offset = 10;
    int jitter;

    if(Stable(0.5f)){
        m_heightMap[(m_terrainHeight*mCurrentZ)+mCurrentX].y += heightIncrease;
        posFound = true;
    }else{
        if(!m_lowerList.empty()){

            int element = rand()%m_lowerList.size();

            int lowerIndex = m_lowerList.at(element);

            MoveTo(lowerIndex);

        }
    }
} 
}

bool TerrainClass::Stable(float deltaHeight){

int index[9];
float height[9];

index[0] = ((m_terrainHeight*mCurrentZ)+mCurrentX);                                                                     //the current index
index[1] = ValidIndex((m_terrainHeight*mCurrentZ)+mCurrentX+1)     ? (m_terrainHeight*mCurrentZ)+mCurrentX+1    : -1;   // if the index to the right is valid index set index[] to index else set index[] to -1
index[2] = ValidIndex((m_terrainHeight*mCurrentZ)+mCurrentX-1)     ? (m_terrainHeight*mCurrentZ)+mCurrentX-1    : -1;   //to the left
index[3] = ValidIndex((m_terrainHeight*(mCurrentZ+1))+mCurrentX)   ? (m_terrainHeight*(mCurrentZ+1))+mCurrentX  : -1;   // above
index[4] = ValidIndex((m_terrainHeight*(mCurrentZ-1))+mCurrentX)   ? (m_terrainHeight*(mCurrentZ-1))+mCurrentX  : -1;   // bellow
index[5] = ValidIndex((m_terrainHeight*(mCurrentZ+1))+mCurrentX+1) ? (m_terrainHeight*(mCurrentZ+1))+mCurrentX+1: -1;   // above to the right
index[6] = ValidIndex((m_terrainHeight*(mCurrentZ-1))+mCurrentX+1) ? (m_terrainHeight*(mCurrentZ-1))+mCurrentX+1: -1;   // below to the right
index[7] = ValidIndex((m_terrainHeight*(mCurrentZ+1))+mCurrentX-1) ? (m_terrainHeight*(mCurrentZ+1))+mCurrentX-1: -1;   // above to the left
index[8] = ValidIndex((m_terrainHeight*(mCurrentZ-1))+mCurrentX-1) ? (m_terrainHeight*(mCurrentZ-1))+mCurrentX-1: -1;   // above to the right

for ( int i = 0; i < 9; i++){
    height[i] = (index[i] != -1) ? m_heightMap[index[i]].y : -1;
}

m_lowerList.clear();

for(int i = 1; i < 9; i++){
    if(height[i] != -1){
        if(height[i] < height[0] - deltaHeight){
            m_lowerList.push_back(index[i]);
        }
    }
}

return m_lowerList.empty();
}

bool TerrainClass::ValidIndex(int index){
return (index > 0 && index < m_terrainWidth*m_terrainHeight) ?  true : false;
}

void TerrainClass::MoveTo(int index){
mCurrentX = index%m_terrainWidth;
mCurrentZ = index/m_terrainHeight;
}
Run Code Online (Sandbox Code Playgroud)

这是所有使用的代码.

Mat*_*ský 8

你应该看看这两篇论文:

GPU上的快速水力侵蚀模拟与可视化

GPU上的快速液压和热侵蚀(首先读取第一个,第二个扩展)

不要被"在GPU上"吓到,算法在CPU上工作得很好(尽管速度较慢).算法本身不进行粒子沉降(但你也不做;)) - 它们将粒子聚合成几层矢量场.

这个算法的一个重要之处在于它侵蚀了已经存在的高度图 - 例如用perlin噪声生成的.如果初始高度场完全平坦(或者即使它没有足够的高度变化),它也会失败.

我自己实现了这个算法,并且大部分时间都取得了成功(还有很多工作要做,算法很难平衡以获得普遍好的结果) - 见下图.

请注意,第二篇论文中使用热风化成分的柏林噪音对您来说可能已经足够了(并且可能为您节省很多麻烦).

您还可以在我的项目中找到基于C++ CPU的此算法实现(特别是此文件,请注意GPL许可证!)及其论文第24-29页的简化说明.

侵蚀的例子