コード例 #1
0
        protected Area ReadArea(Stream ms)
        {
            var currentZone = new Area {Content = new AreaContent()};
            var buffer = new byte[4];

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            int zoneDataVersion = BitConverter.ToInt32(buffer, 0);
            if (zoneDataVersion != 2)
                throw new InvalidDataException("Zone data version != 2 not supported!");

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Id = BitConverter.ToInt32(buffer, 0);

            ms.Read(buffer, 0, buffer.Length);
            int idStrLen = BitConverter.ToInt32(buffer, 0);
            var idStrBuffer = new byte[Utils.RoundToNextDivBy4(idStrLen)];
            ms.Read(idStrBuffer, 0, idStrBuffer.Length);
            currentZone.Name = Encoding.UTF8.GetString(idStrBuffer).TrimEnd('\0');

            ms.AssertAreaMagic();

            var attachmentPt = new Point3D();
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.X = BitConverter.ToSingle(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.Y = BitConverter.ToSingle(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            attachmentPt.Z = BitConverter.ToSingle(buffer, 0);
            currentZone.AttachmentPoint = attachmentPt;

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            var subParts = BitConverter.ToInt32(buffer, 0);

            for (int sp = 0; sp < subParts; sp++)
            {
                var aced = new AreaClipped();
                ms.Read(buffer, 0, buffer.Length);
                aced.StartTriangle = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.TriangleCount = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.StartVertex = BitConverter.ToInt32(buffer, 0);
                ms.Read(buffer, 0, buffer.Length);
                aced.VertexCount = BitConverter.ToInt32(buffer, 0);
                currentZone.Content.ClippedAreas.Add(aced);
            }

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.StartTriangle = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.TriangleCount = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.StartVertex = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderTriangle.VertexCount = BitConverter.ToInt32(buffer, 0);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderVertex.StartVertex = BitConverter.ToInt32(buffer, 0);
            ms.Read(buffer, 0, buffer.Length);
            currentZone.Content.BorderVertex.VertexCount = BitConverter.ToInt32(buffer, 0);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            int vertexCount = BitConverter.ToInt32(buffer, 0);

            ms.Read(buffer, 0, buffer.Length);
            int trianglesCount = BitConverter.ToInt32(buffer, 0);

            for (int v = 0; v < vertexCount; v++)
            {
                var curVertex = new AreaVertex();

                ms.Read(buffer, 0, buffer.Length);
                curVertex.X = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Y = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Z = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.W = BitConverter.ToSingle(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                curVertex.Center = BitConverter.ToSingle(buffer, 0);

                currentZone.Content.Vertices.Add(curVertex);
            }

            ms.AssertAreaMagic();

            for (int f = 0; f < trianglesCount; f++)
            {
                var currentTriangle = new MeshTriangularFace();

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point1 = BitConverter.ToInt32(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point2 = BitConverter.ToInt32(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                currentTriangle.Point3 = BitConverter.ToInt32(buffer, 0);

                currentZone.Content.Triangles.Add(currentTriangle);
            }

            ms.AssertAreaMagic();
            ms.Seek(4, SeekOrigin.Current);

            ms.AssertAreaMagic();

            ms.Read(buffer, 0, buffer.Length);
            if (BitConverter.ToUInt32(buffer, 0) != 809782853)
                throw new InvalidDataException("END0 expected!");

            return currentZone;
        }
コード例 #2
0
ファイル: Geometry.cs プロジェクト: Scrivener07/moddingSuite
        public static AreaContent getFromOutline(List<AreaVertex> outline)
        {
            var content = new AreaContent();

            if (!isRightHanded(outline))
            {
                outline.Reverse();
            }
            outline=outline.Select(x => { x.Center = 1; return x; }).ToList();
            content.Vertices.AddRange(outline);

            //var bottomRing = new List<AreaVertex>(outline);
            var bottomRing = outline.Select(x => {
                var y = new AreaVertex();
                y.X = x.X;
                y.Y = x.Y;
                y.Z = x.Z;
                y.W = x.W;
                y.Center = 0;
                return y; }).ToList();
            content.Vertices.AddRange(bottomRing);

            var innerRing = getInnerRing(outline);
            content.Vertices.AddRange(innerRing);

            var triangles = getTriangles(outline, 0, outline.Count - 1);
            content.Triangles.AddRange(triangles);
            var borderTriangles = getBorderTriangles(outline.Count);
            content.Triangles.AddRange(borderTriangles);

            AreaClipped ac = new AreaClipped();
            ac.StartTriangle = 0;
            ac.StartVertex = 0;
            ac.TriangleCount = triangles.Count;
            ac.VertexCount = outline.Count;
            //ac.TriangleCount = content.Triangles.Count;
            //ac.VertexCount = content.Vertices.Count;
            content.ClippedAreas.Add(ac);
            AreaClipped ac2 = new AreaClipped();
            content.ClippedAreas.Add(ac2);

            AreaClipped bt = new AreaClipped();
            bt.StartTriangle = triangles.Count;
            bt.StartVertex = outline.Count;
            bt.TriangleCount = borderTriangles.Count;
            bt.VertexCount = 2 * outline.Count;
            content.BorderTriangle = bt;

            AreaClipped bv = new AreaClipped();
            bv.StartTriangle = 0;
            bv.StartVertex = 2*outline.Count;
            bv.TriangleCount = 0;
            bv.VertexCount = outline.Count;
            content.BorderVertex = bv;

            //Console.Write("vertices=[");
            //var scen = content;
            /*foreach (var v in scen.Vertices)
            {
                Console.WriteLine("{0:G},{1:G},{2:G};", (int)v.X, (int)v.Y, (int)v.Center);
            }
            Console.WriteLine("]");

            Console.Write("tri=[");
            foreach (var v in scen.Triangles)
            {
                Console.WriteLine("{0},{1},{2};", (int)v.Point1, (int)v.Point2, (int)v.Point3);
            }
            Console.WriteLine("]");

            Console.WriteLine("bt=[{0:G},{1:G},{2:G},{3:G}]", scen.BorderTriangle.StartTriangle, scen.BorderTriangle.StartVertex, scen.BorderTriangle.TriangleCount, scen.BorderTriangle.VertexCount);
            Console.WriteLine("bv=[{0:G},{1:G},{2:G},{3:G}]", scen.BorderVertex.StartTriangle, scen.BorderVertex.StartVertex, scen.BorderVertex.TriangleCount, scen.BorderVertex.VertexCount);
            var k = 0;
            foreach (var a in scen.ClippedAreas)
            {
                Console.WriteLine("t{4}=[{0:G},{1:G},{2:G},{3:G}]", a.StartTriangle, a.StartVertex, a.TriangleCount, a.VertexCount, k++);
            }
            */
            return content;
        }