0

所以我正在尝试使用 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;
};
4

1 回答 1

0

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

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

于 2013-10-29T05:43:36.527 回答