public static modelInfo geomToModel(Stream input)
        {
            modelInfo model = new modelInfo();

            if (input.Length == 0) return model;

            BinaryReader reader = new BinaryReader(input);

            // Read in the RCOL Header
            uint rcolVersion = reader.ReadUInt32();
            uint rcolDatatype = reader.ReadUInt32();

            uint rcolIndex3 = reader.ReadUInt32();
            uint rcolIndex1 = reader.ReadUInt32();
            uint rcolIndex2 = reader.ReadUInt32();

            for (int i = 0; i < rcolIndex2; i++)
            {
                ulong instanceId = reader.ReadUInt64();
                uint typeId = reader.ReadUInt32();
                uint groupId = reader.ReadUInt32();
            }

            for (int i = 0; i < rcolIndex2; i++)
            {
                uint chunkOffset = reader.ReadUInt32();
                uint chunkSize = reader.ReadUInt32();
            }

            // Real GEOM chunk
            string geomString = Encoding.ASCII.GetString(reader.ReadBytes(4));

            uint geomVersion = reader.ReadUInt32();

            uint tailOffset = reader.ReadUInt32();
            uint tailSize = reader.ReadUInt32();

            uint embeddedId = reader.ReadUInt32();
            if (embeddedId != 0)
            {
                uint embeddedSize = reader.ReadUInt32();
                reader.ReadBytes((int)embeddedSize);
            }

            uint unk1 = reader.ReadUInt32();
            uint unk2 = reader.ReadUInt32();

            uint numVerts = reader.ReadUInt32();
            model.numVertices = numVerts;

            uint vfCount = reader.ReadUInt32();

            vertexFormats.Clear();
            for (int i = 0; i < vfCount; i++)
            {
                vertexFormat vf = new vertexFormat();
                vf.dataType = reader.ReadUInt32();
                vf.subType = reader.ReadUInt32();
                vf.bytesPerElement = reader.ReadByte();
                //textBox1.Text += "Vertex Format #" + i.ToString() + ": " + vf.dataType.ToString() + ":" + vf.subType.ToString() + ":" + vf.bytesPerElement.ToString() + Environment.NewLine;
                vertexFormats.Add(vf);
            }

            // Used for bounding box
            float minX = 50f;
            float minY = 50f;
            float minZ = 50f;
            float maxX = -50f;
            float maxY = -50f;
            float maxZ = -50f;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < numVerts; i++)
            {
                sb.AppendLine("Vertex #" + i.ToString());

                for (int j = 0; j < vertexFormats.Count; j++)
                {
                    float x = 0;
                    float y = 0;
                    float z = 0;

                    vertexFormat vf = (vertexFormat)vertexFormats[j];
                    switch (vf.dataType)
                    {
                        case 1:
                            x = reader.ReadSingle();
                            y = reader.ReadSingle();
                            z = reader.ReadSingle();

                            if (x < minX) minX = x;
                            if (x > maxX) maxX = x;
                            if (y < minY) minY = y;
                            if (y > maxY) maxY = y;
                            if (z < minZ) minZ = z;
                            if (z > maxZ) maxZ = z;

                            sb.AppendLine("  XYZ: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                            model.vertexList.Add(new Vector3(x, y, z));
                            break;
                        case 2:
                            x = reader.ReadSingle();
                            y = reader.ReadSingle();
                            z = reader.ReadSingle();
                            sb.AppendLine("  Normal: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                            model.vertexNormal.Add(new Vector3(x, y, z));

                            break;
                        case 3:
                            float u = reader.ReadSingle();
                            float v = reader.ReadSingle();
                            sb.AppendLine("  UV: " + u.ToString() + " " + v.ToString());
                            model.textureVertex.Add(new uvcoord(u, v));
                            //model.vertexList[i] = new vertex(model.vertexList[i].x, model.vertexList[i].y, model.vertexList[i].z, u, v);
                            break;
                        case 4:
                            sb.AppendLine("  Bone: " + reader.ReadUInt32().ToString());
                            break;
                        case 5:
                            float w1 = reader.ReadSingle();
                            float w2 = reader.ReadSingle();
                            float w3 = reader.ReadSingle();
                            float w4 = reader.ReadSingle();
                            sb.AppendLine("  Weights: " + w1.ToString() + " " + w2.ToString() + " " + w3.ToString() + " " + w4.ToString());
                            break;
                        case 6:
                            x = reader.ReadSingle();
                            z = reader.ReadSingle();
                            y = reader.ReadSingle();
                            sb.AppendLine("  Tangent Normal: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                            model.vertexTangentList.Add(new Vector3(x, y, z));
                            break;
                        case 7:
                            sb.AppendLine("  TagVal: " + reader.ReadUInt32().ToString());
                            break;
                        case 10:
                            sb.AppendLine("  VertexID: " + reader.ReadUInt32().ToString());
                            break;

                    }

                }
            }

            Matrix blah = new Matrix();

            for (int i = 0; i < model.numVertices; i++)
            {
                //model.vertexData.Add(new vertex(model.vertexList[i].X, model.vertexList[i].X, model.vertexList[i].X, model.textureVertex[i].u, model.textureVertex[i].v, model.vertexNormal[i].X, model.vertexNormal[i].Y, model.vertexNormal[i].Z));
                vertex v = new vertex();
                if (model.vertexList.Count > 0 && model.vertexList[i] != null)
                {
                    v.x = -model.vertexList[i].X;
                    v.y = model.vertexList[i].Y;
                    v.z = model.vertexList[i].Z;
                }
                if (model.textureVertex.Count > 0 && model.textureVertex[i] != null)
                {
                    v.tu = model.textureVertex[i].u;
                    v.tv = model.textureVertex[i].v;
                }
                if (model.vertexNormal.Count > 0 && model.vertexNormal[i] != null)
                {
                    v.nx = -model.vertexNormal[i].X;
                    v.ny = model.vertexNormal[i].Y;
                    v.nz = model.vertexNormal[i].Z;
                }
                if (model.vertexTangentList.Count > 0 && model.vertexTangentList[i] != null)
                {
                    v.tx = model.vertexTangentList[i].X;
                    v.ty = model.vertexTangentList[i].Y;
                    v.tz = model.vertexTangentList[i].Z;
                }
                model.vertexData.Add(v);
                //model.vertexData.Add(new vertex(-model.vertexList[i].X, model.vertexList[i].Y, model.vertexList[i].Z, model.textureVertex[i].u, model.textureVertex[i].v, -model.vertexNormal[i].X, model.vertexNormal[i].Y, model.vertexNormal[i].Z, -model.vertexTangentList[i].X, model.vertexTangentList[i].Y, model.vertexTangentList[i].Z));
            }

            // Go through the vertex lists again, this time normalising the values
            float midX = minX + ((maxX - minX) / 2);
            float midY = minY + ((maxY - minY) / 2);
            float midZ = minZ + ((maxZ - minZ) / 2);

            model.bounds.min = new Vector3(minX, minY, minZ);
            model.bounds.mid = new Vector3(midX, midY, midZ);
            model.bounds.max = new Vector3(maxX, maxY, maxZ);

            //model.midPoint = new Vector3(midX, midY, midZ);

            uint itemCount = reader.ReadUInt32();
            byte bytesPerFacePoint = reader.ReadByte();

            uint numFacePoints = reader.ReadUInt32();
            model.numPolygons = numFacePoints / 3;

            for (int i = 0; i < model.numPolygons; i++)
            {
                switch (bytesPerFacePoint)
                {
                    case 2:
                        uint v1 = reader.ReadUInt16();
                        uint v2 = reader.ReadUInt16();
                        uint v3 = reader.ReadUInt16();
                        //model.faces.Add(new polygon(v1, v2, v3));
                        model.faceData.Add(v1);
                        model.faceData.Add(v2);
                        model.faceData.Add(v3);
                        break;
                }
            }
            //textBox1.Text = sb.ToString();

            return model;
        }
예제 #2
0
        public static modelInfo geomToModel(Stream input)
        {
            modelInfo model = new modelInfo();

            if (input.Length == 0)
            {
                return(model);
            }

            BinaryReader reader = new BinaryReader(input);

            // Read in the RCOL Header
            uint rcolVersion  = reader.ReadUInt32();
            uint rcolDatatype = reader.ReadUInt32();

            uint rcolIndex3 = reader.ReadUInt32();
            uint rcolIndex1 = reader.ReadUInt32();
            uint rcolIndex2 = reader.ReadUInt32();

            for (int i = 0; i < rcolIndex2; i++)
            {
                ulong instanceId = reader.ReadUInt64();
                uint  typeId     = reader.ReadUInt32();
                uint  groupId    = reader.ReadUInt32();
            }

            for (int i = 0; i < rcolIndex2; i++)
            {
                uint chunkOffset = reader.ReadUInt32();
                uint chunkSize   = reader.ReadUInt32();
            }

            // Real GEOM chunk
            string geomString = Encoding.ASCII.GetString(reader.ReadBytes(4));

            uint geomVersion = reader.ReadUInt32();

            uint tailOffset = reader.ReadUInt32();
            uint tailSize   = reader.ReadUInt32();

            uint embeddedId = reader.ReadUInt32();

            if (embeddedId != 0)
            {
                uint embeddedSize = reader.ReadUInt32();
                reader.ReadBytes((int)embeddedSize);
            }

            uint unk1 = reader.ReadUInt32();
            uint unk2 = reader.ReadUInt32();

            uint numVerts = reader.ReadUInt32();

            model.numVertices = numVerts;

            uint vfCount = reader.ReadUInt32();

            vertexFormats.Clear();
            for (int i = 0; i < vfCount; i++)
            {
                vertexFormat vf = new vertexFormat();
                vf.dataType        = reader.ReadUInt32();
                vf.subType         = reader.ReadUInt32();
                vf.bytesPerElement = reader.ReadByte();
                //textBox1.Text += "Vertex Format #" + i.ToString() + ": " + vf.dataType.ToString() + ":" + vf.subType.ToString() + ":" + vf.bytesPerElement.ToString() + Environment.NewLine;
                vertexFormats.Add(vf);
            }

            // Used for bounding box
            float minX = 50f;
            float minY = 50f;
            float minZ = 50f;
            float maxX = -50f;
            float maxY = -50f;
            float maxZ = -50f;

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < numVerts; i++)
            {
                sb.AppendLine("Vertex #" + i.ToString());

                for (int j = 0; j < vertexFormats.Count; j++)
                {
                    float x = 0;
                    float y = 0;
                    float z = 0;

                    vertexFormat vf = (vertexFormat)vertexFormats[j];
                    switch (vf.dataType)
                    {
                    case 1:
                        x = reader.ReadSingle();
                        y = reader.ReadSingle();
                        z = reader.ReadSingle();

                        if (x < minX)
                        {
                            minX = x;
                        }
                        if (x > maxX)
                        {
                            maxX = x;
                        }
                        if (y < minY)
                        {
                            minY = y;
                        }
                        if (y > maxY)
                        {
                            maxY = y;
                        }
                        if (z < minZ)
                        {
                            minZ = z;
                        }
                        if (z > maxZ)
                        {
                            maxZ = z;
                        }

                        sb.AppendLine("  XYZ: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                        model.vertexList.Add(new Vector3(x, y, z));
                        break;

                    case 2:
                        x = reader.ReadSingle();
                        y = reader.ReadSingle();
                        z = reader.ReadSingle();
                        sb.AppendLine("  Normal: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                        model.vertexNormal.Add(new Vector3(x, y, z));

                        break;

                    case 3:
                        float u = reader.ReadSingle();
                        float v = reader.ReadSingle();
                        sb.AppendLine("  UV: " + u.ToString() + " " + v.ToString());
                        model.textureVertex.Add(new uvcoord(u, v));
                        //model.vertexList[i] = new vertex(model.vertexList[i].x, model.vertexList[i].y, model.vertexList[i].z, u, v);
                        break;

                    case 4:
                        sb.AppendLine("  Bone: " + reader.ReadUInt32().ToString());
                        break;

                    case 5:
                        float w1 = reader.ReadSingle();
                        float w2 = reader.ReadSingle();
                        float w3 = reader.ReadSingle();
                        float w4 = reader.ReadSingle();
                        sb.AppendLine("  Weights: " + w1.ToString() + " " + w2.ToString() + " " + w3.ToString() + " " + w4.ToString());
                        break;

                    case 6:
                        x = reader.ReadSingle();
                        z = reader.ReadSingle();
                        y = reader.ReadSingle();
                        sb.AppendLine("  Tangent Normal: " + x.ToString() + " " + y.ToString() + " " + z.ToString());
                        model.vertexTangentList.Add(new Vector3(x, y, z));
                        break;

                    case 7:
                        sb.AppendLine("  TagVal: " + reader.ReadUInt32().ToString());
                        break;

                    case 10:
                        sb.AppendLine("  VertexID: " + reader.ReadUInt32().ToString());
                        break;
                    }
                }
            }

            Matrix blah = new Matrix();


            for (int i = 0; i < model.numVertices; i++)
            {
                //model.vertexData.Add(new vertex(model.vertexList[i].X, model.vertexList[i].X, model.vertexList[i].X, model.textureVertex[i].u, model.textureVertex[i].v, model.vertexNormal[i].X, model.vertexNormal[i].Y, model.vertexNormal[i].Z));
                vertex v = new vertex();
                if (model.vertexList.Count > 0 && model.vertexList[i] != null)
                {
                    v.x = -model.vertexList[i].X;
                    v.y = model.vertexList[i].Y;
                    v.z = model.vertexList[i].Z;
                }
                if (model.textureVertex.Count > 0 && model.textureVertex[i] != null)
                {
                    v.tu = model.textureVertex[i].u;
                    v.tv = model.textureVertex[i].v;
                }
                if (model.vertexNormal.Count > 0 && model.vertexNormal[i] != null)
                {
                    v.nx = -model.vertexNormal[i].X;
                    v.ny = model.vertexNormal[i].Y;
                    v.nz = model.vertexNormal[i].Z;
                }
                if (model.vertexTangentList.Count > 0 && model.vertexTangentList[i] != null)
                {
                    v.tx = model.vertexTangentList[i].X;
                    v.ty = model.vertexTangentList[i].Y;
                    v.tz = model.vertexTangentList[i].Z;
                }
                model.vertexData.Add(v);
                //model.vertexData.Add(new vertex(-model.vertexList[i].X, model.vertexList[i].Y, model.vertexList[i].Z, model.textureVertex[i].u, model.textureVertex[i].v, -model.vertexNormal[i].X, model.vertexNormal[i].Y, model.vertexNormal[i].Z, -model.vertexTangentList[i].X, model.vertexTangentList[i].Y, model.vertexTangentList[i].Z));
            }

            // Go through the vertex lists again, this time normalising the values
            float midX = minX + ((maxX - minX) / 2);
            float midY = minY + ((maxY - minY) / 2);
            float midZ = minZ + ((maxZ - minZ) / 2);

            model.bounds.min = new Vector3(minX, minY, minZ);
            model.bounds.mid = new Vector3(midX, midY, midZ);
            model.bounds.max = new Vector3(maxX, maxY, maxZ);

            //model.midPoint = new Vector3(midX, midY, midZ);

            uint itemCount         = reader.ReadUInt32();
            byte bytesPerFacePoint = reader.ReadByte();

            uint numFacePoints = reader.ReadUInt32();

            model.numPolygons = numFacePoints / 3;

            for (int i = 0; i < model.numPolygons; i++)
            {
                switch (bytesPerFacePoint)
                {
                case 2:
                    uint v1 = reader.ReadUInt16();
                    uint v2 = reader.ReadUInt16();
                    uint v3 = reader.ReadUInt16();
                    //model.faces.Add(new polygon(v1, v2, v3));
                    model.faceData.Add(v1);
                    model.faceData.Add(v2);
                    model.faceData.Add(v3);
                    break;
                }
            }
            //textBox1.Text = sb.ToString();

            return(model);
        }