阅读BMP高度图

时间:2013-05-06 22:45:24

标签: directx bmp heightmap

我正在尝试使用带有DirectX的C ++加载高度图。作为测试,我创建了一个全黑的BMP文件,因此地形应该是平坦的。虽然部分边缘不平坦。谁能告诉我为什么?

BITMAPFILEHEADER bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;

ifstream bitmapFile("heightMap.bmp", ios::binary);
if (!bitmapFile)
    return;

// Read in file header.
bitmapFile.read((char*)&bitmapFileHeader, sizeof(BITMAPFILEHEADER));

// Read in the bitmap info header.
bitmapFile.read((char*)&bitmapInfoHeader, sizeof(BITMAPINFOHEADER));

int width = bitmapInfoHeader.biWidth;
int height = bitmapInfoHeader.biHeight;
unsigned char* bitmapImage = new unsigned char[width * height * 3];
bitmapFile.seekg(bitmapFileHeader.bfOffBits, ios::beg);

// Store the image data.
bitmapFile.read((char*)bitmapImage, width * height * 3);

int numCellsWide = 200;
int numCellsHigh = 200;
int numVerticesWide = numCellsWide + 1;
int numVerticesHigh = numCellsHigh + 1;
numberVertices = (numCellsWide + 1) * (numCellsHigh + 1);
numberTriangles = numCellsWide * numCellsHigh * 2;

int cellWidth = 1;
int cellHeight = 1;

// Create the terrain vertices.
Vertex* vertices = new Vertex[numberVertices];
int k = 0;
for (int z = 0; z < numVerticesHigh; z++)
{
    for (int x = 0; x < numVerticesWide; x++)
    {
        int index = z * numVerticesWide + x;
        vertices[index].x = x * cellWidth;
        vertices[index].y = (float)bitmapImage[k];
        vertices[index].z = z * cellHeight;
        vertices[index].tu = vertices[index].x / (numCellsWide * cellWidth);
        vertices[index].tv = vertices[index].z / (numCellsHigh * cellHeight);

        k += 3;
    }
}

0 个答案:

没有答案