Reading texture data

Eldritch

28-04-2007 09:00:31

I want to be able to read the data from a loaded texture to build myself a navigation map from that texture. Here is the code I use, but it does not work:


public void Load(string strFilename)
{
m_ptrMap = TextureManager.Singleton.Load
(
strFilename,
ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME
);

m_uiWidth = m_ptrMap.Width;
m_uiHeight = m_ptrMap.Height;
m_bNavData = new bool[m_uiWidth, m_uiHeight];

HardwarePixelBufferSharedPtr buf = m_ptrMap.GetBuffer();
unsafe
{
buf.Lock(HardwareBuffer.LockOptions.HBL_NORMAL);
PixelBox pixelBox = buf.CurrentLock;

uint* pSrc = (uint*)pixelBox.data;
for (int i = 0; i < m_uiWidth; i++)
{
for (int j = 0; j < m_uiHeight; j++)
{
uint R = (uint)(*pSrc++);
uint G = (uint)(*pSrc++);
uint B = (uint)(*pSrc++);

if ((R + G + B) / 3 >= 250)
m_bNavData[i, j] = false;
else
m_bNavData[i, j] = true;
}
}
}

buf.Unlock();
buf.Dispose();
m_ptrMap.Dispose();
}


I am not entirely sure I am doing it correctly from the beginning. I want to check each pixel for their color value. If it is white, then it will be blocked, otherwise it will be walkable. Am I on the right path here or totally in the blue?

Blackbeard

01-05-2007 20:58:19

Hi, that should work:


TexturePtr tp=TextureManager.Singleton.Load(strFilename, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME );

bool[,] NavData = new bool[tp.Width, tp.Height];

HardwarePixelBufferSharedPtr buf = tp.GetBuffer();

int whitefoundc = 0;

{
buf.Lock(HardwareBuffer.LockOptions.HBL_NORMAL);
PixelBox pixelBox = buf.CurrentLock;


uint* pSrc = (uint*)pixelBox.data;
for (int i = 0; i < tp.Width; i++)
{
for (int j = 0; j < tp.Height; j++)
{
Color color = Color.FromArgb((int)*pSrc++);

if ((color.R + color.G + color.B) == 765)
{
NavData[i, j] = false;
whitefoundc = whitefoundc + 1;
test = whitefoundc.ToString();
}

}
}
}

buf.Unlock();
buf.Dispose();
tp.Dispose();


And as note for me:

//To get TexturePtr from Material:
//(does not iterate through Technique's, Pass'es etc.)

MaterialPtr mats = MaterialManager.Singleton.GetByName("Grid");
Technique tech = mats.GetTechnique(0);
Pass pass = tech.GetPass(0);
TextureUnitState textUS = pass.GetTextureUnitState(0);
test = textUS.TextureName;
ResourcePtr rp = TextureManager.Singleton.GetByName(test);
TexturePtr tp = (TexturePtr)rp;