예제 #1
0
        public int RegisterTexture2(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
                {
                    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();

                    //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
                        H2BitmTag.DDSWriter d = new H2BitmTag.DDSWriter();
                        d.WriteHeader(ref bw, ref bmp.m_images[t]);
                        // Now copy the binary data to the stream
                        bw.Write(bmp.m_images[t].DDSData);
                        stream.Seek(0, SeekOrigin.Begin);

                        m_LookupTable[m_LookupCount].TextureList[t].texture = TextureLoader.FromStream(MdxRender.Dev, stream);
                    }

                    tex_index = m_LookupCount;
                    m_LookupCount++;
                }
                catch (Exception e)
                {
                    Trace.WriteLine("TextureManager Exception: " + e.Message);
                }
            }

            return(tex_index);
        }
예제 #2
0
        public int RegisterTexture3(string texture_name, ref byte[] ColorMaps, ref ushort[] LightMapIndex, ref ushort[] ColorMapIndex, ref H2BitmTag.H2Bitmap bmp)
        {
            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 == texture_name)
                {
                    bTextureFound = true;
                    break;
                }
            }

            if (bTextureFound == false)
            {
                //try
                //{

                m_LookupTable[m_LookupCount] = new TEXTURE_LOOKUP_ELEMENT();

                //TODO: create array of textures
                m_LookupTable[m_LookupCount].TextureList = new TextureWithFormat[LightMapIndex.Length];
                byte[] header = new byte[0x80]
                {
                    0x44, 0x44, 0x53, 0x20, 0x7C, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
                    0x10, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
                    0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
                    0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x10, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };

                for (int t = 0; t < LightMapIndex.Length; t++)
                {
                    if (LightMapIndex[t] != 0xffff)
                    {
                        int          ColorsOffset = ColorMapIndex[t] * 0x400;
                        MemoryStream stream       = new MemoryStream();
                        BinaryWriter bw           = new BinaryWriter(stream);
                        //if (LightMapIndex[t] != 0xffff)
                        //{
                        byte[] tmpBuf = new byte[bmp.m_images[LightMapIndex[t]].DDSData.Length * 4];
                        // Write the DDS header
                        PutUInt(ref header, 12, (uint)bmp.m_images[LightMapIndex[t]].height);
                        PutUInt(ref header, 16, (uint)bmp.m_images[LightMapIndex[t]].width);

                        bw.Seek(0, SeekOrigin.Begin);
                        bw.Write(header);

                        for (int cpo = 0; cpo < bmp.m_images[LightMapIndex[t]].DDSData.Length - 4; cpo++)
                        {
                            //SwapInt(ref ColorMaps,(uint)cpo);
                            byte index = bmp.m_images[LightMapIndex[t]].DDSData[cpo];

                            tmpBuf[4 * cpo]     = ColorMaps[ColorsOffset + index * 4];
                            tmpBuf[4 * cpo + 1] = ColorMaps[ColorsOffset + index * 4 + 1];
                            tmpBuf[4 * cpo + 2] = ColorMaps[ColorsOffset + index * 4 + 2];
                            tmpBuf[4 * cpo + 3] = ColorMaps[ColorsOffset + index * 4 + 3];
                        }                                  //ColorMaps.CopyTo(tmpBuf,);
                        bw.Write(tmpBuf, 0, tmpBuf.Length);
                        stream.Seek(0, SeekOrigin.Begin);

                        m_LookupTable[m_LookupCount].TextureList[t]         = new TextureWithFormat();
                        m_LookupTable[m_LookupCount].TextureList[t].texture = TextureLoader.FromStream(MdxRender.Dev, stream);
                    }
                }

                tex_index = m_LookupCount;
                m_LookupCount++;
            }
            //catch(Exception e)
            //{
            //	Trace.WriteLine("TextureManager Exception: " + e.Message);
            //}
            //}
            return(tex_index);
        }
예제 #3
0
        //public void LoadTagData( shader_mgr)

        public void LoadTagData(string FileName)
        {
            int          i;
            BinaryReader br = new BinaryReader(m_stream);

            // Load the Header
            string[] SFileName = FileName.Split(new char[] { '.' }, 256);
            string   DirSep    = @"\";

            string[] NFileName = SFileName[0].Split(DirSep.ToCharArray(), 256);
            string   NewDir    = "";

            for (int dn = 1; dn < NFileName.Length - 1; dn++)
            {
                NewDir += @"\" + NFileName[dn];
            }
            NewDir += @"\" + NFileName[NFileName.Length - 2];
            string LtmpTag = NewDir + "_" + NFileName[NFileName.Length - 1] + "_lightmap.ltmp";
            //string LtmpTag = SFileName[0] + "_" + NFileName[NFileName.Length - 1] + "_lightmap.ltmp";
            string LightMapTag = NewDir + "_" + NFileName[NFileName.Length - 1] + "_lightmap_bitmaps.bitmap";
            //string LightMapTag = SFileName[0] + "_" + NFileName[NFileName.Length - 1] + "_lightmap_bitmaps.bitmap";

            TagFileName tfnLTMP     = new TagFileName(LtmpTag, MapfileVersion.XHALO2);
            TagFileName tfnLightMap = new TagFileName(LightMapTag, MapfileVersion.XHALO2);

            H2BitmTag.H2Bitmap bmp = new H2BitmTag.H2Bitmap();
            bmp.LoadTagBuffer(tfnLightMap);
            bmp.LoadTagData();

            if (tfnLTMP.Exists)
            {
                TagBase tbLTMP = new TagBase();
                tbLTMP.LoadTagBuffer(tfnLTMP);

                //fsin = fiin.Open(FileMode.Open,FileAccess.Read);
                //fsin.Seek(0x40,System.IO.SeekOrigin.Begin);
                byte[] ltmpheader = new byte[0x104];
                tbLTMP.Stream.Read(ltmpheader, 0, ltmpheader.Length);
                uint ltmpsub1Offset = BitConverter.ToUInt32(ltmpheader, 0x84);
                tbLTMP.Stream.Seek((long)ltmpsub1Offset, System.IO.SeekOrigin.Begin);
                byte[] ltmpsub1 = new byte[0xA4];
                tbLTMP.Stream.Read(ltmpsub1, 0, ltmpsub1.Length);
                uint ColorMapCount  = BitConverter.ToUInt32(ltmpsub1, 0x08);
                uint ColorMapOffset = BitConverter.ToUInt32(ltmpsub1, 0x0C);
                tbLTMP.Stream.Seek((long)ColorMapOffset, System.IO.SeekOrigin.Begin);
                uint   ColorMapSize = ColorMapCount * 0x400;
                byte[] ColorMaps    = new byte[ColorMapSize];
                tbLTMP.Stream.Read(ColorMaps, 0, ColorMaps.Length);
                uint bspLightMapCount   = BitConverter.ToUInt32(ltmpsub1, 0x28);
                uint bspLightMapOffset  = BitConverter.ToUInt32(ltmpsub1, 0x2C);
                uint ScenLightMapCount  = BitConverter.ToUInt32(ltmpsub1, 0x48);
                uint ScenLightMapOffset = BitConverter.ToUInt32(ltmpsub1, 0x4C);
                SbspLightMapIndex1 = new ushort[bspLightMapCount];
                ScenLightMapIndex1 = new ushort[ScenLightMapCount];
                SbspLightMapIndex2 = new ushort[bspLightMapCount];
                ScenLightMapIndex2 = new ushort[ScenLightMapCount];

                byte[] tmpin = new byte[0x04];
                tbLTMP.Stream.Seek(bspLightMapOffset, System.IO.SeekOrigin.Begin);
                for (int r = 0; r < bspLightMapCount; r++)
                {
                    tbLTMP.Stream.Read(tmpin, 0, tmpin.Length);
                    SbspLightMapIndex1[r] = BitConverter.ToUInt16(tmpin, 0);
                    SbspLightMapIndex2[r] = BitConverter.ToUInt16(tmpin, 2);
                }
                tbLTMP.Stream.Seek(ScenLightMapOffset, System.IO.SeekOrigin.Begin);
                for (int r = 0; r < ScenLightMapCount; r++)
                {
                    tbLTMP.Stream.Read(tmpin, 0, tmpin.Length);
                    ScenLightMapIndex1[r] = BitConverter.ToUInt16(tmpin, 0);
                    ScenLightMapIndex2[r] = BitConverter.ToUInt16(tmpin, 2);
                }
                m_ColorMapIndex = MdxRender.SM.m_TextureManager.RegisterTexture3("ColorMap", ref ColorMaps, ref SbspLightMapIndex1, ref SbspLightMapIndex2, ref bmp);
                m_ScMapIndex    = MdxRender.SM.m_TextureManager.RegisterTexture3("SColorMap", ref ColorMaps, ref ScenLightMapIndex1, ref ScenLightMapIndex2, ref bmp);
                tbLTMP          = null;
                tfnLTMP         = null;
            }

            b_Header.Load(ref br);
            //string LtmpName = br.BaseStream.
            m_LightmapIndex        = MdxRender.SM.LoadLightmaps2(LightMapTag);
            br.BaseStream.Position = b_Header.Geometry.Offset;
            //Load the Lightmap Sections
            b_Lightmaps = new H2_BSP_LIGHTMAPS[b_Header.Geometry.Count];
            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                b_Lightmaps[i] = new H2_BSP_LIGHTMAPS();
                b_Lightmaps[i].Load(ref br);
            }
            //Save File pos
            long RawDataOffset = br.BaseStream.Position;

            //Move File pointer ahead for somthing
            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                br.BaseStream.Position += b_Lightmaps[i].ModelResourceBlockSize;
            }

            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                b_Lightmaps[i].LoadRawDescriptors(ref br);
            }

            br.BaseStream.Position = RawDataOffset;

            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                b_Lightmaps[i].LoadRawData(ref br);
            }

            this.b_BSPLoaded = true;

            //br.BaseStream.Position = b_Header.Shaders.Offset;
            br.BaseStream.Seek(b_Header.Shaders.Offset, SeekOrigin.Begin);
            m_Shaders = new H2_BSP_SHADER[b_Header.Shaders.Count];
            for (i = 0; i < b_Header.Shaders.Count; i++)
            {
                br.BaseStream.Position += 8;
                m_Shaders[i]            = new H2_BSP_SHADER();
                m_Shaders[i].TagRef.Load(ref br);
                br.BaseStream.Position += 16;
            }

            for (i = 0; i < b_Header.Shaders.Count; i++)
            {
                m_Shaders[i].TagRef.ReadString(ref br);
                m_Shaders[i].ShaderManagerIndex = MdxRender.SM.RegisterShader(new TagFileName(m_Shaders[i].TagRef.data, "dahs", m_PromHeader.GameVersion));
            }

            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                for (int j = 0; j < b_Lightmaps[i].ResourceHeader.InfoChunkCount; j++)
                {
                    b_Lightmaps[i].ResourceInfo[j].ShaderIndex = (ushort)
                                                                 m_Shaders[b_Lightmaps[i].ResourceInfo[j].ShaderIndex].ShaderManagerIndex;
                }
            }


            br.BaseStream.Position = b_Header.Scenery.Offset;
            b_Scenery = new H2_BSP_SCENERY[b_Header.Scenery.Count];
            for (i = 0; i < b_Header.Scenery.Count; i++)
            {
                if (i == 21)
                {
                    int BreakPoint = 0;
                }
                b_Scenery[i] = new H2_BSP_SCENERY();
                b_Scenery[i].Load(ref br);
            }
            RawDataOffset = br.BaseStream.Position;
            //Move File pointer
            for (i = 0; i < b_Header.Geometry.Count; i++)
            {
                br.BaseStream.Position += b_Lightmaps[i].ModelResourceBlockSize;
            }

            for (i = 0; i < b_Header.Scenery.Count; i++)
            {
                b_Scenery[i].LoadRawDescriptors(ref br);
            }
            br.BaseStream.Position = RawDataOffset;

            for (i = 0; i < b_Header.Scenery.Count; i++)
            {
                b_Scenery[i].LoadRawData(ref br);
            }

            for (i = 0; i < b_Header.Scenery.Count; i++)
            {
                for (int j = 0; j < b_Scenery[i].ResourceHeader.InfoChunkCount; j++)
                {
                    b_Scenery[i].ResourceInfo[j].ShaderIndex = (ushort)
                                                               m_Shaders[b_Scenery[i].ResourceInfo[j].ShaderIndex].ShaderManagerIndex;
                }
            }
            b_SceneryPos           = new H2_BSP_SCENERY_POS[b_Header.SceneryPos.Count];
            br.BaseStream.Position = b_Header.SceneryPos.Offset;
            for (int h = 0; h < b_Header.SceneryPos.Count; h++)
            {
                b_SceneryPos[h] = new H2_BSP_SCENERY_POS();
                b_SceneryPos[h].Load(ref br);
            }
        }