SIlkroad Model Converter + View Sample [C#]

11/01/2009 14:54 chea77er#1
I am finished with my C# BMS Converter:
Code:
    class BMSConverter
    {
        int charCount;
        int vertCount;
        int boneCount;
        int faceCount;
        string mtlLib;
        string meshGroup;
        float[,] vertices;
        float[,] normals;
        float[,] textures;
        //char[,] bones;
        short[,] faces;

        #region Calculate
        public void Calculate(string file)
        {
            BinaryReader br = new BinaryReader(File.Open(file, FileMode.Open));

            string filetype = new string(br.ReadChars(8)); // Filetype ( first 9 Bytes)
            string version = new string(br.ReadChars(4));  // 110

            if (version.Contains("110"))
            {
                // Springe zur position 72 ( Nicht benötigte Header Informationen )
                br.BaseStream.Position += 60;

                // Länge des MeshGroup Namens
                charCount = br.ReadInt32();

                // Lese Namen
                meshGroup = new string(br.ReadChars(charCount));

                // Länge des Materiels Namen
                charCount = br.ReadInt32();

                // Lese  Material Namen
                mtlLib = new string(br.ReadChars(charCount));

                // Ignoierere Unbekannten Wert ( Int32 = 4bytes ) ?
                br.BaseStream.Position += 4;

                // Lese anzahl der Verticen
                vertCount = br.ReadInt32();

                // Erstelle Arrays
                vertices = new float[vertCount, 3];
                normals = new float[vertCount, 3];
                textures = new float[vertCount, 2];

                // Lese Werte
                for (int i = 0; i < vertCount; i++)
                {
                    // Vertices
                    for (int j = 0; j < 3; j++)
                    {
                        vertices[i,j] = br.ReadSingle();
                    }
                    // Normal
                    for (int j = 0; j < 3; j++)
                    {
                        normals[i, j] = br.ReadSingle();
                    }
                    // Textures
                    for (int j = 0; j < 2; j++)
                    {
                        textures[i, j] = br.ReadSingle();
                    }

                    // Ignoiere 12 Bytes pro Vertices ??
                    br.BaseStream.Position += 12;
                }

                // Lese Bones/.. (Ignoiere Bones)
                boneCount = br.ReadInt32();

                if (boneCount > 0)
                {
                    for (int i = 0; i < boneCount; i++)
                    {
                        // b
                        charCount = br.ReadInt32();
                        
                        // Überspringe
                        br.BaseStream.Position += charCount;
                    }
                    br.BaseStream.Position += vertCount * 6; // Unnötiger Gaymax schrott
                }

                // Lese Faces
                faceCount = br.ReadInt32();
                faces = new short[faceCount, 3];

                for (int i = 0; i < faceCount; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        faces[i, j] = br.ReadInt16();
                    }
                }

                // Schließe Reader
                br.Close();
            }
            else
            {
                Console.WriteLine("Invaild Version: {0}", version);
            }
        }
        #endregion

        #region WriteOBJ
        public void WriteOBJ(string output)
        {
            List<string> l_List = new List<string>();
            string line;

            // # = Kommentar
            line = "# ~ Converted by BMSConverter.NET ~";
            l_List.Add(line);
            line = "# ~ Joymax Engine BMS Version: 110 ~";
            l_List.Add(line);

            // Schreibe Vertices
            for (int i = 0; i < vertCount; i++)
            {
                line = "v " + vertices[i, 0] + " " + vertices[i, 1] + " " + vertices[i, 2];
                l_List.Add(line);
            }

            // ... normals
            for (int i = 0; i < vertCount; i++)
            {
                line = "vn " + normals[i, 0] + " " + normals[i, 1] + " " + normals[i, 2];
                l_List.Add(line);
            }
            // Schreibe Texture Vertice
            for (int i = 0; i < vertCount; i++)
            {
                line = "vt " + textures[i, 0] + " " + textures[i, 1];
                l_List.Add(line);
            }


            line = "# " + vertCount + " vertices\n";
            l_List.Add(line);

            line = "g " + meshGroup;
            l_List.Add(line);

            // Faces
            for (int i = 0; i < faceCount; i++)
            {
                line = "f " + (faces[i, 0] + 1) + "/" + (faces[i, 0] + 1) + "/" + (faces[i, 0] + 1) + " " +
                    (faces[i, 1] + 1) + "/" + (faces[i, 1] + 1) + "/" + (faces[i, 1] + 1) + " " +
                    (faces[i, 2] + 1) + "/" + (faces[i, 2] + 1) + "/" + (faces[i, 2] + 1);

                l_List.Add(line);
            }

            for (int i = 0; i < l_List.Count; i++)
            {
                // Convert all , to .
                l_List[i] = l_List[i].Replace(',', '.');
            }
            File.WriteAllLines(output, l_List.ToArray());
        }
        #endregion
    }
It convert a Silkroad MOdel File to an Wavefront OBJ file
You can view OBJ file with Blender, or with this XNA Sample

XNA Sample: [Only registered and activated users can see links. Click Here To Register...]

You can find Models and Textures here:
Data.pk2/prim/mesh/ = MOdels
Data.pk2/prim/mtrl/ = Textures

To Convert a ddj file you need a HexEditor like XVI32
Then delete the first 20bytes and then save as *.dds
DDS can converted by "DDS Converter 2"

Many Models are saved in more then 1 *.bms file
[Only registered and activated users can see links. Click Here To Register...]
11/02/2009 15:41 moderiz11#2
And obj back to silkroad format?
11/02/2009 16:31 chea77er#3
I will try to code an OBJ to BMS today in the evening
11/04/2009 14:45 moderiz11#4
New weapons mods here we come
11/04/2009 15:39 soadmania#5
Awesome work :)
11/04/2009 15:52 Nezekan#6
OBJ => BMS would be awesome, after all these years...
11/06/2009 11:10 hiho3#7
Quote:
Originally Posted by chea77er View Post
To Convert a ddj file you need a HexEditor like XVI32
Then delete the first 20bytes and then save as *.dds
DDS can converted by "DDS Converter 2"
its better and faster too use this tool:

[Only registered and activated users can see links. Click Here To Register...]


its not from me this converter
credits go to:

Code:
Created by Jan Chlpik 2008
Downloaded from Silkroad.filefront.com
Open discusion at www.silkroadforums.com
12/31/2009 15:36 addonman#8
thank you my friend
06/23/2010 23:26 playerh007#9
chea77er is it possible that you try to code BMS=>OBJ

that would be great

(sry i know that this threat is very old but i need it)
06/24/2010 00:31 flatro21#10
Ohh so good word gz dude.
06/29/2010 10:00 chea77er#11
sry guys, i got some request for OBJ to BMS. But there are some informations which i don´´t know what it is. In my code you can see that there are many bytes which aren´t converted.. so you can try to make OBJ=>BMS without this infos just replace the jumped bytes with 00 or something. don´t know if thats work.

I have no time to check this.
07/02/2010 20:54 jhondelle#12
thanks.. too ^^
11/25/2010 23:24 darky666#13
i dont know if anny1 is still watching this tread

but if you want to experiment with this you will have to collect the garbage
(just google it garbage collect C#)

because if you start the vieuwer it uses up all your memory :p then it crashes

other then the memory usage its a nice program
11/26/2010 00:23 bootdisk#14
Garbage collectors run in background, the only platform in which it was needed to force a call was at j2me.

Debug the code and I'm sure there must be an infinite loop, as perhaps, most of the structures have changed from chea77er release to today's silkroad version.