예제 #1
0
파일: BSP.cs 프로젝트: yarligayan/libTech
        public static libTechMap LoadMap(Stream BSPStream)
        {
            Q3BSP Map = Q3BSP.FromStream(BSPStream);

            libTechMap Q3Map = new libTechMap();
            Dictionary <int, List <Vertex3> > TexturedMeshes = new Dictionary <int, List <Vertex3> >();

            foreach (var FaceIdx in GetFaceIndices(Map))
            {
                Q3BSPFace Face = Map.Faces[FaceIdx];

                if (!TexturedMeshes.ContainsKey(Face.Texture))
                {
                    TexturedMeshes.Add(Face.Texture, new List <Vertex3>());
                }

                if (Face.Type == FaceType.Polygon || Face.Type == FaceType.Mesh)
                {
                    Vertex3[] Vertices = new Vertex3[Face.NVerts];
                    for (int VertOffset = 0; VertOffset < Face.NVerts; VertOffset++)
                    {
                        Vertices[VertOffset] = Map.Vertices[Face.Vert + VertOffset].ToVertex3();
                    }

                    for (int MeshVertOffset = 0; MeshVertOffset < Face.NMeshVerts; MeshVertOffset++)
                    {
                        TexturedMeshes[Face.Texture].Add(Vertices[Map.MeshVertices[Face.MeshVert + MeshVertOffset]]);
                    }
                }
            }

            string[] TextureNames = Map.Textures.Select(T => T.GetName()).ToArray();

            libTechModel Model = new libTechModel();

            Q3Map.AddModel(Model);

            foreach (var KV in TexturedMeshes)
            {
                string TexName = TextureNames[KV.Key];

                if (TexName == "textures/lun3dm5/lblusky2")
                {
                    continue;
                }

                Model.AddMesh(new libTechMesh(KV.Value.ToArray().Reverse().ToArray(), Engine.GetMaterial(TexName)));
            }

            return(Q3Map);
        }
예제 #2
0
파일: BSP.cs 프로젝트: yarligayan/libTech
        public static libTechMap LoadMap(string FilePath)
        {
            // TODO: Ugh, fix
            FilePath = Path.GetFullPath("." + FilePath);

            libTechMap   Map             = new libTechMap();
            libTechModel CurrentMapModel = null;

            using (ValveBspFile BSP = new ValveBspFile(FilePath)) {
                string BSPProviderPath = Path.GetFileName(FilePath);
                Engine.VFS.GetSourceProvider().Add(BSPProviderPath, BSP.PakFile);

                Face[]        Faces     = BSP.Faces.ToArray();
                SVector3[]    Verts     = BSP.Vertices.ToArray();
                BspModel[]    Models    = BSP.Models.ToArray();
                Edge[]        Edges     = BSP.Edges.ToArray();
                int[]         SurfEdges = BSP.SurfEdges.ToArray();
                TextureInfo[] TexInfos  = BSP.TextureInfos.ToArray();
                TextureData[] TexDatas  = BSP.TextureData.ToArray();
                Brush[]       Brushes   = BSP.Brushes.ToArray();
                BrushSide[]   Sides     = BSP.BrushSides.ToArray();

                foreach (var VEnt in BSP.Entities)
                {
                    Vector3    Angles  = ToVec3(VEnt.Angles) * ((float)Math.PI / 360) + new Vector3(-((float)Math.PI / 2), 0, 0);;
                    Vector3    Origin  = ToVec3(VEnt.Origin);
                    Quaternion QAngles = Quaternion.CreateFromYawPitchRoll(Angles.Y, Angles.X, Angles.Z);

                    EntityKeyValues KVs = new EntityKeyValues(Map);
                    KVs.Add(new EntityKeyValue("origin", Origin));
                    KVs.Add(new EntityKeyValue("angles", Angles));
                    KVs.Add(new EntityKeyValue("qangles", QAngles));

                    foreach (var Name in VEnt.PropertyNames)
                    {
                        if (!KVs.Contains(Name))
                        {
                            KVs.Add(new EntityKeyValue(Name, (string)VEnt[Name]));
                        }
                    }

                    Entity Ent = Entity.CreateInstance(VEnt.ClassName, KVs);

                    if (Ent == null)
                    {
                        continue;
                    }

                    Map.SpawnEntity(Ent);
                }

                List <Vertex3> DispTriangleStrip = new List <Vertex3>();

                for (int ModelIdx = 0; ModelIdx < Models.Length; ModelIdx++)
                {
                    ref BspModel Model = ref Models[ModelIdx];

                    CurrentMapModel = new libTechModel();
                    if (ModelIdx > 0)
                    {
                        CurrentMapModel.Enabled = false;
                    }

                    Dictionary <string, List <Vertex3> > TexturedMeshes = new Dictionary <string, List <Vertex3> >();

                    for (int FaceIdx = Model.FirstFace; FaceIdx < Model.FirstFace + Model.NumFaces; FaceIdx++)
                    {
                        ref Face        Face    = ref Faces[FaceIdx];
                        ref TextureInfo TexInfo = ref TexInfos[Face.TexInfo];

                        bool IsSkybox = (TexInfo.Flags & (SurfFlags.SKY | SurfFlags.SKY2D)) != 0;

                        if ((TexInfo.Flags & (SurfFlags.NODRAW | SurfFlags.LIGHT)) != 0 || IsSkybox)
                        {
                            continue;
                        }

                        ref TextureData TexData  = ref TexDatas[TexInfo.TexData];