void TextureThread()
{
//OpenTK.INativeWindow window = new OpenTK.NativeWindow();
//OpenTK.Graphics.IGraphicsContext context = new OpenTK.Graphics.GraphicsContext(GLMode, window.WindowInfo);
//context.MakeCurrent(window.WindowInfo);
TextureThreadContextReady.Set();
PendingTextures.Open();
Logger.DebugLog("Started Texture Thread");
while (/*window.Exists &&*/ TextureThreadRunning)
{
//window.ProcessEvents();
TextureLoadItem item = null;
if (!PendingTextures.Dequeue(Timeout.Infinite, ref item)) continue;
// Already have this one loaded
if (item.Data.TextureInfo.TexturePointer != 0) continue;
byte[] imageBytes = null;
if (item.TGAData != null)
{
imageBytes = item.TGAData;
}
else if (item.TextureData != null || item.LoadAssetFromCache)
{
if (item.LoadAssetFromCache)
{
item.TextureData = Client.Assets.Cache.GetCachedAssetBytes(item.Data.TextureInfo.TextureID);
}
ManagedImage mi;
if (!OpenJPEG.DecodeToImage(item.TextureData, out mi)) continue;
bool hasAlpha = false;
bool fullAlpha = false;
bool isMask = false;
if ((mi.Channels & ManagedImage.ImageChannels.Alpha) != 0)
{
fullAlpha = true;
isMask = true;
// Do we really have alpha, is it all full alpha, or is it a mask
for (int i = 0; i < mi.Alpha.Length; i++)
{
if (mi.Alpha[i] < 255)
{
hasAlpha = true;
}
if (mi.Alpha[i] != 0)
{
fullAlpha = false;
}
if (mi.Alpha[i] != 0 && mi.Alpha[i] != 255)
{
isMask = false;
}
}
if (!hasAlpha)
{
mi.ConvertChannels(mi.Channels & ~ManagedImage.ImageChannels.Alpha);
}
}
item.Data.TextureInfo.HasAlpha = hasAlpha;
item.Data.TextureInfo.FullAlpha = fullAlpha;
item.Data.TextureInfo.IsMask = isMask;
imageBytes = mi.ExportTGA();
if (CacheDecodedTextures)
{
RHelp.SaveCachedImage(imageBytes, item.TeFace.TextureID, hasAlpha, fullAlpha, isMask);
}
}
if (imageBytes != null)
{
Image img;
using (MemoryStream byteData = new MemoryStream(imageBytes))
{
img = OpenMetaverse.Imaging.LoadTGAClass.LoadTGA(byteData);
}
Bitmap bitmap = (Bitmap)img;
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
instance.MainForm.BeginInvoke(new MethodInvoker(() =>
{
item.Data.TextureInfo.TexturePointer = RHelp.GLLoadImage(bitmap, item.Data.TextureInfo.HasAlpha);
// GL.Flush();
bitmap.Dispose();
}));
}
item.TextureData = null;
item.TGAData = null;
imageBytes = null;
}
//context.MakeCurrent(window.WindowInfo);
//context.Dispose();
//window.Dispose();
TextureThreadContextReady.Set();
Logger.DebugLog("Texture thread exited");
}