C++ 正在读取包含Heightmap的.raw文件
我正在使用libnoise库生成随机地形,并将其保存在.raw文件中,该文件的高程点以米为单位。此地形文件包含16位带符号的大端值,按行的主要顺序从南到北排列。这是我用来读取文件的代码C++ 正在读取包含Heightmap的.raw文件,c++,directx-11,heightmap,C++,Directx 11,Heightmap,我正在使用libnoise库生成随机地形,并将其保存在.raw文件中,该文件的高程点以米为单位。此地形文件包含16位带符号的大端值,按行的主要顺序从南到北排列。这是我用来读取文件的代码 struct HeightMapType { float x, y, z; float nx, ny, nz; float r, g, b; }; bool Terrain::LoadRawFile() { int error, i, j,
struct HeightMapType
{
float x, y, z;
float nx, ny, nz;
float r, g, b;
};
bool Terrain::LoadRawFile()
{
int error, i, j, index;
FILE* filePtr;
unsigned long long imageSize, count;
unsigned short* rawImage;
// Create the float array to hold the height map data.
m_heightMap = new HeightMapType[m_terrainWidth * m_terrainHeight];
if(!m_heightMap)
{
return false;
}
// Open the 16 bit raw height map file for reading in binary.
error = fopen_s(&filePtr, m_terrainFilename, "rb");
if(error != 0)
{
return false;
}
// Calculate the size of the raw image data.
imageSize = m_terrainHeight * m_terrainWidth;
// Allocate memory for the raw image data.
rawImage = new unsigned short[imageSize];
if(!rawImage)
{
return false;
}
// Read in the raw image data.
count = fread(rawImage, sizeof(unsigned short), imageSize, filePtr);
if(count != imageSize)
{
return false;
}
// Close the file.
error = fclose(filePtr);
if(error != 0)
{
return false;
}
// Copy the image data into the height map array.
for(j=0; j<m_terrainHeight; j++)
{
for(i=0; i<m_terrainWidth; i++)
{
index = (m_terrainWidth * j) + i;
// Store the height at this point in the height map array.
m_heightMap[index].y = (float)rawImage[index];
}
}
// Release the bitmap image data.
delete [] rawImage;
rawImage = 0;
// Release the terrain filename now that it has been read in.
delete [] m_terrainFilename;
m_terrainFilename = 0;
return true;
}
结构高度映射类型
{
浮动x,y,z;
浮动nx,ny,nz;
浮子r,g,b;
};
布尔地形::LoadRawFile()
{
int错误,i,j,索引;
文件*filePtr;
无符号长图像大小,计数;
无符号短*原始图像;
//创建浮动数组以保存高度贴图数据。
m_heightMap=新的HeightMapType[m_terrainWidth*m_terrainHeight];
如果(!m_高度图)
{
返回false;
}
//打开16位原始高度映射文件以二进制读取。
错误=fopen_s(&filePtr,m_terrainFilename,“rb”);
如果(错误!=0)
{
返回false;
}
//计算原始图像数据的大小。
imageSize=m_地形高度*m_地形宽度;
//为原始图像数据分配内存。
rawImage=新的无符号短[imageSize];
如果(!rawImage)
{
返回false;
}
//读取原始图像数据。
count=fread(rawmimage,sizeof(unsigned short),imageSize,filePtr);
如果(计数!=图像大小)
{
返回false;
}
//关闭文件。
错误=fclose(文件服务器);
如果(错误!=0)
{
返回false;
}
//将图像数据复制到高度贴图数组中。
对于(j=0;j两个问题:
您使用的是signed short
,但是您在说明中说数字是有符号的。因此您应该使用signed short
如果你在一台小的endian机器上,你应该把你的值从大端到小端转换
可以使用以下命令转换endianness:
short endianConvert(short x) {
unsigned short v = (unsigned short)x;
return (short)(v>>8|v<<8);
}
short-endianConvert(短x){
无符号短v=(无符号短)x;
返回(短)(v>>8 | v感谢您的帮助,但它仍然不会更改渲染图像。是否有方法找到两个不同原始文件之间的差异,以便了解问题所在?必须进行一些更改。如果根本没有更改,则代码中还有其他问题。如果您共享.raw文件,我可以查看它,有什么问题在那里。好的,谢谢!你可以在这里找到链接:我可以很好地渲染terrain.raw。问题在于terrainLIB.rawfile@Swagging:这两个文件的格式似乎不同。即使terrain.raw也不包含您描述的内容。如我所见,它包含一个2049x2049地图,其中每个点存储在一个字节中(不是16位有符号整数)。terrainLIB.raw似乎更复杂,可能它的格式与您描述的格式相同。如果我按照您描述的方式查看此文件,我会看到一个高噪音的地图。