游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2299|回复: 0

用fault算法生成的地形图腐蚀后地图变平了,怎么解决

[复制链接]

9

主题

15

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2011-5-23 23:10:00 | 显示全部楼层 |阅读模式
使用fault算法生成的地形图,在使用腐蚀参数来平滑地形图之前都没什么问题,使用了腐蚀参数来平滑地形图后地形图就变完全平的了,到底是什么问题?
实现代码:
//************************************
// Method:      CreateHeightMapByFault
// FullName:    ITBTerrain::CreateHeightMapByFault
// Access:      public
// Returns:     bool
// Description: 根据fault formation 生成高度图
// Parameter:   int minDheight 迭代中最小的dheight值,一般取0
// Parameter:   int maxDheight 迭代中最大的dheight值,一般取255
// Parameter:   int iteration  迭代次数
// Parameter:   int size       生成的高度图尺寸
//************************************
bool ITBTerrain::CreateHeightMapByFault(int minDheight,int maxDheight,int iteration,int size,float erosionPmter)
{
        float * pTempheightMap;//一个临时缓存存放高度图
        SetFaultParameter(iteration,erosionPmter);
       
        pTempheightMap = new float[size * size];

        for(int i = 0; i < size * size; i++)
        {
                pTempheightMap = 0;
        }
       
        if(m_heightMap)
        {
        //        UnLoadHeightMap();
        }
        m_heightMap = new UCHAR[size * size];
        m_size = size;


        srand(time(0));
        for(int curietor = 1; curietor <= m_iteration; curietor++)
        {
                int dheight = maxDheight -(curietor*(maxDheight - minDheight))/iteration;
               
                //随机生成两点
                int pointAx  = rand()%size;
                int pointAz  = rand()%size;
                int pointBx  = rand()%size;
                int pointBz  = rand()%size;
                //判断两点是不是在同一个点
                while(pointAx == pointBx && pointAz == pointBz)
                {
                        pointBx = rand()%size;
                        pointBz = rand()%size;
                }
       
                //vector1.x,vector1.z
                int vec_1x = pointAx - pointBx;
                int vec_1z = pointAz - pointBz;
                for (int x = 0; x < size; x++)
                {
                        for(int z = 0; z < size; z++)
                        {
                                int vec_2x = x - pointAx;
                                int vec_2z = z - pointAz;
                                //进行叉乘运算,此处的叉乘用四元数乘法运算
                                //vec2 X vec1
                                if((vec_2x * vec_1z - vec_2z * vec_1x) > 0)
                                {
                                        pTempheightMap[x * size + z] += (float)dheight;
                                }
                        }
                }
       
                FilterHeightMap(pTempheightMap,m_erosionPmter);
        }
        ScaleHeightMap(pTempheightMap);
        for (int x = 0; x < m_size; x++)
        {
                for(int z = 0; z < m_size; z++)
                {
                        SetHeight(x,z,pTempheightMap[x * m_size + z]);
                }
        }
       
        TBDELETEARRAY(pTempheightMap);
        return true;
}
//************************************
// Method:      FilterHeightMap
// FullName:    ITBTerrain::FilterHeightMap
// Access:      public
// Returns:     void
// Description: 过滤高度图。模拟腐蚀效果,实现地形的平滑处理
// Parameter:   float * pHeightData 高度图
// Parameter:   float erosiobPmtr 腐蚀效果的参数
//************************************
void ITBTerrain::FilterHeightMap(float * pHeightData,float erosiobPmtr)
{
        //left-right
        for (int i = 0; i < m_size; i++)
        {
                FileterHeightMapBands(&pHeightData[i*m_size],erosiobPmtr,m_size,1);
        }
        //right-left
        for(int i = 0; i < m_size; i++)
        {
                FileterHeightMapBands(&pHeightData[m_size *i + m_size - 1],erosiobPmtr,m_size,-1);
        }
        //top-bottom
        for (int i = 0; i < m_size; i++)
        {
                FileterHeightMapBands(&pHeightData,erosiobPmtr,m_size,m_size);
        }
        //bottom-top
        for (int i = 0; i < m_size; i++)
        {
                FileterHeightMapBands(&pHeightData[m_size*(m_size-1)+m_size+i],erosiobPmtr,m_size,-m_size);
        }
}

void ITBTerrain::FileterHeightMapBands(float * pBands,float erosionPmtr,int count,int stride)
{
        float yi = pBands[0];
        int j = stride;
        float a=        pBands[j];
        for(int i = 0; i < count-1;i++ )
        {
                pBands[j] = erosionPmtr*yi + (1-erosionPmtr) * pBands[j];
                yi = pBands[j];
                j += stride;
        }
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-10 16:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表