Beispiel #1
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 #2
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 #3
0
        private int GetGLTexIdFromCache(int j3dTextureId)
        {
            if (_textureCache.ContainsKey(j3dTextureId))
            {
                return(_textureCache[j3dTextureId]);
            }

            //Console.WriteLine("Generating GL texture for id: " + j3dTextureId);

            //Look up the material first.
            //_file.Materials.GetMaterialRemapTable((ushort)j3dTextureId);
            J3DFormat.MaterialInitData matData = _file.Materials.GetMaterialInitData(_file.Materials.GetMaterialRemapTable((ushort)j3dTextureId));

            //If the texture cache doesn't contain the ID, we're going to load it here.

            ushort textureIndex = matData.GetTextureIndex(0);

            if (textureIndex == 0xFF || textureIndex == 0xFFFF)
            {
                _textureCache[j3dTextureId] = 0;
                return(0);
            }
            BinaryTextureImage image = _file.Textures.GetTexture(_file.Materials.GetMaterialIndex(textureIndex));

            //image.WriteImageToFile("image_" + matChunk.GetMaterialIndex(matData.GetTextureIndex(0)) + image.Format + ".png");

            byte[] imageData   = image.GetData();
            ushort imageWidth  = image.Width;
            ushort imageHeight = image.Height;

            //Generate a black and white textureboard if the texture format is not supported.
            if (imageData.Length == 0)
            {
                imageData = new byte[]
                {
                    0, 0, 0, 255, 255, 255, 255, 255,
                    255, 255, 255, 255, 0, 0, 0, 255
                };
                imageWidth  = 2;
                imageHeight = 2;
            }


            int glTextureId;

            GL.GenTextures(1, out glTextureId);
            GL.BindTexture(TextureTarget.Texture2D, glTextureId);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, imageWidth, imageHeight, 0, PixelFormat.Bgra, PixelType.UnsignedInt8888Reversed, imageData);


            _textureCache[j3dTextureId] = glTextureId;
            return(glTextureId);
        }
Beispiel #4
0
        public Texture(string name, BinaryTextureImage compressedData)
        {
            Name           = name;
            CompressedData = compressedData;

            m_glTextureIndex = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, m_glTextureIndex);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GXToOpenGL.GetWrapMode(compressedData.WrapS));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GXToOpenGL.GetWrapMode(compressedData.WrapT));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)GXToOpenGL.GetMinFilter(compressedData.MinFilter));
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)GXToOpenGL.GetMagFilter(compressedData.MagFilter));

            // Border Color
            WLinearColor borderColor = compressedData.BorderColor;

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, new[] { borderColor.R, borderColor.G, borderColor.B, borderColor.A });

            // ToDo: Min/Mag LOD & Biases

            // Upload Image Data
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, compressedData.Width, compressedData.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, compressedData.GetData());

            // Generate Mip Maps
            if (compressedData.MipMapCount > 0)
            {
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
            }
        }
Beispiel #5
0
        public void WriteObj(string f)
        {
            //string dirPath = Path.GetDirectoryName(f);
            string fileName = $"{ AppContext.BaseDirectory }\\{ Path.GetFileNameWithoutExtension(f) }";

            StringWriter objWriter = new StringWriter();
            StringWriter mtlWriter = new StringWriter();

            objWriter.WriteLine("# dumped with booldozer");
            objWriter.WriteLine($"mtllib { fileName }.mtl");

            foreach (var vert in verticies)
            {
                objWriter.WriteLine($"v {vert.X} {vert.Y} {vert.Z}");
            }

            if (normals.Count != 0)
            {
                foreach (var vert in normals)
                {
                    objWriter.WriteLine($"vn { vert.X } { vert.Y } { vert.Z }");
                }
            }

            if (uvs.Count != 0)
            {
                foreach (var vert in uvs)
                {
                    objWriter.WriteLine($"vt { vert.X } { 1 - vert.Y }");
                }
            }

            objWriter.WriteLine();

            int index = 0;

            foreach (DrawElement drw in drawelements)
            {
                Material mat = materials[drw.matIndex];
                GXBatch  shp = shapes[drw.shapeIndex];

                mtlWriter.WriteLine($"newmtl { index }");
                mtlWriter.WriteLine($"Kd { ((mat.color & 0xFF000000) >> 24) / 255 } { ((mat.color & 0x00FF0000) >> 16) / 255 } { ((mat.color & 0x0000FF00) >> 8) / 255 }");
                mtlWriter.WriteLine($"d { (mat.color & 0x000000FF) / 255 }");

                if (mat.num_tev_stages > 0)
                {
                    TexObj texObj = texobjs[mat.stages[0].texobj_index];
                    mtlWriter.WriteLine($"map_Kd { index }.png");
                    BinaryTextureImage tex = textures[texObj.textureIndex];
                    tex.SaveImageToDisk($"{ AppContext.BaseDirectory }\\{ index }.png", tex.GetData(), tex.Width, tex.Height);
                }

                objWriter.WriteLine($"o { index }");
                objWriter.WriteLine($"usemtl { index }");

                for (int i = 0; i < shp.RawVertices.Count; i += 3)
                {
                    string[] verts = new string[3] {
                        "", "", ""
                    };

                    for (int j = 0; j < 3; j++)
                    {
                        string pos  = "";
                        string uv   = "";
                        string norm = "";

                        if (shp.ActiveAttributes.Contains(GXAttribute.Position))
                        {
                            pos = $"{ Convert.ToString(shp.RawVertices[i + j].Indices[shp.ActiveAttributes.IndexOf(GXAttribute.Position)] + 1) }/";
                        }
                        if (shp.ActiveAttributes.Contains(GXAttribute.Tex0))
                        {
                            uv = $"{ Convert.ToString(shp.RawVertices[i + j].Indices[shp.ActiveAttributes.IndexOf(GXAttribute.Tex0)] + 1) }";
                        }
                        if (shp.ActiveAttributes.Contains(GXAttribute.Normal))
                        {
                            norm = $"/{ Convert.ToString(shp.RawVertices[i + j].Indices[shp.ActiveAttributes.IndexOf(GXAttribute.Normal)] + 1) }/";
                        }

                        verts[j] = $"{ pos }{ uv }{ norm }";
                    }

                    objWriter.WriteLine($"f { verts[0] } { verts[1] } { verts[2] }");
                }

                index++;
            }

            using (FileStream s = new FileStream($"{ fileName }.obj", FileMode.Create, FileAccess.Write))
            {
                EndianBinaryWriter w = new EndianBinaryWriter(s, Endian.Big);
                w.Write(objWriter.ToString().ToCharArray());
            }

            using (FileStream s = new FileStream($"{ fileName }.mtl", FileMode.Create, FileAccess.Write))
            {
                EndianBinaryWriter w = new EndianBinaryWriter(s, Endian.Big);
                w.Write(mtlWriter.ToString().ToCharArray());
            }
        }