public static ModelConverterData ReadOBJFile(string InputFile, bool isShadowCollision)
        {
            ModelConverterData objData = new ModelConverterData()
            {
                MaterialList = new List <string>(),
                VertexList   = new List <Vertex>(),
                UVList       = new List <Vector2>(),
                ColorList    = new List <SharpDX.Color>(),
                TriangleList = new List <Triangle>(),
                MTLLib       = null
            };

            string[] OBJFile = File.ReadAllLines(InputFile);

            int CurrentMaterial = -1;

            bool hasUVCoords = true;

            RenderWareFile.Color TempColFlags = new RenderWareFile.Color(0, 0, 0, 0);

            foreach (string j in OBJFile)
            {
                if (j.Length > 2)
                {
                    if (j.StartsWith("v "))
                    {
                        string a = Regex.Replace(j, @"\s+", " ");

                        string[] SubStrings = a.Split(' ');
                        Vertex   TempVertex = new Vertex();
                        TempVertex.Position.X = Convert.ToSingle(SubStrings[1]);
                        TempVertex.Position.Y = Convert.ToSingle(SubStrings[2]);
                        TempVertex.Position.Z = Convert.ToSingle(SubStrings[3]);

                        TempVertex.Color = SharpDX.Color.White;

                        objData.VertexList.Add(TempVertex);
                    }
                    else if (j.Substring(0, 3) == "vt ")
                    {
                        string[] SubStrings = j.Split(' ');
                        Vector2  TempUV     = new Vector2
                        {
                            X = Convert.ToSingle(SubStrings[1]),
                            Y = Convert.ToSingle(SubStrings[2])
                        };
                        objData.UVList.Add(TempUV);
                    }
                    else if (j.Substring(0, 3) == "vc ") // Special code
                    {
                        string[]      SubStrings = j.Split(' ');
                        SharpDX.Color TempColor  = new SharpDX.Color
                        {
                            R = Convert.ToByte(SubStrings[1]),
                            G = Convert.ToByte(SubStrings[2]),
                            B = Convert.ToByte(SubStrings[3]),
                            A = Convert.ToByte(SubStrings[4])
                        };

                        objData.ColorList.Add(TempColor);
                    }
                    else if (j.StartsWith("f "))
                    {
                        string[] SubStrings = j.Split(' ');

                        Triangle TempTriangle = new Triangle
                        {
                            MaterialIndex = CurrentMaterial,
                            vertex1       = Convert.ToInt32(SubStrings[1].Split('/')[0]) - 1,
                            vertex2       = Convert.ToInt32(SubStrings[2].Split('/')[0]) - 1,
                            vertex3       = Convert.ToInt32(SubStrings[3].Split('/')[0]) - 1
                        };

                        if (hasUVCoords & !isShadowCollision)
                        {
                            try
                            {
                                TempTriangle.UVCoord1 = Convert.ToInt32(SubStrings[1].Split('/')[1]) - 1;
                                TempTriangle.UVCoord2 = Convert.ToInt32(SubStrings[2].Split('/')[1]) - 1;
                                TempTriangle.UVCoord3 = Convert.ToInt32(SubStrings[3].Split('/')[1]) - 1;
                            }
                            catch
                            {
                                MessageBox.Show("Error parsing texture coordinates. The model will be imported without them.");
                                hasUVCoords    = false;
                                objData.UVList = new List <Vector2>()
                                {
                                    new Vector2()
                                };
                            }
                        }

                        objData.TriangleList.Add(TempTriangle);
                    }
                    else if (j.StartsWith("usemtl "))
                    {
                        objData.MaterialList.Add(Regex.Replace(j.Substring(7), @"\s+", ""));
                        CurrentMaterial += 1;
                    }
                    else if (j.StartsWith("mtllib "))
                    {
                        objData.MTLLib = j.Substring(7).Split('\\').LastOrDefault();
                    }
                }
            }

            if (isShadowCollision)
            {
                return(objData);
            }

            // Special code
            if (objData.ColorList.Count == objData.VertexList.Count)
            {
                for (int i = 0; i < objData.VertexList.Count; i++)
                {
                    Vertex v = objData.VertexList[i];
                    v.Color = objData.ColorList[i];
                    objData.VertexList[i] = v;
                }
            }

            try
            {
                objData.MaterialList = ReplaceMaterialNames(InputFile, objData.MTLLib, objData.MaterialList);
            }
            catch
            {
                MessageBox.Show("Unable to load material lib. Will use material names as texture names.");
            }

            if (hasUVCoords)
            {
                return(FixUVCoords(objData));
            }
            else
            {
                return(objData);
            }
        }
        public static ModelConverterData FixUVCoords(ModelConverterData data)
        {
            for (int i = 0; i < data.TriangleList.Count; i++)
            {
                if (data.VertexList[data.TriangleList[i].vertex1].HasUV == false)
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex1];

                    TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord1].X;
                    TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord1].Y;
                    TempVertex.HasUV      = true;
                    data.VertexList[data.TriangleList[i].vertex1] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex1];

                    if ((TempVertex.TexCoord.X != data.UVList[data.TriangleList[i].UVCoord1].X) | (TempVertex.TexCoord.Y != data.UVList[data.TriangleList[i].UVCoord1].Y))
                    {
                        TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord1].X;
                        TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord1].Y;

                        Triangle TempTriangle = data.TriangleList[i];
                        TempTriangle.vertex1 = data.VertexList.Count;
                        data.TriangleList[i] = TempTriangle;
                        data.VertexList.Add(TempVertex);
                    }
                }
                if (data.VertexList[data.TriangleList[i].vertex2].HasUV == false)
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex2];

                    TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord2].X;
                    TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord2].Y;
                    TempVertex.HasUV      = true;
                    data.VertexList[data.TriangleList[i].vertex2] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex2];

                    if ((TempVertex.TexCoord.X != data.UVList[data.TriangleList[i].UVCoord2].X) | (TempVertex.TexCoord.Y != data.UVList[data.TriangleList[i].UVCoord2].Y))
                    {
                        TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord2].X;
                        TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord2].Y;

                        Triangle TempTriangle = data.TriangleList[i];
                        TempTriangle.vertex2 = data.VertexList.Count;
                        data.TriangleList[i] = TempTriangle;
                        data.VertexList.Add(TempVertex);
                    }
                }
                if (data.VertexList[data.TriangleList[i].vertex3].HasUV == false)
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex3];

                    TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord3].X;
                    TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord3].Y;
                    TempVertex.HasUV      = true;
                    data.VertexList[data.TriangleList[i].vertex3] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = data.VertexList[data.TriangleList[i].vertex3];

                    if ((TempVertex.TexCoord.X != data.UVList[data.TriangleList[i].UVCoord3].X) | (TempVertex.TexCoord.Y != data.UVList[data.TriangleList[i].UVCoord3].Y))
                    {
                        TempVertex.TexCoord.X = data.UVList[data.TriangleList[i].UVCoord3].X;
                        TempVertex.TexCoord.Y = data.UVList[data.TriangleList[i].UVCoord3].Y;

                        Triangle TempTriangle = data.TriangleList[i];
                        TempTriangle.vertex3 = data.VertexList.Count;
                        data.TriangleList[i] = TempTriangle;
                        data.VertexList.Add(TempVertex);
                    }
                }
            }

            return(data);
        }
        public static RWSection[] CreateBSPFile(string FileNameForBox, ModelConverterData data)
        {
            Vertex3 Max = new Vertex3(data.VertexList[0].Position.X, data.VertexList[0].Position.Y, data.VertexList[0].Position.Z);
            Vertex3 Min = new Vertex3(data.VertexList[0].Position.X, data.VertexList[0].Position.Y, data.VertexList[0].Position.Z);

            foreach (Vertex i in data.VertexList)
            {
                if (i.Position.X > Max.X)
                {
                    Max.X = i.Position.X;
                }
                if (i.Position.Y > Max.Y)
                {
                    Max.Y = i.Position.Y;
                }
                if (i.Position.Z > Max.Z)
                {
                    Max.Z = i.Position.Z;
                }
                if (i.Position.X < Min.X)
                {
                    Min.X = i.Position.X;
                }
                if (i.Position.Y < Min.Y)
                {
                    Min.Y = i.Position.Y;
                }
                if (i.Position.Z < Min.Z)
                {
                    Min.Z = i.Position.Z;
                }
            }

            List <Vertex3> vList = new List <Vertex3>(data.VertexList.Count);

            foreach (Vertex v in data.VertexList)
            {
                vList.Add(new Vertex3(v.Position.X, v.Position.Y, v.Position.Z));
            }

            List <RenderWareFile.Color> cList = new List <RenderWareFile.Color>(data.VertexList.Count);

            foreach (Vertex v in data.VertexList)
            {
                cList.Add(new RenderWareFile.Color(v.Color.R, v.Color.G, v.Color.B, v.Color.A));
            }

            List <TextCoord> uvList = new List <TextCoord>(data.VertexList.Count);

            if (Program.levelEditor.checkBoxFlipUVs.Checked)
            {
                foreach (Vertex v in data.VertexList)
                {
                    uvList.Add(new TextCoord(v.TexCoord.X, v.TexCoord.Y));
                }
            }
            else
            {
                foreach (Vertex v in data.VertexList)
                {
                    uvList.Add(new TextCoord(v.TexCoord.X, -v.TexCoord.Y));
                }
            }

            List <RenderWareFile.Triangle> tList = new List <RenderWareFile.Triangle>(data.TriangleList.Count);

            foreach (Triangle t in data.TriangleList)
            {
                tList.Add(new RenderWareFile.Triangle((ushort)t.MaterialIndex, (ushort)t.vertex1, (ushort)t.vertex2, (ushort)t.vertex3));
            }

            List <BinMesh> binMeshList = new List <BinMesh>();
            int            TotalNumberOfTristripIndicies = 0;

            if (Program.levelEditor.checkBoxTristrip.Checked) // tristrip generator
            {
                for (int i = 0; i < data.MaterialList.Count; i++)
                {
                    List <Triangle> TriangleStream2 = new List <Triangle>();
                    foreach (Triangle f in data.TriangleList)
                    {
                        if (f.MaterialIndex == i)
                        {
                            TriangleStream2.Add(f);
                        }
                    }

                    List <List <int> > indexLists = GenerateTristrips(TriangleStream2);

                    foreach (List <int> indices in indexLists)
                    {
                        TotalNumberOfTristripIndicies += indices.Count();
                        binMeshList.Add(new BinMesh()
                        {
                            materialIndex = i,
                            indexCount    = indices.Count(),
                            vertexIndices = indices.ToArray()
                        });
                    }
                }
            }
            else //trilist generator
            {
                for (int i = 0; i < data.MaterialList.Count; i++)
                {
                    List <int> indices = new List <int>();
                    foreach (Triangle f in data.TriangleList)
                    {
                        if (f.MaterialIndex == i)
                        {
                            indices.Add(f.vertex1);
                            indices.Add(f.vertex2);
                            indices.Add(f.vertex3);
                        }
                    }
                    TotalNumberOfTristripIndicies += indices.Count();

                    binMeshList.Add(new BinMesh
                    {
                        materialIndex = i,
                        indexCount    = indices.Count(),
                        vertexIndices = indices.ToArray()
                    });
                }
            }

            WorldFlags worldFlags = WorldFlags.HasOneSetOfTextCoords | WorldFlags.HasVertexColors
                                    | WorldFlags.WorldSectorsOverlap | (WorldFlags)0x00010000;

            worldFlags = Program.levelEditor.checkBoxTristrip.Checked ? worldFlags | WorldFlags.UseTriangleStrips : worldFlags;

            World_000B world = new World_000B()
            {
                worldStruct = new WorldStruct_0001()
                {
                    rootIsWorldSector = 1,
                    inverseOrigin     = new Vertex3(-0f, -0f, -0f),
                    numTriangles      = (uint)data.TriangleList.Count(),
                    numVertices       = (uint)data.VertexList.Count(),
                    numPlaneSectors   = 0,
                    numAtomicSectors  = 1,
                    colSectorSize     = 0,
                    worldFlags        = worldFlags,
                    boxMaximum        = Max,
                    boxMinimum        = Min,
                },

                materialList = new MaterialList_0008()
                {
                    materialListStruct = new MaterialListStruct_0001()
                    {
                        materialCount = data.MaterialList.Count()
                    },
                    materialList = new Material_0007[data.MaterialList.Count()]
                },

                firstWorldChunk = new AtomicSector_0009()
                {
                    atomicStruct = new AtomicSectorStruct_0001()
                    {
                        matListWindowBase = 0,
                        numTriangles      = data.TriangleList.Count(),
                        numVertices       = data.VertexList.Count(),
                        boxMaximum        = Max,
                        boxMinimum        = Min,
                        collSectorPresent = 0x2F50D984,
                        unused            = 0,
                        vertexArray       = vList.ToArray(),
                        colorArray        = cList.ToArray(),
                        uvArray           = uvList.ToArray(),
                        triangleArray     = tList.ToArray()
                    },
                    atomicExtension = new Extension_0003()
                    {
                        extensionSectionList = new List <RWSection>()
                        {
                            new BinMeshPLG_050E()
                            {
                                binMeshHeaderFlags = Program.levelEditor.checkBoxTristrip.Checked ? BinMeshHeaderFlags.TriangleStrip : BinMeshHeaderFlags.TriangleList,
                                numMeshes          = binMeshList.Count(),
                                totalIndexCount    = TotalNumberOfTristripIndicies,
                                binMeshList        = binMeshList.ToArray()
                            }
                        }
                    }
                },

                worldExtension = new Extension_0003()
            };

            for (int i = 0; i < data.MaterialList.Count; i++)
            {
                world.materialList.materialList[i] = new Material_0007()
                {
                    materialStruct = new MaterialStruct_0001()
                    {
                        unusedFlags = 0,
                        color       = new RenderWareFile.Color()
                        {
                            R = 0xFF, G = 0xFF, B = 0xFF, A = 0xFF
                        },
                        unusedInt2 = 0x2DF53E84,
                        isTextured = 1,
                        ambient    = 1f,
                        specular   = 1f,
                        diffuse    = 1f
                    },
                    texture = new Texture_0006()
                    {
                        textureStruct = new TextureStruct_0001()
                        {
                            filterMode   = FilterMode.FILTERLINEAR,
                            addressModeU = AddressMode.TEXTUREADDRESSWRAP,
                            addressModeV = AddressMode.TEXTUREADDRESSWRAP,
                            useMipLevels = 1
                        },
                        diffuseTextureName = new String_0002()
                        {
                            stringString = data.MaterialList[i]
                        },
                        alphaTextureName = new String_0002()
                        {
                            stringString = ""
                        },
                        textureExtension = new Extension_0003()
                    },
                    materialExtension = new Extension_0003(),
                };
            }

            return(new RWSection[] { world });
        }
        public static RWSection[] CreateShadowCollisionBSPFile(ModelConverterData data)
        {
            Vertex3 Max = new Vertex3(data.VertexList[0].Position.X, data.VertexList[0].Position.Y, data.VertexList[0].Position.Z);
            Vertex3 Min = new Vertex3(data.VertexList[0].Position.X, data.VertexList[0].Position.Y, data.VertexList[0].Position.Z);

            foreach (Vertex i in data.VertexList)
            {
                if (i.Position.X > Max.X)
                {
                    Max.X = i.Position.X;
                }
                if (i.Position.Y > Max.Y)
                {
                    Max.Y = i.Position.Y;
                }
                if (i.Position.Z > Max.Z)
                {
                    Max.Z = i.Position.Z;
                }
                if (i.Position.X < Min.X)
                {
                    Min.X = i.Position.X;
                }
                if (i.Position.Y < Min.Y)
                {
                    Min.Y = i.Position.Y;
                }
                if (i.Position.Z < Min.Z)
                {
                    Min.Z = i.Position.Z;
                }
            }

            List <Vertex3> vList = new List <Vertex3>(data.VertexList.Count);

            foreach (Vertex v in data.VertexList)
            {
                vList.Add(new Vertex3(v.Position.X, v.Position.Y, v.Position.Z));
            }

            List <RenderWareFile.Triangle> tList = new List <RenderWareFile.Triangle>(data.TriangleList.Count);

            foreach (Triangle t in data.TriangleList)
            {
                tList.Add(new RenderWareFile.Triangle((ushort)t.MaterialIndex, (ushort)t.vertex1, (ushort)t.vertex2, (ushort)t.vertex3));
            }

            List <BinMesh> binMeshList = new List <BinMesh>();
            int            TotalNumberOfTristripIndicies = 0;

            for (int i = 0; i < data.MaterialList.Count; i++)
            {
                List <int> indices = new List <int>();
                foreach (Triangle f in data.TriangleList)
                {
                    if (f.MaterialIndex == i)
                    {
                        indices.Add(f.vertex1);
                        indices.Add(f.vertex2);
                        indices.Add(f.vertex3);
                    }
                }
                TotalNumberOfTristripIndicies += indices.Count();

                binMeshList.Add(new BinMesh
                {
                    materialIndex = i,
                    indexCount    = indices.Count(),
                    vertexIndices = indices.ToArray()
                });
            }

            // GENERATE COLLISION DATA

            List <ushort> TriangleIndexList = new List <ushort>();
            List <Split>  splitlist         = new List <Split>();
            ushort        loop              = 0;
            bool          exitloop          = false;
            Split         split             = new Split();
            byte          trianglesPerSplit = 200;

            while (!exitloop)
            {
                split = new Split
                {
                    negativeSector = new Sector()
                    {
                        Max = Max,
                        Min = Min,
                        TriangleIndexList = new List <ushort>(),
                        splitPosition     = Max.X,
                        type = SectorType.NegativeX
                    },
                    positiveSector = new Sector()
                    {
                        Max = Max,
                        Min = Min,
                        TriangleIndexList = new List <ushort>(),
                        splitPosition     = Min.X,
                        type = SectorType.PositiveX
                    }
                };

                for (ushort i = (ushort)(trianglesPerSplit * loop); i < tList.Count(); i++)
                {
                    TriangleIndexList.Add(i);

                    split.negativeSector.TriangleIndexList.Add(i);

                    if (split.negativeSector.TriangleIndexList.Count() == trianglesPerSplit)
                    {
                        split.negativeSector.triangleAmount = trianglesPerSplit;
                        split.negativeSector.referenceIndex = (ushort)(trianglesPerSplit * loop);
                        loop += 1;

                        split.positiveSector.triangleAmount = 0xFF;
                        split.positiveSector.referenceIndex = loop;
                        splitlist.Add(split);
                        split    = new Split();
                        exitloop = false;
                        break;
                    }

                    exitloop = true;
                }
            }

            split.negativeSector.triangleAmount = (byte)split.negativeSector.TriangleIndexList.Count();
            split.negativeSector.referenceIndex = (ushort)(trianglesPerSplit * loop);
            split.positiveSector.triangleAmount = 0;
            split.positiveSector.referenceIndex = 0;
            loop += 1;
            splitlist.Add(split);

            //Sector sector = new Sector()
            //{
            //    Max = Max,
            //    Min = Min,
            //    TriangleIndexList = new List<ushort>()
            //};

            // sector.TriangleIndexList = FindTrianglesInsideNode(vList, tList, sector, TriangleIndexList);

            // List<ushort> TriangleIndexReferenceList = new List<ushort>();
            // List<Split> splitList = new List<Split>();

            //PositionOnList = 0;
            //SplitSector(sector, 20, 0, splitList, TriangleIndexReferenceList, vList, tList);

            // COLLISION FLAGS

            Color[] cFlagList = new Color[data.MaterialList.Count];
            for (int i = 0; i < cFlagList.Length; i++)
            {
                cFlagList[i] = new Color(0x01, 0x00, 0x02, 0x00);

                string a = data.MaterialList[i].Split('_').Last();

                if (a == "c") // ceiling
                {
                    cFlagList[i] = new Color(0x00, 0x00, 0x00, 0x00);
                }
                else if (a == "f") // road floor
                {
                    cFlagList[i] = new Color(0x01, 0x00, 0x02, 0x00);
                }
                else if (a == "fs") // stone floor
                {
                    cFlagList[i] = new Color(0x01, 0x00, 0x00, 0x60);
                }
                else if (a == "fm") // metal floor
                {
                    cFlagList[i] = new Color(0x01, 0x01, 0x01, 0x10);
                }
                else if (a == "t") // triangle jump wall
                {
                    cFlagList[i] = new Color(0x02, 0x00, 0x00, 0x00);
                }
                else if (a == "a") // angle wall
                {
                    cFlagList[i] = new Color(0x02, 0x01, 0x01, 0x10);
                }
                else if (a == "i") // invisible wall
                {
                    cFlagList[i] = new Color(0x02, 0x02, 0x00, 0x00);
                }
                else if (a == "g") // green goo
                {
                    cFlagList[i] = new Color(0x05, 0x00, 0x02, 0x00);
                }
                else if (a == "k") // barrier
                {
                    cFlagList[i] = new Color(0x08, 0x00, 0x00, 0x00);
                }
                else if (a == "i2") // invisible wall at distance
                {
                    cFlagList[i] = new Color(0x10, 0x00, 0x00, 0x00);
                }
                else if (a == "x") // death
                {
                    cFlagList[i] = new Color(0x20, 0x00, 0x00, 0x00);
                }
                else if (a.Count() == 8)
                {
                    try
                    {
                        cFlagList[i] = Color.FromString(a);
                    }
                    catch
                    {
                        cFlagList[i] = new Color(0x01, 0x00, 0x02, 0x00);
                    }
                }
            }

            List <Color> cFlags = new List <Color>();

            foreach (Triangle t in data.TriangleList)
            {
                cFlags.Add(cFlagList[t.MaterialIndex]);
            }

            // GENERATE RENDERWARE DATA

            World_000B world = new World_000B
            {
                worldStruct = new WorldStruct_0001()
                {
                    rootIsWorldSector = 1,
                    inverseOrigin     = new Vertex3(-0f, -0f, -0f),
                    numTriangles      = (uint)data.TriangleList.Count(),
                    numVertices       = (uint)data.VertexList.Count(),
                    numPlaneSectors   = 0,
                    numAtomicSectors  = 1,
                    colSectorSize     = 0,
                    worldFlags        = WorldFlags.WorldSectorsOverlap,// | WorldFlags.ModulateMaterialColors, //(WorldFlags)0x40000040,
                    boxMaximum        = Max,
                    boxMinimum        = Min
                },

                materialList = new MaterialList_0008()
                {
                    materialListStruct = new MaterialListStruct_0001()
                    {
                        materialCount = data.MaterialList.Count()
                    },
                    materialList = new Material_0007[data.MaterialList.Count()]
                },

                firstWorldChunk = new AtomicSector_0009()
                {
                    atomicSectorStruct = new AtomicSectorStruct_0001()
                    {
                        matListWindowBase = 0,
                        numTriangles      = data.TriangleList.Count(),
                        numVertices       = data.VertexList.Count(),
                        boxMaximum        = Max,
                        boxMinimum        = Min,
                        collSectorPresent = 0x0012f410,
                        unused            = 0,
                        vertexArray       = vList.ToArray(),
                        colorArray        = null,
                        uvArray           = null,
                        triangleArray     = tList.ToArray()
                    },
                    atomicSectorExtension = new Extension_0003()
                    {
                        extensionSectionList = new List <RWSection>()
                        {
                            new BinMeshPLG_050E()
                            {
                                binMeshHeaderFlags = BinMeshHeaderFlags.TriangleList,
                                numMeshes          = binMeshList.Count(),
                                totalIndexCount    = TotalNumberOfTristripIndicies,
                                binMeshList        = binMeshList.ToArray()
                            },
                            new CollisionPLG_011D()
                            {
                                colTree = new ColTree_002C()
                                {
                                    colTreeStruct = new ColTreeStruct_0001()
                                    {
                                        useMap        = 1,
                                        boxMaximum    = Max,
                                        boxMinimum    = Min,
                                        numSplits     = splitlist.Count(),
                                        numTriangles  = TriangleIndexList.Count(),
                                        splitArray    = splitlist.ToArray(),
                                        triangleArray = TriangleIndexList.ToArray(),
                                    },
                                },
                                unknownValue = 0x00037002
                            },
                            new UserDataPLG_011F()
                            {
                                userDataType   = 0x02,
                                unknown2       = 0x0A,
                                attribute      = "attribute",
                                unknown3       = 0x01,
                                numTriangles   = tList.Count(),
                                collisionFlags = cFlags.ToArray(),
                                unknown4       = 0x0D,
                                userData       = "FVF.UserData",
                                unknown5       = 0x01,
                                unknown6       = 0x01,
                                unknown7       = 0x3003
                            }
                        }
                    }
                },

                worldExtension = new Extension_0003()
            };

            for (int i = 0; i < data.MaterialList.Count; i++)
            {
                world.materialList.materialList[i] = new Material_0007()
                {
                    materialStruct = new MaterialStruct_0001()
                    {
                        unusedFlags = 0,
                        color       = new Color()
                        {
                            R = 0xFF, G = 0xFF, B = 0xFF, A = 0xFF
                        },
                        unusedInt2 = 0x01FAE70C,
                        isTextured = 0,
                        ambient    = 1f,
                        specular   = 1f,
                        diffuse    = 1f
                    },
                    texture           = null,
                    materialExtension = new Extension_0003()
                };
            }

            return(new RWSection[] { world });
        }
        public static ModelConverterData ReadAssimp(string fileName)
        {
            Scene scene = new AssimpContext().ImportFile(fileName,
                                                         PostProcessSteps.Debone | PostProcessSteps.FindInstances | PostProcessSteps.FindInvalidData |
                                                         PostProcessSteps.OptimizeGraph | PostProcessSteps.OptimizeMeshes | PostProcessSteps.Triangulate |
                                                         PostProcessSteps.PreTransformVertices);

            ModelConverterData data = new ModelConverterData()
            {
                MaterialList = new List <string>(),
                VertexList   = new List <Vertex>(),
                UVList       = new List <Vector2>(),
                ColorList    = new List <SharpDX.Color>(),
                TriangleList = new List <Triangle>()
            };

            foreach (var mat in scene.Materials)
            {
                if (mat.TextureDiffuse.FilePath == null)
                {
                    data.MaterialList.Add(Path.GetFileNameWithoutExtension(""));
                }
                else
                {
                    data.MaterialList.Add(Path.GetFileNameWithoutExtension(mat.TextureDiffuse.FilePath));
                }
            }

            int totalVertices = 0;

            foreach (var m in scene.Meshes)
            {
                for (int i = 0; i < m.VertexCount; i++)
                {
                    Vertex v = new Vertex()
                    {
                        Position = new Vector3(m.Vertices[i].X, m.Vertices[i].Y, m.Vertices[i].Z)
                    };

                    if (m.HasTextureCoords(0))
                    {
                        v.TexCoord = new Vector2(m.TextureCoordinateChannels[0][i].X, m.TextureCoordinateChannels[0][i].Y);
                    }
                    else
                    {
                        v.TexCoord = new Vector2();
                    }

                    if (m.HasVertexColors(0))
                    {
                        v.Color = new SharpDX.Color(m.VertexColorChannels[0][i].R, m.VertexColorChannels[0][i].G, m.VertexColorChannels[0][i].B, m.VertexColorChannels[0][i].A);
                    }
                    else
                    {
                        v.Color = SharpDX.Color.White;
                    }

                    data.VertexList.Add(v);
                }

                foreach (var t in m.Faces)
                {
                    data.TriangleList.Add(new Triangle()
                    {
                        vertex1       = t.Indices[0] + totalVertices,
                        vertex2       = t.Indices[1] + totalVertices,
                        vertex3       = t.Indices[2] + totalVertices,
                        MaterialIndex = m.MaterialIndex
                    });
                }
                totalVertices += m.VertexCount;
            }

            return(data);
        }
        public static ModelConverterData ConvertDataFromDAEObject(List <ModelConverterDataCollada> DAEObjectList, bool ignoreUVsAndColors)
        {
            ModelConverterData data = new ModelConverterData()
            {
                MaterialList = new List <string>(),
                VertexList   = new List <Vertex>(),
                UVList       = new List <Vector2>(),
                ColorList    = new List <Color>(),
                TriangleList = new List <Triangle>(),
                MTLLib       = null
            };

            int TotalVertices = 0;
            int TotalUVs      = 0;
            int TotalColors   = 0;

            foreach (ModelConverterDataCollada i in DAEObjectList)
            {
                foreach (Vector3 j in i.PositionVertexList)
                {
                    Vector3 NewPos = (Vector3)Vector3.Transform(j, i.TransformMatrix);

                    data.VertexList.Add(new Vertex
                    {
                        Position = NewPos
                    });
                }
                foreach (Vector2 j in i.TexCoordList)
                {
                    data.UVList.Add(j);
                }
                foreach (Color j in i.VColorList)
                {
                    data.ColorList.Add(j);
                }
                foreach (TriangleListCollada j in i.TriangleListList)
                {
                    if (j.TriangleList.Count == 0)
                    {
                        continue;
                    }

                    if (j.TextureName == null)
                    {
                        j.TextureName = "default";
                    }

                    if (!data.MaterialList.Contains(j.TextureName))
                    {
                        data.MaterialList.Add(j.TextureName);
                    }

                    foreach (Triangle k in j.TriangleList)
                    {
                        for (int m = 0; m < data.MaterialList.Count; m++)
                        {
                            if (data.MaterialList[m] == j.TextureName)
                            {
                                k.MaterialIndex = m;
                                break;
                            }
                        }

                        k.vertex1  += TotalVertices;
                        k.vertex2  += TotalVertices;
                        k.vertex3  += TotalVertices;
                        k.UVCoord1 += TotalUVs;
                        k.UVCoord2 += TotalUVs;
                        k.UVCoord3 += TotalUVs;
                        k.Color1   += TotalColors;
                        k.Color2   += TotalColors;
                        k.Color3   += TotalColors;
                        data.TriangleList.Add(k);
                    }
                }
                TotalVertices += i.PositionVertexList.Count;
                TotalUVs      += i.TexCoordList.Count;
                TotalColors   += i.VColorList.Count;
            }

            if (!ignoreUVsAndColors)
            {
                FixUVCoords(data);
                FixColors(data);
            }

            return(data);
        }
Exemple #7
0
        public static void FixColors(ModelConverterData d)
        {
            for (int i = 0; i < d.TriangleList.Count; i++)
            {
                if (d.VertexList[d.TriangleList[i].vertex1].HasColor == false)
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex1];

                    TempVertex.Color    = d.ColorList[d.TriangleList[i].Color1];
                    TempVertex.HasColor = true;
                    d.VertexList[d.TriangleList[i].vertex1] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex1];

                    if (!ColorsAreEqual(TempVertex.Color, d.ColorList[d.TriangleList[i].Color1]))
                    {
                        TempVertex.Color = d.ColorList[d.TriangleList[i].Color1];

                        Triangle TempTriangle = d.TriangleList[i];
                        TempTriangle.vertex1 = d.VertexList.Count;
                        d.TriangleList[i]    = TempTriangle;
                        d.VertexList.Add(TempVertex);
                    }
                }

                if (d.VertexList[d.TriangleList[i].vertex2].HasColor == false)
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex2];

                    TempVertex.Color    = d.ColorList[d.TriangleList[i].Color2];
                    TempVertex.HasColor = true;
                    d.VertexList[d.TriangleList[i].vertex2] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex2];

                    if (!ColorsAreEqual(TempVertex.Color, d.ColorList[d.TriangleList[i].Color2]))
                    {
                        TempVertex.Color = d.ColorList[d.TriangleList[i].Color2];

                        Triangle TempTriangle = d.TriangleList[i];
                        TempTriangle.vertex2 = d.VertexList.Count;
                        d.TriangleList[i]    = TempTriangle;
                        d.VertexList.Add(TempVertex);
                    }
                }

                if (d.VertexList[d.TriangleList[i].vertex3].HasColor == false)
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex3];

                    TempVertex.Color    = d.ColorList[d.TriangleList[i].Color3];
                    TempVertex.HasColor = true;
                    d.VertexList[d.TriangleList[i].vertex3] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = d.VertexList[d.TriangleList[i].vertex3];

                    if (!ColorsAreEqual(TempVertex.Color, d.ColorList[d.TriangleList[i].Color3]))
                    {
                        TempVertex.Color = d.ColorList[d.TriangleList[i].Color3];

                        Triangle TempTriangle = d.TriangleList[i];
                        TempTriangle.vertex3 = d.VertexList.Count;
                        d.TriangleList[i]    = TempTriangle;
                        d.VertexList.Add(TempVertex);
                    }
                }
            }
        }
Exemple #8
0
        public static ModelConverterData FixColors(ModelConverterData d)
        {
            List <Vertex>   VertexStream   = d.VertexList;
            List <Triangle> TriangleStream = d.TriangleList;
            List <Color>    ColorStream    = d.ColorList;

            for (int i = 0; i < TriangleStream.Count; i++)
            {
                if (VertexStream[TriangleStream[i].vertex1].HasColor == false)
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex1];

                    TempVertex.Color    = ColorStream[TriangleStream[i].Color1];
                    TempVertex.HasColor = true;
                    VertexStream[TriangleStream[i].vertex1] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex1];

                    if (TempVertex.Color != ColorStream[TriangleStream[i].Color1])
                    {
                        TempVertex.Color.R = ColorStream[TriangleStream[i].Color1].R;
                        TempVertex.Color.G = ColorStream[TriangleStream[i].Color1].G;
                        TempVertex.Color.B = ColorStream[TriangleStream[i].Color1].B;
                        TempVertex.Color.A = ColorStream[TriangleStream[i].Color1].A;

                        Triangle TempTriangle = TriangleStream[i];
                        TempTriangle.vertex1 = VertexStream.Count;
                        TriangleStream[i]    = TempTriangle;
                        VertexStream.Add(TempVertex);
                    }
                }
                if (VertexStream[TriangleStream[i].vertex2].HasColor == false)
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex2];

                    TempVertex.Color    = ColorStream[TriangleStream[i].Color2];
                    TempVertex.HasColor = true;
                    VertexStream[TriangleStream[i].vertex2] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex2];

                    if (TempVertex.Color != ColorStream[TriangleStream[i].Color2])
                    {
                        TempVertex.Color.R = ColorStream[TriangleStream[i].Color2].R;
                        TempVertex.Color.G = ColorStream[TriangleStream[i].Color2].G;
                        TempVertex.Color.B = ColorStream[TriangleStream[i].Color2].B;
                        TempVertex.Color.A = ColorStream[TriangleStream[i].Color2].A;

                        Triangle TempTriangle = TriangleStream[i];
                        TempTriangle.vertex2 = VertexStream.Count;
                        TriangleStream[i]    = TempTriangle;
                        VertexStream.Add(TempVertex);
                    }
                }
                if (VertexStream[TriangleStream[i].vertex3].HasColor == false)
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex3];

                    TempVertex.Color    = ColorStream[TriangleStream[i].Color3];
                    TempVertex.HasColor = true;
                    VertexStream[TriangleStream[i].vertex3] = TempVertex;
                }
                else
                {
                    Vertex TempVertex = VertexStream[TriangleStream[i].vertex3];

                    if (TempVertex.Color != ColorStream[TriangleStream[i].Color3])
                    {
                        TempVertex.Color.R = ColorStream[TriangleStream[i].Color3].R;
                        TempVertex.Color.G = ColorStream[TriangleStream[i].Color3].G;
                        TempVertex.Color.B = ColorStream[TriangleStream[i].Color3].B;
                        TempVertex.Color.A = ColorStream[TriangleStream[i].Color3].A;

                        Triangle TempTriangle = TriangleStream[i];
                        TempTriangle.vertex3 = VertexStream.Count;
                        TriangleStream[i]    = TempTriangle;
                        VertexStream.Add(TempVertex);
                    }
                }
            }

            return(new ModelConverterData()
            {
                MaterialList = d.MaterialList,
                VertexList = VertexStream,
                UVList = d.UVList,
                ColorList = ColorStream,
                TriangleList = TriangleStream,
                MTLLib = d.MTLLib
            });
        }