Beispiel #1
0
        private void LoadTexturesFromScene(Assimp.Scene scene, string model_directory, Arguments cmdargs)
        {
            foreach (Assimp.Mesh mesh in scene.Meshes)
            {
                Console.Write(mesh.Name);
                Assimp.Material mat = scene.Materials[mesh.MaterialIndex];

                if (mat.HasTextureDiffuse)
                {
                    string texname       = System.IO.Path.GetFileNameWithoutExtension(mat.TextureDiffuse.FilePath);
                    bool   isEmbedded    = false;
                    int    embeddedIndex = -1;

                    if (mat.TextureDiffuse.FilePath.StartsWith("*"))
                    {
                        string index = mat.TextureDiffuse.FilePath.Substring(1, mat.TextureDiffuse.FilePath.Length);;
                        isEmbedded = int.TryParse(index, out embeddedIndex);
                        texname    = String.Format("embedded_tex{0}", embeddedIndex);
                    }

                    bool already_exists = false;

                    foreach (BinaryTextureImage image in Textures)
                    {
                        if (image.Name == texname)
                        {
                            already_exists = true;
                            break;
                        }
                    }

                    if (already_exists)
                    {
                        continue;
                    }

                    BinaryTextureImage img = new BinaryTextureImage();

                    if (isEmbedded)
                    {
                        Assimp.EmbeddedTexture embeddedTexture = scene.Textures[embeddedIndex];
                        img.Load(mat.TextureDiffuse, embeddedTexture);
                    }
                    else
                    {
                        img.Load(mat.TextureDiffuse, model_directory, cmdargs.readMipmaps);
                    }
                    Textures.Add(img);
                }
                else
                {
                    Console.WriteLine(" -> Has No Textures");
                }
            }
        }
Beispiel #2
0
        public TEX1(EndianBinaryReader reader, int offset)
        {
            Textures = new List <BinaryTextureImage>();

            reader.BaseStream.Seek(offset, System.IO.SeekOrigin.Begin);
            reader.SkipInt32();
            int   tex1Size = reader.ReadInt32();
            short texCount = reader.ReadInt16();

            reader.SkipInt16();

            int textureHeaderOffset    = reader.ReadInt32();
            int textureNameTableOffset = reader.ReadInt32();

            List <string> names = NameTableIO.Load(reader, offset + textureNameTableOffset);

            reader.BaseStream.Seek(textureHeaderOffset + offset, System.IO.SeekOrigin.Begin);

            for (int i = 0; i < texCount; i++)
            {
                reader.BaseStream.Seek((offset + 0x20 + (0x20 * i)), System.IO.SeekOrigin.Begin);

                BinaryTextureImage img = new BinaryTextureImage(names[i]);
                img.Load(reader, (offset + 0x20 + (0x20 * i)));
                Textures.Add(img);
            }
        }
Beispiel #3
0
        private static List<Texture2D> LoadTEX1FromFile(EndianBinaryReader reader, long chunkStart)
        {
            ushort textureCount = reader.ReadUInt16();
            ushort padding = reader.ReadUInt16(); // Usually 0xFFFF?
            uint textureHeaderOffset = reader.ReadUInt32(); // textureCount # bti image headers are stored here, relative to chunkStart.
            uint stringTableOffset = reader.ReadUInt32(); // One filename per texture. relative to chunkStart.
            List<Texture2D> textureList = new List<Texture2D>();

            // Get all Texture Names
            reader.BaseStream.Position = chunkStart + stringTableOffset;
            StringTable stringTable = StringTable.FromStream(reader);

            for (int t = 0; t < textureCount; t++)
            {
                // 0x20 is the length of the BinaryTextureImage header which all come in a row, but then the stream gets jumped around while loading the BTI file.
                reader.BaseStream.Position = chunkStart + textureHeaderOffset + (t * 0x20);
                BinaryTextureImage texture = new BinaryTextureImage();
                texture.Load(reader, chunkStart + 0x20, t);

                Texture2D texture2D = new Texture2D(texture.Width, texture.Height);
                texture2D.Name = stringTable[t];
                texture2D.PixelData = texture.GetData();
                textureList.Add(texture2D);

                string executionPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                texture.SaveImageToDisk(executionPath + "/TextureDump/" + string.Format("{0}_({1}-{2}).png", stringTable[t], texture.Format, t));
            }

            return textureList;
        }
Beispiel #4
0
        private static List <Texture2D> LoadTEX1FromFile(EndianBinaryReader reader, long chunkStart)
        {
            ushort           textureCount        = reader.ReadUInt16();
            ushort           padding             = reader.ReadUInt16(); // Usually 0xFFFF?
            uint             textureHeaderOffset = reader.ReadUInt32(); // textureCount # bti image headers are stored here, relative to chunkStart.
            uint             stringTableOffset   = reader.ReadUInt32(); // One filename per texture. relative to chunkStart.
            List <Texture2D> textureList         = new List <Texture2D>();

            // Get all Texture Names
            reader.BaseStream.Position = chunkStart + stringTableOffset;
            StringTable stringTable = StringTable.FromStream(reader);

            for (int t = 0; t < textureCount; t++)
            {
                // 0x20 is the length of the BinaryTextureImage header which all come in a row, but then the stream gets jumped around while loading the BTI file.
                reader.BaseStream.Position = chunkStart + textureHeaderOffset + (t * 0x20);
                BinaryTextureImage texture = new BinaryTextureImage();
                texture.Load(reader, chunkStart + 0x20, t);

                Texture2D texture2D = new Texture2D(texture.Width, texture.Height);
                texture2D.Name      = stringTable[t];
                texture2D.PixelData = texture.GetData();
                textureList.Add(texture2D);


                string executionPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                texture.SaveImageToDisk(executionPath + "/TextureDump/" + string.Format("{0}_({1}-{2}).png", stringTable[t], texture.Format, t));
            }

            return(textureList);
        }
Beispiel #5
0
        public void AddTextureFromPath(string path, bool readMipmaps)
        {
            string             modelDirectory = System.IO.Path.GetDirectoryName(path);
            BinaryTextureImage img            = new BinaryTextureImage();

            // Only the path and the wrap mode are relevant, the rest doesn't matter for img.Load
            TextureSlot tex = new TextureSlot(path, 0, 0, 0, 0, (float)0.0, 0, TextureWrapMode.Clamp, TextureWrapMode.Clamp, 0);

            img.Load(tex, modelDirectory, readMipmaps);

            Textures.Add(img);
        }
Beispiel #6
0
        private void LoadTexturesFromScene(Assimp.Scene scene, string model_directory)
        {
            foreach (Assimp.Mesh mesh in scene.Meshes)
            {
                Assimp.Material mat = scene.Materials[mesh.MaterialIndex];

                if (mat.HasTextureDiffuse)
                {
                    BinaryTextureImage img = new BinaryTextureImage();
                    img.Load(mat.TextureDiffuse, model_directory);
                    Textures.Add(img);
                }
            }
        }
Beispiel #7
0
        public TEX1(Assimp.Scene scene, string modelDirectory)
        {
            Textures = new List <BinaryTextureImage>();

            foreach (Assimp.Material mat in scene.Materials)
            {
                if (mat.HasTextureDiffuse)
                {
                    BinaryTextureImage img = new BinaryTextureImage();
                    img.Load(mat.TextureDiffuse, modelDirectory);
                    Textures.Add(img);
                }
            }
        }
Beispiel #8
0
        private ObservableCollection <BinaryTextureImage> LoadImages(string[] paths)
        {
            if (ImageList == null)
            {
                ImageList = new ObservableCollection <BinaryTextureImage>();
            }

            ObservableCollection <BinaryTextureImage> tempList = ImageList;

            foreach (string path in paths)
            {
                BinaryTextureImage openedImage = null;

                // Open a BTI
                if (path.EndsWith(".bti"))
                {
                    using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        EndianBinaryReader reader = new EndianBinaryReader(stream, Endian.Big);

                        openedImage = new BinaryTextureImage();

                        openedImage.Load(reader, path, 0);
                    }
                }
                // Open a PNG
                else if (path.EndsWith(".png") || path.EndsWith(".PNG"))
                {
                    Bitmap input = new Bitmap(path);

                    openedImage = new BinaryTextureImage(path, input, BinaryTextureImage.TextureFormats.PNG);
                }
                // Open a TGA
                else if (path.EndsWith(".tga"))
                {
                    Bitmap input = TgaReader.Load(path);

                    openedImage = new BinaryTextureImage(path, input, BinaryTextureImage.TextureFormats.TGA);
                }

                tempList.Add(openedImage);
            }

            return(tempList);
        }
Beispiel #9
0
        public void LoadTEX1FromStream(EndianBinaryReader reader, long tagStart, bool dumpTextures)
        {
            ushort numTextures = reader.ReadUInt16();

            Trace.Assert(reader.ReadUInt16() == 0xFFFF); // Padding

            int textureHeaderDataOffset = reader.ReadInt32();
            int stringTableOffset       = reader.ReadInt32();

            // Texture Names
            reader.BaseStream.Position = tagStart + stringTableOffset;
            StringTable nameTable = StringTable.FromStream(reader);

            Textures = new BindingList <Texture>();
            for (int t = 0; t < numTextures; t++)
            {
                // Reset the stream position to the start of this header as loading the actual data of the texture
                // moves the stream head around.
                reader.BaseStream.Position = tagStart + textureHeaderDataOffset + (t * 0x20);

                BinaryTextureImage compressedTex = new BinaryTextureImage();
                string             tempFileName  = string.Format("TextureDump/{1}_{0}.png", nameTable[t], t);
                if (!m_allowTextureCache || !File.Exists(tempFileName))
                {
                    compressedTex.Load(reader, tagStart + 0x20 /* Size of TEX1 header?*/, t);
                }
                else
                {
                    compressedTex.LoadImageFromDisk(tempFileName);
                }

                Texture texture = new Texture(nameTable[t], compressedTex);
                Textures.Add(texture);

                if (dumpTextures)
                {
                    compressedTex.SaveImageToDisk(string.Format("TextureDump/{2}__{0}_{1}_{3}.png", texture.Name, compressedTex.Format, t, compressedTex.PaletteFormat));
                }
            }
        }
Beispiel #10
0
        public MdlModel(string path)
        {
            m_Counts          = new ushort[20];
            m_Offsets         = new long[18];
            shapes            = new List <GXBatch>();
            globalMatrixTable = new List <Matrix4>();
            textures          = new List <BinaryTextureImage>();

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                EndianBinaryReader stream = new EndianBinaryReader(fs, Endian.Big);
                stream.ReadInt32();                 //ignore the magic
                for (int i = 0; i < 20; i++)
                {
                    m_Counts[i] = stream.ReadUInt16();
                }
                stream.BaseStream.Seek(0x30, 0);
                for (int i = 0; i < 18; i++)
                {
                    m_Offsets[i] = stream.ReadUInt32();
                }

                verticies    = fromVec3(LoadSection <vec3>(stream, 6, 6));
                normals      = fromVec3(LoadSection <vec3>(stream, 7, 7));
                uvs          = fromVec2(LoadSection <vec2>(stream, 9, 9));
                drawelements = LoadSection <DrawElement>(stream, 17, 17);
                shapepackets = LoadSection <ShapePacket>(stream, 1, 3);
                materials    = LoadSection <Material>(stream, 14, 18);
                texobjs      = LoadSection <TexObj>(stream, 15, 16);

                stream.BaseStream.Seek(m_Offsets[2], 0);
                for (int i = 0; i < m_Counts[5]; i++)
                {
                    Matrix4 mat = new Matrix4(
                        new Vector4(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()),
                        new Vector4(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()),
                        new Vector4(stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle(), stream.ReadSingle()),
                        new Vector4(0, 0, 0, 1)
                        );
                    globalMatrixTable.Add(mat.Inverted());
                }
                for (int i = 0; i < m_Counts[4]; i++)
                {
                    globalMatrixTable.Add(Matrix4.Identity);
                }

                stream.BaseStream.Seek(m_Offsets[16], 0);
                for (int i = 0; i < m_Counts[19]; i++)
                {
                    GXBatch bat = new GXBatch();
                    bat.LoadMdlBatch(stream, shapepackets, globalMatrixTable, verticies);
                    shapes.Add(bat);
                }

                /*
                 * foreach (var packet in shapepackets)
                 * {
                 *      //create local matrix table for shape packet
                 *      Matrix4[] localMats = new Matrix4[packet.numMatIndicies];
                 *      for (int i = 0; i < packet.numMatIndicies; i++)
                 *      {
                 *              if (packet.matIndicies[i] != 0xFFFF)
                 *              {
                 *                      localMats[i] = globalMatrixTable[packet.matIndicies[i]];
                 *              } else {break;}
                 *      }
                 *      //apply to shapes
                 *      foreach (GXBatch shape in shapes)
                 *      {
                 *              if (shape.ActiveAttributes.Contains(GXAttribute.PositionMatrixIndex))
                 *              {
                 *                      foreach (var vert in shape.RawVertices)
                 *                      {
                 *                              var matIndex = vert.Indices[shape.ActiveAttributes.IndexOf(GXAttribute.PositionMatrixIndex)];
                 *                              Console.WriteLine($"Mat Index: {matIndex}\nLocal Matrix List Size: {localMats.Length}");
                 *                              if(shape.ActiveAttributes.Contains(GXAttribute.PositionMatrixIndex)){
                 *                                      Matrix4 mat = localMats[matIndex];
                 *                                      Vector4 pos = new Vector4(verticies[vert.Indices[shape.ActiveAttributes.IndexOf(GXAttribute.Position)]]);
                 *                                      Vector4.Transform(pos, mat);
                 *                                      verticies[vert.Indices[shape.ActiveAttributes.IndexOf(GXAttribute.Position)]] = new Vector3(pos);
                 *                              }
                 *                      }
                 *              }
                 *      }
                 * }*/

                stream.BaseStream.Seek(m_Offsets[12], SeekOrigin.Begin);
                for (int i = 0; i < m_Counts[14]; i++)
                {
                    int  textureOffset = stream.ReadInt32();
                    long nextOffsetPos = stream.BaseStream.Position;

                    stream.BaseStream.Seek(textureOffset, SeekOrigin.Begin);
                    BinaryTextureImage img = new BinaryTextureImage();
                    img.Load(stream, textureOffset, 1);
                    textures.Add(img);

                    stream.BaseStream.Seek(nextOffsetPos, SeekOrigin.Begin);
                }

                WriteObj("mdlTest.obj");
            }
        }