我正在DirectX11中创建一个高度图,当我将地形高度指定为bitmapInfoHeader中的地形高度时,我将获得一个访问冲突写入位置。
bool HMTerrain::LoadHeightMap(char* filename)
{
FILE* filePtr;
int error;
unsigned int count;
BITMAPFILEHEADER bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;
int imageSize, i, j, k, index;
unsigned char* bitmapImage;
unsigned char height;
// Open the height map file in binary.
error = fopen_s(&filePtr, filename, "rb");
if (error != 0)
{
return false;
}
// Read in the file header.
count = fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
if (count != 1)
{
return false;
}
// Read in the bitmap info header.
count = fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
if (count != 1)
{
return false;
}
// Save the dimensions of the terrain.
_pTerrainWidth = bitmapInfoHeader.biWidth; // THIS IS THE BREAK
_pTerrainHeight = bitmapInfoHeader.biHeight;
// Calculate the size of the bitmap image data.
imageSize = _pTerrainWidth * _pTerrainHeight * 3;
// Allocate memory for the bitmap image data.
bitmapImage = new unsigned char[imageSize];
if (!bitmapImage)
{
return false;
}
// Move to the beginning of the bitmap data.
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
// Read in the bitmap image data.
count = fread(bitmapImage, 1, imageSize, filePtr);
if (count != imageSize)
{
return false;
}
// Close the file.
error = fclose(filePtr);
if (error != 0)
{
return false;
}
// Create the structure to hold the height map data.
_pHeightMap = new HeightMapType[_pTerrainWidth * _pTerrainHeight];
if (!_pHeightMap)
{
return false;
}
// Initialize the position in the image data buffer.
k = 0;
// Read the image data into the height map.
for (j = 0; j<_pTerrainHeight; j++)
{
for (i = 0; i<_pTerrainWidth; i++)
{
height = bitmapImage[k];
index = (_pTerrainHeight * j) + i;
_pHeightMap[index].x = (float)i;
_pHeightMap[index].y = (float)height;
_pHeightMap[index].z = (float)j;
k += 3;
}
}
// Release the bitmap image data.
delete[] bitmapImage;
bitmapImage = 0;
return true;
}
///////////////////////// HEADER FILE /////////////////////////
class HMTerrain
{
private:
INT _pTerrainWidth, _pTerrainHeight;
int _pVertexCount, _pIndexCount;
ID3D11Buffer* _pVertexBuffer, *_pIndexBuffer;
HeightMapType* _pHeightMap;
bool LoadHeightMap(char*);
bool InitialiseBuffers(ID3D11Device*);
void NormalizeHeightMap();
void ShutdownHeightMap();
void ShutdownBuffers();
void RenderBuffers(ID3D11DeviceContext*);
public:
HMTerrain();
HMTerrain(const HMTerrain&);
~HMTerrain();
bool Initialise(ID3D11Device*, char*);
void Shutdown();
void Render(ID3D11DeviceContext*);
int GetIndexCount();
};
_pTerrainWidth是一个int。虽然biWidth是DirectX LONG。我相信这是问题但是当我将_pTerrainWidth更改为DirectX LONG时,我仍然在同一行上获得相同的违规错误。
任何想法都会非常感激:-))