OSDN Git Service

refactored. GetHashCode
[tdcgexplorer/tso2mqo.git] / TSOFile.cs
index 8e1acaa..f0ec1cc 100644 (file)
@@ -3,8 +3,6 @@ using System.Collections.Generic;
 using System.IO;\r
 using System.Text;\r
 using System.ComponentModel;\r
-using Microsoft.DirectX;\r
-using Microsoft.DirectX.Direct3D;\r
 \r
 namespace Tso2MqoGui\r
 {\r
@@ -32,6 +30,16 @@ namespace Tso2MqoGui
             System.Diagnostics.Debug.WriteLine(s);\r
         }\r
 \r
+        public static void ExchangeChannel(byte[] data, int depth)\r
+        {\r
+            for(int j= 0; j < data.Length; j+=depth)\r
+            {\r
+                byte    tmp = data[j+2];\r
+                data[j+2]   = data[j+0];\r
+                data[j+0]   = tmp;\r
+            }\r
+        }\r
+\r
         public void ReadAll()\r
         {\r
             byte[]  magic                   = r.ReadBytes(4);\r
@@ -100,12 +108,7 @@ namespace Tso2MqoGui
                 textures[i].data            = r.ReadBytes(textures[i].width * textures[i].height * textures[i].depth);\r
                 texturemap.Add(textures[i].name, textures[i]);\r
 \r
-                for(int j= 0; j < textures[i].data.Length; j+=4)\r
-                {\r
-                    byte    tmp          = textures[i].data[j+2];\r
-                    textures[i].data[j+2]= textures[i].data[j+0];\r
-                    textures[i].data[j+0]= tmp;\r
-                }\r
+                ExchangeChannel(textures[i].data, textures[i].depth);\r
 \r
                 WriteLine(r.BaseStream.Position.ToString("X"));\r
             }\r
@@ -155,9 +158,6 @@ namespace Tso2MqoGui
             count                           = r.ReadInt32();\r
             meshes                          = new TSOMesh[count];\r
             int             check           = 0;\r
-#if true\r
-            bool    debug   = false;\r
-#endif\r
             for(int i= 0; i < count; ++i)\r
             {\r
                 meshes[i]                   = new TSOMesh();\r
@@ -188,22 +188,11 @@ namespace Tso2MqoGui
 \r
                     for(int k= 0; k < meshes[i].sub[j].numvertices; ++k)\r
                     {\r
-                        if(debug)\r
-                        {\r
-                            WriteLine(r.BaseStream.Position.ToString("X"));\r
-                            ReadVertexDebug(ref v[k]);\r
-                        } else\r
-                        {\r
-                            ReadVertex(ref v[k]);\r
-                        }\r
+                        ReadVertex(ref v[k]);\r
                     }\r
 \r
                     WriteLine(r.BaseStream.Position.ToString("X"));\r
                     System.Diagnostics.Debug.WriteLine(r.BaseStream.Position.ToString("X"));\r
-#if DEBUG\r
-                    if(r.BaseStream.Position == 0x61F94)\r
-                        debug   = true;\r
-#endif\r
                 }\r
             }\r
 \r
@@ -219,18 +208,16 @@ namespace Tso2MqoGui
         internal int            id;\r
         internal string         name;\r
         internal string         sname;\r
-        internal Matrix matrix;\r
-        internal Matrix world;\r
+        internal Matrix44       matrix;\r
+        internal Matrix44       world;\r
         internal List<TSONode>  children    = new List<TSONode>();\r
         internal TSONode        parent;\r
 \r
         [Category("General")] public int      ID        { get { return id; } }\r
         [Category("General")] public string   Name      { get { return name; } }\r
         [Category("General")] public string   ShortName { get { return sname; } }\r
-        [Category("Detail")]\r
-        public Matrix Matrix { get { return matrix; } set { matrix = value; } }\r
-        [Category("Detail")]\r
-        public Matrix World { get { return world; } set { world = value; } }\r
+        [Category("Detail")]  public Matrix44 Matrix    { get { return matrix; } set { matrix= value; } }\r
+        [Category("Detail")]  public Matrix44 World     { get { return world;  } set { world = value; } }\r
 \r
         public override string ToString()\r
         {\r
@@ -322,7 +309,6 @@ namespace Tso2MqoGui
 \r
     public class TSOMaterialCode : Dictionary<string, TSOParameter>\r
     {\r
-\r
         public TSOMaterialCode(string code)\r
             : this(code.Split('\r', '\n'))\r
         {\r
@@ -338,7 +324,7 @@ namespace Tso2MqoGui
             TSOParameter    p   = this[index];\r
             p.Value             = value;\r
            }\r
-       \r
+\r
         public TSOMaterialCode(string[] code)\r
         {\r
             foreach(string i in code)\r
@@ -385,12 +371,12 @@ namespace Tso2MqoGui
         internal float      lightDirY;       // = [-0.0582338]\r
         internal float      lightDirZ;       // = [-0.998302]\r
         internal float      lightDirW;       // = [0]\r
-        internal Vector4 shadowColor;     // = [0, 0, 0, 1]\r
+        internal Point4     shadowColor;     // = [0, 0, 0, 1]\r
         internal string     shadeTex;        // = Ninjya_Ribbon_Toon_Tex\r
         internal float      highLight;       // = [0]\r
         internal float      colorBlend;      // = [10]\r
         internal float      highLightBlend;  // = [10]\r
-        internal Vector4 penColor;        // = [0.166, 0.166, 0.166, 1]\r
+        internal Point4     penColor;        // = [0.166, 0.166, 0.166, 1]\r
         internal float      ambient;         // = [38]\r
         internal string     colorTex;        // = file24\r
         internal float      thickness;       // = [0.018]\r
@@ -409,14 +395,12 @@ namespace Tso2MqoGui
         [Category("Parameters")] public float       LightDirY          { get { return lightDirY;      } set { lightDirY     = value; } }\r
         [Category("Parameters")] public float       LightDirZ          { get { return lightDirZ;      } set { lightDirZ     = value; } }\r
         [Category("Parameters")] public float       LightDirW          { get { return lightDirW;      } set { lightDirW     = value; } }\r
-        [Category("Parameters")]\r
-        public Vector4 ShadowColor { get { return shadowColor; } set { shadowColor = value; } }\r
+        [Category("Parameters")] public Point4      ShadowColor     { get { return shadowColor;    } set { shadowColor   = value; } }\r
         [Category("Parameters")] public string      ShadeTex           { get { return shadeTex;       } set { shadeTex      = value; } }\r
         [Category("Parameters")] public float       HighLight          { get { return highLight;      } set { highLight     = value; } }\r
         [Category("Parameters")] public float       ColorBlend         { get { return colorBlend;     } set { colorBlend    = value; } }\r
         [Category("Parameters")] public float       HighLightBlend     { get { return highLightBlend; } set { highLightBlend= value; } }\r
-        [Category("Parameters")]\r
-        public Vector4 PenColor { get { return penColor; } set { penColor = value; } }\r
+        [Category("Parameters")] public Point4      PenColor           { get { return penColor;       } set { penColor      = value; } }\r
         [Category("Parameters")] public float       Ambient                { get { return ambient;        } set { ambient       = value; } }\r
         [Category("Parameters")] public string      ColorTex           { get { return colorTex;       } set { colorTex      = value; } }\r
         [Category("Parameters")] public float       Thickness          { get { return thickness;      } set { thickness     = value; } }\r
@@ -485,10 +469,10 @@ namespace Tso2MqoGui
             return float.Parse(value.Trim('[', ']', ' '));\r
         }\r
 \r
-        public Vector4 GetPoint4(string value)\r
+        public Point4   GetPoint4(string value)\r
         {\r
             string[]    token   = value.Trim('[', ']', ' ').Split(',');\r
-            Vector4 p;\r
+            Point4      p       = new Point4();\r
             p.X                 = float.Parse(token[0].Trim());\r
             p.Y                 = float.Parse(token[1].Trim());\r
             p.Z                 = float.Parse(token[2].Trim());\r
@@ -501,15 +485,14 @@ namespace Tso2MqoGui
     {\r
         internal TSOFile        file;\r
         internal string         name;\r
-        internal Matrix matrix;\r
+        internal Matrix44       matrix;\r
         internal int            effect;\r
         internal int            numsubs;\r
         internal TSOSubMesh[]   sub;\r
 \r
         [Category("General")]   public string   Name    { get { return name; } set { name= value; } }\r
       //[Category("Detail")]    public int      Effect  { get { return name; } set { name= value; } }\r
-        [Category("Detail")]\r
-        public Matrix Matrix { get { return matrix; } set { matrix = value; } }\r
+        [Category("Detail")]    public Matrix44 Matrix  { get { return matrix; } set { matrix= value; } }\r
 \r
         public override string ToString()\r
         {\r
@@ -547,17 +530,95 @@ namespace Tso2MqoGui
         }\r
     }\r
 \r
+    public struct Matrix44\r
+    {\r
+        public static readonly Matrix44 Identity    = new Matrix44(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);\r
+\r
+        public float m11, m12, m13, m14;\r
+        public float m21, m22, m23, m24;\r
+        public float m31, m32, m33, m34;\r
+        public float m41, m42, m43, m44;\r
+\r
+        public float M11 { get { return m11; } set { m11= value; } }\r
+        public float M12 { get { return m12; } set { m12= value; } }\r
+        public float M13 { get { return m13; } set { m13= value; } }\r
+        public float M14 { get { return m14; } set { m14= value; } }\r
+        public float M21 { get { return m21; } set { m21= value; } }\r
+        public float M22 { get { return m22; } set { m22= value; } }\r
+        public float M23 { get { return m23; } set { m23= value; } }\r
+        public float M24 { get { return m24; } set { m24= value; } }\r
+        public float M31 { get { return m31; } set { m31= value; } }\r
+        public float M32 { get { return m32; } set { m32= value; } }\r
+        public float M33 { get { return m33; } set { m33= value; } }\r
+        public float M34 { get { return m34; } set { m34= value; } }\r
+        public float M41 { get { return m41; } set { m41= value; } }\r
+        public float M42 { get { return m42; } set { m42= value; } }\r
+        public float M43 { get { return m43; } set { m43= value; } }\r
+        public float M44 { get { return m44; } set { m44= value; } }\r
+\r
+        public Matrix44(\r
+            float a11, float a12, float a13, float a14,\r
+            float a21, float a22, float a23, float a24,\r
+            float a31, float a32, float a33, float a34,\r
+            float a41, float a42, float a43, float a44)\r
+        {\r
+            m11=a11; m12=a12; m13=a13; m14=a14;\r
+            m21=a21; m22=a22; m23=a23; m24=a24;\r
+            m31=a31; m32=a32; m33=a33; m34=a34;\r
+            m41=a41; m42=a42; m43=a43; m44=a44;\r
+        }\r
+\r
+        public Point3 Translation   { get { return new Point3(M41, M42, M43); } } \r
+\r
+        public override string ToString()\r
+        {\r
+            StringBuilder   sb  = new StringBuilder();\r
+            sb.AppendFormat("[{0:F4}, {1:F4}, {2:F4}, {3:F4}], ",  M11, M12, M13, M14)\r
+              .AppendFormat("[{0:F4}, {1:F4}, {2:F4}, {3:F4}], ",  M21, M22, M23, M24)\r
+              .AppendFormat("[{0:F4}, {1:F4}, {2:F4}, {3:F4}], ",  M31, M32, M33, M34)\r
+              .AppendFormat("[{0:F4}, {1:F4}, {2:F4}, {3:F4}]",    M41, M42, M43, M44);\r
+            return sb.ToString();\r
+        }\r
+\r
+        public static Matrix44 Mul(Matrix44 a, Matrix44 b)\r
+        {\r
+            Matrix44    m   = new Matrix44();\r
+\r
+            m.M11   = a.M11*b.M11 + a.M12*b.M21 + a.M13*b.M31 + a.M14*b.M41;\r
+            m.M12   = a.M11*b.M12 + a.M12*b.M22 + a.M13*b.M32 + a.M14*b.M42;\r
+            m.M13   = a.M11*b.M13 + a.M12*b.M23 + a.M13*b.M33 + a.M14*b.M43;\r
+            m.M14   = a.M11*b.M14 + a.M12*b.M24 + a.M13*b.M34 + a.M14*b.M44;\r
+\r
+            m.M21   = a.M21*b.M11 + a.M22*b.M21 + a.M23*b.M31 + a.M24*b.M41;\r
+            m.M22   = a.M21*b.M12 + a.M22*b.M22 + a.M23*b.M32 + a.M24*b.M42;\r
+            m.M23   = a.M21*b.M13 + a.M22*b.M23 + a.M23*b.M33 + a.M24*b.M43;\r
+            m.M24   = a.M21*b.M14 + a.M22*b.M24 + a.M23*b.M34 + a.M24*b.M44;\r
+\r
+            m.M31   = a.M31*b.M11 + a.M32*b.M21 + a.M33*b.M31 + a.M34*b.M41;\r
+            m.M32   = a.M31*b.M12 + a.M32*b.M22 + a.M33*b.M32 + a.M34*b.M42;\r
+            m.M33   = a.M31*b.M13 + a.M32*b.M23 + a.M33*b.M33 + a.M34*b.M43;\r
+            m.M34   = a.M31*b.M14 + a.M32*b.M24 + a.M33*b.M34 + a.M34*b.M44;\r
+\r
+            m.M41   = a.M41*b.M11 + a.M42*b.M21 + a.M43*b.M31 + a.M44*b.M41;\r
+            m.M42   = a.M41*b.M12 + a.M42*b.M22 + a.M43*b.M32 + a.M44*b.M42;\r
+            m.M43   = a.M41*b.M13 + a.M42*b.M23 + a.M43*b.M33 + a.M44*b.M43;\r
+            m.M44   = a.M41*b.M14 + a.M42*b.M24 + a.M43*b.M34 + a.M44*b.M44;\r
+\r
+            return m;\r
+        }\r
+    }\r
+\r
     public partial struct Vertex : IComparable<Vertex>\r
     {\r
-        public Vector3 Pos;\r
-        public Vector4 Wgt;\r
+        public Point3       Pos;\r
+        public Point4       Wgt;\r
         public UInt32       Idx;\r
-        public Vector3 Nrm;\r
-        public Vector2 Tex;\r
+        public Point3       Nrm;\r
+        public Point2       Tex;\r
       //public int          Count;\r
       //public Weights[]    Weights;\r
 \r
-        public Vertex(Vector3 pos, Vector4 wgt, UInt32 idx, Vector3 nrm, Vector2 tex)\r
+        public Vertex(Point3 pos, Point4 wgt, UInt32 idx, Point3 nrm, Point2 tex)\r
         {\r
             Pos = pos;\r
             Wgt = wgt;\r
@@ -568,39 +629,29 @@ namespace Tso2MqoGui
 \r
         public int CompareTo(Vertex o)\r
         {\r
-            if(Pos.X < o.Pos.X) return -1; if(Pos.X > o.Pos.X) return 1;\r
-            if(Pos.Y < o.Pos.Y) return -1; if(Pos.Y > o.Pos.Y) return 1;\r
-            if(Pos.Z < o.Pos.Z) return -1; if(Pos.Z > o.Pos.Z) return 1;\r
-            if(Nrm.X < o.Nrm.X) return -1; if(Nrm.X > o.Nrm.X) return 1;\r
-            if(Nrm.Y < o.Nrm.Y) return -1; if(Nrm.Y > o.Nrm.Y) return 1;\r
-            if(Nrm.Z < o.Nrm.Z) return -1; if(Nrm.Z > o.Nrm.Z) return 1;\r
-            if(Tex.X < o.Tex.X) return -1; if(Tex.X > o.Tex.X) return 1;\r
-            if(Tex.Y < o.Tex.Y) return -1; if(Tex.Y > o.Tex.Y) return 1;\r
-            if(Wgt.X < o.Wgt.X) return -1; if(Wgt.X > o.Wgt.X) return 1;\r
-            if(Wgt.Y < o.Wgt.Y) return -1; if(Wgt.Y > o.Wgt.Y) return 1;\r
-            if(Wgt.Z < o.Wgt.Z) return -1; if(Wgt.Z > o.Wgt.Z) return 1;\r
-            if(Wgt.W < o.Wgt.W) return -1; if(Wgt.W > o.Wgt.W) return 1;\r
-            if(Idx   < o.Idx)   return -1; if(Idx   > o.Idx)   return 1;\r
-            return 0;\r
+            int cmp = 0;\r
+            cmp = Pos.CompareTo(o.Pos); if (cmp != 0) return cmp;\r
+            cmp = Nrm.CompareTo(o.Nrm); if (cmp != 0) return cmp;\r
+            cmp = Tex.CompareTo(o.Tex); if (cmp != 0) return cmp;\r
+            cmp = Wgt.CompareTo(o.Wgt); if (cmp != 0) return cmp;\r
+            cmp = Idx.CompareTo(o.Idx);\r
+            return cmp;\r
         }\r
 \r
         public override int GetHashCode()\r
         {\r
-            return Pos.X.GetHashCode() ^ Pos.Y.GetHashCode() ^ Pos.Z.GetHashCode()\r
-                 ^ Nrm.X.GetHashCode() ^ Nrm.Y.GetHashCode() ^ Nrm.Z.GetHashCode()\r
-                 ^ Tex.X.GetHashCode() ^ Tex.Y.GetHashCode() ^ Wgt.W.GetHashCode()\r
-                 ^ Wgt.X.GetHashCode() ^ Wgt.Y.GetHashCode() ^ Wgt.Z.GetHashCode()\r
-                 - Idx.GetHashCode();\r
+            return Pos.GetHashCode() ^ Nrm.GetHashCode() ^ Tex.GetHashCode()\r
+                 ^ Wgt.GetHashCode() ^ Idx.GetHashCode();\r
         }\r
 \r
         public override bool Equals(object obj)\r
         {\r
             Vertex  o   = (Vertex)obj;\r
 \r
-            return Pos.X==o.Pos.X && Pos.Y==o.Pos.Y && Pos.Z==o.Pos.Z\r
-                && Nrm.X==o.Nrm.X && Nrm.Y==o.Nrm.Y && Nrm.Z==o.Nrm.Z\r
-                && Tex.X==o.Tex.X && Tex.Y==o.Tex.Y && Wgt.W==o.Wgt.W\r
-                && Wgt.X==o.Wgt.X && Wgt.Y==o.Wgt.Y && Wgt.Z==o.Wgt.Z\r
+            return Pos.x==o.Pos.x && Pos.y==o.Pos.y && Pos.z==o.Pos.z\r
+                && Nrm.x==o.Nrm.x && Nrm.y==o.Nrm.y && Nrm.z==o.Nrm.z\r
+                && Tex.x==o.Tex.x && Tex.y==o.Tex.y && Wgt.w==o.Wgt.w\r
+                && Wgt.x==o.Wgt.x && Wgt.y==o.Wgt.y && Wgt.z==o.Wgt.z\r
                 && Idx  ==o.Idx;\r
         }\r
     }\r