public int RegisterCubemap(TagFileName tfn) { int tex_index = DEFAULT_TEXTURE; bool bTextureFound = false; //TODO: use hash table? for (tex_index = 0; ((tex_index < MaxElements) && (tex_index < m_LookupCount)); tex_index++) { if (m_LookupTable[tex_index].name == tfn.RelativePath) { bTextureFound = true; break; } } if (bTextureFound == false) { try { if (tfn.Exists == false) { Trace.WriteLine("Could not locate texture: " + tfn.RelativePath); } else { H1BitmTag.Bitmap bmp = new H1BitmTag.Bitmap(); if (!bmp.LoadTagBuffer(tfn)) { throw new Exception("Could not load bitmap: " + tfn.RelativePath); } bmp.LoadTagData(); m_LookupTable[m_LookupCount] = new CUBE_TEXTURE_LOOKUP_ELEMENT(); m_LookupTable[m_LookupCount].name = tfn.RelativePath; //TODO: create array of textures Trace.WriteLine("Loading texture: " + tfn.RelativePath); //assume there is only 1 cube map in the .bitmap file? if (bmp.m_header.Bitmaps.Count > 1) { throw new Exception("Cannot load multiple cubemaps from one tag."); } MemoryStream stream = new MemoryStream(); BinaryWriter bw = new BinaryWriter(stream); // Write the DDS header H1BitmTag.DDSWriter d = new H1BitmTag.DDSWriter(); d.GenerateAndWriteHeader(ref bw, ref bmp.m_images[0], true); // Now copy the raw pixel data (6 sides), but skip past the mipmaps MemoryStream tmpStream = new MemoryStream(bmp.m_images[0].DDSData); int rSize = (bmp.m_images[0].width * bmp.m_images[0].height * d.header.ddsd.ddfPixelFormat.RGBBitCount) / 8; int pSize = bmp.m_images[0].DDSData.Length / 6 - rSize; byte[] rawbytes = new byte[rSize]; //figure out sorting tmpStream.Position = (rSize * 0); tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); tmpStream.Position = (rSize * 2); tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); tmpStream.Position = (rSize * 1); tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); tmpStream.Position = (rSize * 3); tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); tmpStream.Position = (rSize * 4);//g tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); tmpStream.Position = (rSize * 5);//g tmpStream.Read(rawbytes, 0, rawbytes.Length); bw.Write(rawbytes); rawbytes = null; //write data out to dds file // FileStream fs = new FileStream(@"c:\temp\cubemap.dds", FileMode.Create); // byte[] tmp = new byte[stream.Length]; // stream.Seek(0, SeekOrigin.Begin); // stream.Read(tmp, 0, (int)stream.Length); // fs.Write(tmp, 0, (int)stream.Length); // fs.Close(); stream.Seek(0, SeekOrigin.Begin); ((CUBE_TEXTURE_LOOKUP_ELEMENT)m_LookupTable[m_LookupCount]).CubeTexture = TextureLoader.FromCubeStream(MdxRender.Dev, stream); m_LookupTable[m_LookupCount].bCubemap = true; tex_index = m_LookupCount; m_LookupCount++; } } catch (Exception e) { Trace.WriteLine("TextureManager Exception: " + e.Message); } } return(tex_index); }
public int RegisterTexture(TagFileName tfn) { int tex_index = DEFAULT_TEXTURE; bool bTextureFound = false; //TODO: use hash table? for (tex_index = 0; ((tex_index < MaxElements) && (tex_index < m_LookupCount)); tex_index++) { if (m_LookupTable[tex_index].name == tfn.RelativePath) { bTextureFound = true; break; } } if (bTextureFound == false) { try { if (tfn.Exists == false) { Trace.WriteLine("Could not locate texture: " + tfn.RelativePath); } else { H1BitmTag.Bitmap bmp = new H1BitmTag.Bitmap(); //H2BitmTag.H2Bitmap bmp = new H2BitmTag.H2Bitmap(); if (!bmp.LoadTagBuffer(tfn)) { throw new Exception("Could not load bitmap: " + tfn.RelativePath); } bmp.LoadTagData(); m_LookupTable[m_LookupCount] = new TEXTURE_LOOKUP_ELEMENT(); m_LookupTable[m_LookupCount].name = tfn.RelativePath; //TODO: create array of textures m_LookupTable[m_LookupCount].TextureList = new TextureWithFormat[bmp.m_header.Bitmaps.Count]; Trace.WriteLine("Loading texture: " + tfn.RelativePath); for (int t = 0; t < bmp.m_header.Bitmaps.Count; t++) { m_LookupTable[m_LookupCount].TextureList[t] = new TextureWithFormat(); MemoryStream stream = new MemoryStream(); BinaryWriter bw = new BinaryWriter(stream); // Write the DDS header H1BitmTag.DDSWriter d = new H1BitmTag.DDSWriter(); d.GenerateAndWriteHeader(ref bw, ref bmp.m_images[t], false); // Now copy the binary data to the stream bw.Write(bmp.m_images[t].DDSData); // if(tfn.RelativePath == @"levels\test\bloodgulch\bitmaps\mp lights small strips red.bitmap") // { // //write data out to dds file // FileStream fs = new FileStream(@"c:\temp\test.dds", FileMode.Create); // byte[] tmp = new byte[stream.Length]; // stream.Seek(0, SeekOrigin.Begin); // stream.Read(tmp, 0, (int)stream.Length); // fs.Write(tmp, 0, (int)stream.Length); // fs.Close(); // } stream.Seek(0, SeekOrigin.Begin); m_LookupTable[m_LookupCount].TextureList[t].texture = TextureLoader.FromStream(MdxRender.Dev, stream); m_LookupTable[m_LookupCount].TextureList[t].Width = bmp.m_images[t].width; m_LookupTable[m_LookupCount].TextureList[t].Height = bmp.m_images[t].height; m_LookupTable[m_LookupCount].TextureList[t].Format = bmp.m_images[t].format; m_LookupTable[m_LookupCount].TextureList[t].TextureDataLength = bmp.m_images[t].size; } tex_index = m_LookupCount; m_LookupCount++; } } catch (Exception e) { Trace.WriteLine("TextureManager Exception: " + e.Message); } } return(tex_index); }