Esempio n. 1
0
        public void LoadTextures(string fileName, CDC.Objects.ExportOptions options)
        {
            FileShaderResourceViewDictionary.Clear();

            Type currentFileType = File.GetType();

            if (currentFileType == typeof(TRLFile))
            {
                try
                {
                    TRLPCTextureFile textureFile = new TRLPCTextureFile(fileName);

                    SceneCDC.progressLevel  = 0;
                    SceneCDC.progressLevels = textureFile.TextureCount;
                    SceneCDC.ProgressStage  = "Loading Textures";

                    for (int t = 0; t < textureFile.TextureCount; t++)
                    {
                        String textureName = SRModel.GetPS2TextureName(File.Name, (int)textureFile.TextureDefinitions[t].ID) + TextureExtension;

                        System.IO.MemoryStream stream = textureFile.GetDataAsStream(t);
                        //textureFile.ExportFile(t, "C:\\Users\\A\\Desktop\\Lara\\" + textureName);
                        if (stream != null)
                        {
                            if (textureFile.TextureDefinitions[t].Format == BenLincoln.TheLostWorlds.CDTextures.DRMFormat.Uncompressed)
                            {
                                MemoryStream stream2 = textureFile.GetUncompressedDataAsStream2(t);
                                AddTexture(stream2, textureName);
                            }
                            else
                            {
                                AddTexture(stream, textureName);
                            }
                            _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(t));
                        }

                        SceneCDC.progressLevel++;
                    }
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("Error: couldn't find a texture file");
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
            else if (currentFileType == typeof(SR2File) ||
                     currentFileType == typeof(DefianceFile))
            {
                try
                {
                    SR2PCTextureFile textureFile = new SR2PCTextureFile(fileName);

                    SceneCDC.progressLevel  = 0;
                    SceneCDC.progressLevels = textureFile.TextureCount;
                    SceneCDC.ProgressStage  = "Loading Textures";

                    for (int t = 0; t < textureFile.TextureCount; t++)
                    {
                        String textureName = SRModel.GetPS2TextureName(File.Name, textureFile.TextureDefinitions[t].Flags1) + TextureExtension;

                        System.IO.MemoryStream stream = textureFile.GetDataAsStream(t);
                        //textureFile.ExportFile(t, "C:\\Users\\A\\Desktop\\" + textureName);
                        if (stream != null)
                        {
                            AddTexture(stream, textureName);
                            _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(t));
                        }

                        SceneCDC.progressLevel++;
                    }
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("Error: couldn't find a texture file");
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
            else if (currentFileType == typeof(SR1File))
            {
                if (File.Platform == CDC.Platform.PC)
                {
                    try
                    {
                        SR1PCTextureFile textureFile = new SR1PCTextureFile(fileName);

                        SceneCDC.progressLevel  = 0;
                        SceneCDC.progressLevels = File.GetNumMaterials();
                        SceneCDC.ProgressStage  = "Loading Textures";

                        foreach (SRModel srModel in File.Models)
                        {
                            foreach (CDC.Material material in srModel.Materials)
                            {
                                if (material.textureUsed)
                                {
                                    System.IO.MemoryStream stream = textureFile.GetDataAsStream(material.textureID);
                                    if (stream != null)
                                    {
                                        String textureName = SRModel.GetSoulReaverPCOrDreamcastTextureName(srModel.Name, material.textureID) + TextureExtension;
                                        AddTexture(stream, textureName);
                                        if (!_TexturesAsPNGs.ContainsKey(textureName))
                                        {
                                            _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(material.textureID));
                                        }
                                    }
                                }

                                SceneCDC.progressLevel++;
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        Console.WriteLine("Error: couldn't find a texture file");
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.ToString());
                    }
                }
                else if (File.Platform == CDC.Platform.Dreamcast)
                {
                    try
                    {
                        SR1DCTextureFile textureFile = new SR1DCTextureFile(fileName);

                        SceneCDC.progressLevel  = 0;
                        SceneCDC.progressLevels = File.GetNumMaterials();
                        SceneCDC.ProgressStage  = "Loading Textures";

                        foreach (SRModel srModel in File.Models)
                        {
                            foreach (CDC.Material material in srModel.Materials)
                            {
                                if (material.textureUsed)
                                {
                                    int textureID = material.textureID;
                                    System.IO.MemoryStream stream = textureFile.GetDataAsStream(textureID);
                                    if (stream != null)
                                    {
                                        String textureName = SRModel.GetSoulReaverPCOrDreamcastTextureName(srModel.Name, material.textureID) + TextureExtension;

                                        AddTexture(stream, textureName);

                                        if (!_TexturesAsPNGs.ContainsKey(textureName))
                                        {
                                            _TexturesAsPNGs.Add(textureName, textureFile.GetTextureAsBitmap(material.textureID));
                                        }
                                    }
                                }

                                SceneCDC.progressLevel++;
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        Console.WriteLine("Error: couldn't find a texture file");
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.ToString());
                    }
                }
                else
                {
                    try
                    {
                        SR1PSTextureFile textureFile = new SR1PSTextureFile(fileName);

                        SceneCDC.progressLevel  = 0;
                        SceneCDC.progressLevels = 100;                         // Arbitrarily large number.
                        SceneCDC.ProgressStage  = "Loading Textures";

                        // Trick it into showing a progress bar.
                        System.Threading.Thread.Sleep(100);
                        SceneCDC.progressLevel = 100;

                        UInt32 polygonCountAllModels = 0;
                        foreach (SRModel srModel in File.Models)
                        {
                            polygonCountAllModels += srModel.PolygonCount;
                        }

                        SR1PSTextureFile.SoulReaverPlaystationPolygonTextureData[] polygons =
                            new SR1PSTextureFile.SoulReaverPlaystationPolygonTextureData[polygonCountAllModels];

                        int polygonNum = 0;
                        foreach (SRModel srModel in File.Models)
                        {
                            foreach (CDC.Polygon polygon in srModel.Polygons)
                            {
                                polygons[polygonNum].paletteColumn = polygon.paletteColumn;
                                polygons[polygonNum].paletteRow    = polygon.paletteRow;
                                polygons[polygonNum].u             = new int[3];
                                polygons[polygonNum].v             = new int[3];
                                //polygons[polygonNum].materialColour = polygon.material.colour;
                                polygons[polygonNum].materialColour = polygon.colour;

                                polygons[polygonNum].u[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].u * 255);
                                polygons[polygonNum].v[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].v * 255);
                                polygons[polygonNum].u[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].u * 255);
                                polygons[polygonNum].v[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].v * 255);
                                polygons[polygonNum].u[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].u * 255);
                                polygons[polygonNum].v[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].v * 255);

                                polygons[polygonNum].textureID = polygon.material.textureID;
                                polygons[polygonNum].CLUT      = polygon.material.clutValue;

                                polygons[polygonNum].textureUsed = polygon.material.textureUsed;
                                polygons[polygonNum].visible     = polygon.material.visible;
                                //polygons[polygonNum].materialColour = polygon.material.colour;

                                polygonNum++;
                            }
                        }
                        bool drawGreyscaleFirst = false;
                        bool quantizeBounds     = true;
                        textureFile.BuildTexturesFromPolygonData(polygons, drawGreyscaleFirst, quantizeBounds, options);

                        // For all models
                        for (int t = 0; t < textureFile.TextureCount; t++)
                        {
                            String textureName = SRModel.GetPlayStationTextureNameDefault(File.Name, t) + TextureExtension;

                            System.IO.MemoryStream stream = textureFile.GetDataAsStream(t);
                            if (stream != null)
                            {
                                AddTexture(stream, textureName);
                            }
                            //string exportedTextureFileName = Path.ChangeExtension(textureName, "png");
                            //_TexturesAsPNGs.Add(exportedTextureFileName, textureFile.GetTextureAsBitmap(t));
                            Bitmap b = textureFile.GetTextureAsBitmap(t);
                            // this is a hack that's being done here for now because I don't know for sure which of the flags/attributes controls
                            // textures that should be alpha-masked. Alpha-masking EVERY texture is expensive.
                            if (options.AlsoInferAlphaMaskingFromTexturePixels)
                            {
                                if (BitmapHasTransparentPixels(b))
                                {
                                    for (int modelNum = 0; modelNum < File.Models.Length; modelNum++)
                                    {
                                        if (t < File.Models[modelNum].Materials.Length)
                                        {
                                            File.Models[modelNum].Materials[t].UseAlphaMask = true;
                                        }
                                    }
                                }
                            }
                            _TexturesAsPNGs.Add(textureName, b);

                            // dump all textures as PNGs for debugging
                            //Bitmap exportedTexture = textureFile.GetTextureAsBitmap(t);
                            //string exportedTextureFileName = Path.ChangeExtension(textureName, "png");
                            //exportedTexture.Save(exportedTextureFileName, ImageFormat.Png);
                            //texNum = 0;
                            //foreach (Bitmap tex in _Textures)
                            //{
                            //    tex.Save(@"C:\Debug\Tex-" + texNum + ".png", ImageFormat.Png);
                            //    texNum++;
                            //}

                            SceneCDC.progressLevel++;
                        }

                        // for models that use index/CLUT textures, if the user has enabled this option
                        if (options.UseEachUniqueTextureCLUTVariation)
                        {
                            foreach (int textureID in textureFile.TexturesByCLUT.Keys)
                            {
                                Dictionary <ushort, Bitmap> textureCLUTCollection = textureFile.TexturesByCLUT[textureID];
                                foreach (ushort clut in textureCLUTCollection.Keys)
                                {
                                    String textureName            = SRModel.GetPlayStationTextureNameWithCLUT(File.Name, textureID, clut) + TextureExtension;
                                    System.IO.MemoryStream stream = textureFile.GetTextureWithCLUTAsStream(textureID, clut);
                                    if (stream != null)
                                    {
                                        AddTexture(stream, textureName);
                                    }
                                    Bitmap b = textureFile.TexturesByCLUT[textureID][clut];
                                    _TexturesAsPNGs.Add(textureName, b);

                                    SceneCDC.progressLevel++;
                                }
                            }
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        Console.WriteLine("Error: couldn't find a texture file");
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.ToString());
                    }
                }
            }
            else
            {
                try
                {
                    Gex3PSTextureFile textureFile = new Gex3PSTextureFile(fileName);

                    UInt32 polygonCountAllModels = 0;
                    foreach (SRModel srModel in File.Models)
                    {
                        polygonCountAllModels += srModel.PolygonCount;
                    }

                    Gex3PSTextureFile.Gex3PlaystationPolygonTextureData[] polygons =
                        new Gex3PSTextureFile.Gex3PlaystationPolygonTextureData[polygonCountAllModels];

                    int polygonNum = 0;
                    foreach (SRModel srModel in File.Models)
                    {
                        foreach (CDC.Polygon polygon in srModel.Polygons)
                        {
                            polygons[polygonNum].paletteColumn = polygon.paletteColumn;
                            polygons[polygonNum].paletteRow    = polygon.paletteRow;
                            polygons[polygonNum].u             = new int[3];
                            polygons[polygonNum].v             = new int[3];
                            //polygons[polygonNum].materialColour = polygon.material.colour;
                            polygons[polygonNum].materialColour = polygon.colour;

                            polygons[polygonNum].u[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].u * 255);
                            polygons[polygonNum].v[0] = (int)(srModel.Geometry.UVs[polygon.v1.UVID].v * 255);
                            polygons[polygonNum].u[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].u * 255);
                            polygons[polygonNum].v[1] = (int)(srModel.Geometry.UVs[polygon.v2.UVID].v * 255);
                            polygons[polygonNum].u[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].u * 255);
                            polygons[polygonNum].v[2] = (int)(srModel.Geometry.UVs[polygon.v3.UVID].v * 255);

                            polygons[polygonNum].textureID = polygon.material.textureID;
                            polygons[polygonNum].CLUT      = polygon.material.clutValue;

                            polygons[polygonNum].textureUsed = polygon.material.textureUsed;
                            polygons[polygonNum].visible     = polygon.material.visible;
                            //polygons[polygonNum].materialColour = polygon.material.colour;

                            polygonNum++;
                        }
                    }

                    bool drawGreyscaleFirst = false;
                    bool quantizeBounds     = true;
                    textureFile.BuildTexturesFromPolygonData(polygons, ((GexFile)File).TPages, drawGreyscaleFirst, quantizeBounds, options);

                    // For all models
                    for (int t = 0; t < textureFile.TextureCount; t++)
                    {
                        String textureName = SRModel.GetPlayStationTextureNameDefault(File.Name, t) + TextureExtension;

                        System.IO.MemoryStream stream = textureFile.GetDataAsStream(t);
                        if (stream != null)
                        {
                            AddTexture(stream, textureName);
                        }
                        //string exportedTextureFileName = Path.ChangeExtension(textureName, "png");
                        //_TexturesAsPNGs.Add(exportedTextureFileName, textureFile.GetTextureAsBitmap(t));
                        Bitmap b = textureFile.GetTextureAsBitmap(t);
                        // this is a hack that's being done here for now because I don't know for sure which of the flags/attributes controls
                        // textures that should be alpha-masked. Alpha-masking EVERY texture is expensive.
                        if (options.AlsoInferAlphaMaskingFromTexturePixels)
                        {
                            if (BitmapHasTransparentPixels(b))
                            {
                                for (int modelNum = 0; modelNum < File.Models.Length; modelNum++)
                                {
                                    if (t < File.Models[modelNum].Materials.Length)
                                    {
                                        File.Models[modelNum].Materials[t].UseAlphaMask = true;
                                    }
                                }
                            }
                        }
                        _TexturesAsPNGs.Add(textureName, b);

                        // dump all textures as PNGs for debugging
                        //Bitmap exportedTexture = textureFile.GetTextureAsBitmap(t);
                        //string exportedTextureFileName = Path.ChangeExtension(textureName, "png");
                        //exportedTexture.Save(exportedTextureFileName, ImageFormat.Png);
                        //texNum = 0;
                        //foreach (Bitmap tex in _Textures)
                        //{
                        //    tex.Save(@"C:\Debug\Tex-" + texNum + ".png", ImageFormat.Png);
                        //    texNum++;
                        //}
                    }

                    // for models that use index/CLUT textures, if the user has enabled this option
                    if (options.UseEachUniqueTextureCLUTVariation)
                    {
                        foreach (int textureID in textureFile.TexturesByCLUT.Keys)
                        {
                            Dictionary <ushort, Bitmap> textureCLUTCollection = textureFile.TexturesByCLUT[textureID];
                            foreach (ushort clut in textureCLUTCollection.Keys)
                            {
                                String textureName            = SRModel.GetPlayStationTextureNameWithCLUT(File.Name, textureID, clut) + TextureExtension;
                                System.IO.MemoryStream stream = textureFile.GetTextureWithCLUTAsStream(textureID, clut);
                                if (stream != null)
                                {
                                    AddTexture(stream, textureName);
                                }
                                Bitmap b = textureFile.TexturesByCLUT[textureID][clut];
                                _TexturesAsPNGs.Add(textureName, b);
                            }
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("Error: couldn't find a texture file");
                }
                catch (Exception ex)
                {
                    Console.Write(ex.ToString());
                }
            }
        }