0
我正在使用libnoise库生成随机地形并将其保存在以。米为单位测量其仰角点的.raw文件中。这个terrain文件包含16位有符号的big-endian值,按照行优先顺序从南到北排列。这是我用来阅读文件的代码。读取包含高度图的.raw文件
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;
}
该代码不返回任何错误,但是这是呈现的结果:rawFileRendering。
我测试了另一个保存在原始文件中的高度图代码(由rastertek给出),它的工作原理。
你知道为什么渲染场景是这样吗? 谢谢你的帮助。
感谢您的帮助,但它仍然不会改变渲染的图像。有没有办法找到两个不同的原始文件之间的差异,以了解什么是错的? – Swagging
必须有一些变化。如果它根本没有改变,那么你的代码还有一些其他的问题。如果你分享.raw文件,我可以研究它,那里有什么。 – geza
好的,谢谢!你可以在这里找到链接:https://drive.google.com/file/d/0B5qZUPGKjCpqUW1iMlVIV3Z0M0E/view?usp=sharing – Swagging