使用BMP作为高度图。如何访问像素颜色来设置高度?

时间:2013-10-29 05:10:18

标签: c++ directx-9

所以我试图用bmp创建一些简单的地形,我不知道如何访问像素颜色以获得它的RGB值作为高度。我理解这个概念,而不是如何将其付诸实践。这是我到目前为止的代码。非常感谢任何帮助!

CUSTOMVERTEX vecArray[256][256];

m_pSurface = nullptr;
D3DXIMAGE_INFO imageInfo;
ZeroMemory(&imageInfo, sizeof(D3DXIMAGE_INFO));

HRESULT hr = D3DXGetImageInfoFromFile(L"heightmap.bmp", &imageInfo);

_pDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_SCRATCH, &m_pSurface, 0);

hr = D3DXLoadSurfaceFromFile(m_pSurface, 0, 0, L"heightmap.bmp", 0, D3DX_FILTER_NONE, 0, &imageInfo);

D3DLOCKED_RECT lockRect;
ZeroMemory(&lockRect, sizeof(D3DLOCKED_RECT));

m_pSurface->LockRect(&lockRect, 0, D3DLOCK_READONLY);

int iNumPixels = imageInfo.Width * imageInfo.Height;
int iPixelsWidth = imageInfo.Width;
int iPixelsHeight = imageInfo.Height;

for (int i = 0; i < iPixelsWidth; ++i)          // HORIZONTAL ROWS
{
    for (int j = 0; j < iPixelsHeight; ++j)     // VERTICAL ROWS
    {
        vecArray[i][j].x = (float)i;
        vecArray[i][j].y = (float)j;

        vecArray[i][j].z = ???? // Get Height from bmp

        vecArray[i][j].color =  D3DCOLOR_XRGB(255, 255, 255);
    }
}

m_pSurface->UnlockRect();

结构定义为:

struct CUSTOMVERTEX
{
    FLOAT x, y, z;  // The untransformed, 3D position for the vertex
    DWORD color;
};

1 个答案:

答案 0 :(得分:0)

将像素映射到缓冲区后,将颜色值插入整数(范围0到255)到您的高度范围。如果您使用的是3通道图像(rgb),则需要先将通道平均为一个值(即(r + g + b)/ 3)。这将为您提供单个像素值。

请参阅“线性插值”以将像素范围转换为高度范围。如果您询问如何自己访问像素值,则这取决于用于加载位图的方法。