如何将地形写入 .raw 文件?

发布于 2025-01-03 22:12:40 字数 1495 浏览 4 评论 0原文

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

Nehe 的 HeightMap 教程 我知道 .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 对应于高度值。我打算使用 libnoiseKen Perlin 页面上的 noise2 函数,使 1024x1024 矩阵中的每个值对应于高度对于这一点,但 .raw 文件存储在单个维度中,我怎样才能使 x,y 对应在那里工作?

I want to generate a terrain through Perlin noise and store it into a .raw file.

From Nehe's HeightMap tutorial I know the .raw file is read like this:

#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 is one-dimensional, so I don't understand how I would write the x,y correspondence to a height value. I was planning to use either libnoise or the noise2 function on Ken Perlin's page, to make each value in a 1024x1024 matrix correspond to the height for the point, but the .raw file is stored in a single dimension, how can I make x,y correspondence work there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2025-01-10 22:12:40

令 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

Let A be a 2-dimensional array with equal dimensions:

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

You could also design this matrix as a single-dimension array:

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

In the first case (2-dimension), you access the first element in the second array using a notation similar to A[1][0]. However, in the second case (1-dimension), you would access the same element using a notation similar to A[1 * n + 0], where n is the length of each of the logically contained arrays, 3 in this case. Note that you still use the same index values (1 and 0), but for the single-dimensional case you have to include that multiplier n for the offset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文