So I'm trying to create some simple terrain using a bmp and I don't know how to access the pixel color to get it's RGB value to use as the height. I understand the concept, just not how to put it into practice. This is the code I have so far. Any help is very much appreciated!

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();

The struct is define as:

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

解决方案

Once you have the pixels mapped into a buffer, interpolate the color value as an integer (range 0 to 255) to your height range. If you are using a 3-channel image (rgb) you will need to first average the channels to one value (ie. (r + g + b) / 3). That will give you a single value for the pixel.

See "linear interpolation" to convert from pixel range to height range. If you're asking how to access the pixel values themselves, that depends on the method used to load the bitmap.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top