如何将地形写入 .raw 文件?
我想通过 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 对应于高度值。我打算使用 libnoise 或 Ken 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令 A 为具有相同维度的二维数组:
您还可以将此矩阵设计为一维数组:
在第一种情况(二维)中,您可以使用类似于 < 的表示法访问第二个数组中的第一个元素代码>A[1][0]。但是,在第二种情况(一维)中,您将使用类似于
A[1 * n + 0]
的表示法来访问相同的元素,其中n
是每个逻辑包含的数组的长度,在本例中为 3。请注意,您仍然使用相同的索引值(1 和 0),但对于一维情况,您必须包含偏移量的乘数n
。Let A be a 2-dimensional array with equal dimensions:
You could also design this matrix as a single-dimension array:
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 toA[1 * n + 0]
, wheren
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 multipliern
for the offset.