Example #1
0
/// <summary>
/// Carga los valores del Heightmap en una matriz
/// </summary>
IntMatrix TgcSimpleTerrain::loadHeightMap(Device d3dDevice, string path)
{
	Texture bitmap = TextureLoader::FromFile(d3dDevice,path.c_str(),0,0,1,Usage_None, Format_X8R8G8B8,Pool_Managed,Filter_None,Filter_None,0);
	SurfaceDescription desc = bitmap.GetSurfaceDescription(0);

	int pitch;
	byte * bdata = (byte*)bitmap.LockRectangle(0,LockFlags_ReadOnly,pitch);

	int width = desc.Width;
	int height = desc.Height;
	IntMatrix heightmap(width, height);
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < height; j++)
		{
			//(j, i) invertido para primero barrer filas y despues columnas
			int offset = j*pitch + i*4;
			byte r = bdata[offset + 0];
			byte g = bdata[offset + 1];
			byte b = bdata[offset + 2];
			float intensity = r * 0.299f + g * 0.587f + b * 0.114f;
			heightmap.SetItem(i, j, (int)intensity);
		}
    
	}

	bitmap.Dispose();
	return heightmap;
}