【问题标题】:How can I write a terrain into a .raw file?如何将地形写入 .raw 文件?
【发布时间】:2012-02-08 18:48:24
【问题描述】:

我想通过 Perlin 噪声生成地形并将其存储到 .raw 文件中。

来自Nehe's HeightMap tutorial 我知道.raw 文件是这样读取的:

#define MAP_SIZE        1024    

void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
    FILE *pFile = NULL;

    // Let's open the file in Read/Binary mode.
    pFile = fopen( strName, "rb" );

    // Check to see if we found the file and could open it
    if ( pFile == NULL )    
    {
        // Display our error message and stop the function
        MessageBox(NULL, "Can't find the height map!", "Error", MB_OK);
        return;
    }

    // Here we load the .raw file into our pHeightMap data array.
    // We are only reading in '1', and the size is the (width * height)
    fread( pHeightMap, 1, nSize, pFile );

    // After we read the data, it's a good idea to check if everything read fine.
    int result = ferror( pFile );

    // Check if we received an error.
    if (result)
    {
        MessageBox(NULL, "Can't get data!", "Error", MB_OK);
    }

    // Close the file.
    fclose(pFile);

}

pHeightMap 是一维的,所以我不明白如何将 x,y 对应于高度值。我打算使用libnoisenoise2 function on Ken Perlin's page 来使1024x1024 矩阵中的每个值对应于点的高度,但是.raw 文件存储在一个维度中,我怎样才能使x,y通信工作在那里?

【问题讨论】:

  • 看看它是如何在Height函数中使用的。

标签: c terrain perlin-noise heightmap


【解决方案1】:

令 A 为等维二维数组:

A[3][3] = {
            {'a', 'b', 'c'},
            {'d', 'e', 'f'},
            {'g', 'h', 'i'}
          }

您也可以将此矩阵设计为一维数组:

A[9] = {
         'a', 'b', 'c',
         'd', 'e', 'f',
         'g', 'h', 'i'
       }

在第一种情况(二维)中,您使用类似于A[1][0] 的符号访问第二个数组中的第一个元素。但是,在第二种情况(一维)中,您将使用类似于A[1 * n + 0] 的符号访问相同的元素,其中n 是每个逻辑包含数组的长度,在这种情况下为 3。请注意,您仍然使用相同的索引值(1 和 0),但对于单维情况,您必须将乘数 n 包含在偏移量中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多