Example #1
0
        public virtual VertexTriangle[] ToTriangles()
        {
            // Example polygons:

            //   4----3
            //  /      \
            // 5        2
            //  \      /
            //   0----1
            // Converted: 012, 023, 034, 045

            // 3---2
            // |   |
            // 0---1
            // Converted: 012, 023

            int triangleCount = Vertices.Count - 2;

            if (triangleCount < 1)
            {
                return(new VertexTriangle[0]);
            }
            VertexTriangle[] list = new VertexTriangle[triangleCount];
            for (int i = 0; i < triangleCount; ++i)
            {
                list[i] = new VertexTriangle(Vertices[0].HardCopy(), Vertices[i + 1].HardCopy(), Vertices[i + 2].HardCopy());
            }
            return(list);
        }
Example #2
0
        public void ctor()
        {
            VertexTriangle tri = new VertexTriangle(1, 2, 3, 4);

            Assert.AreEqual(1, tri.v1);
            Assert.AreEqual(2, tri.v2);
            Assert.AreEqual(3, tri.v3);
            Assert.AreEqual(4, tri.m);
        }
 public override VertexTriangle[] ToTriangles()
 {
     VertexTriangle[] triangles = new VertexTriangle[FaceCount];
     for (int i = 2, count = _vertices.Count, bit = 0; i < count; bit = ++i & 1)
     {
         triangles[i - 2] = new VertexTriangle(
             _vertices[i - 2],
             _vertices[i - 1 + bit],
             _vertices[i - bit]);
     }
     return(triangles);
 }
Example #4
0
        public void write()
        {
            MemoryStream stream = new MemoryStream();

            using BinaryWriter writer = new BinaryWriter(stream);
            VertexTriangle tri = new VertexTriangle(30806, 48282, 61662, 13330);

            tri.Write(writer);

            byte[] actual = stream.ToArray();
            Assert.AreEqual(actual, expected);
        }
Example #5
0
        public void read()
        {
            MemoryStream stream = new MemoryStream(expected, false);

            using BinaryReader reader = new BinaryReader(stream);
            VertexTriangle tri = VertexTriangle.ReadNew(reader);

            Assert.AreEqual(30806, tri.v1);
            Assert.AreEqual(48282, tri.v2);
            Assert.AreEqual(61662, tri.v3);
            Assert.AreEqual(13330, tri.m);
        }
Example #6
0
        protected override void readStruct(Stream stream)
        {
            var world = FindParentById(SectionId.World) as RWWorld;

            if (world == null)
            {
                throw new InvalidDataException("RWAtomicSection has to be child of RWWorld");
            }
            GeometryFormat worldFormat = world.format;

            using BinaryReader reader = new BinaryReader(stream);
            matIdBase = reader.ReadUInt32();
            triangles = new VertexTriangle[reader.ReadUInt32()];
            vertices  = new Vector3[reader.ReadUInt32()];
            bbox1     = reader.ReadVector3();
            bbox2     = reader.ReadVector3();
            reader.ReadUInt32(); // unused
            reader.ReadUInt32();

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i] = reader.ReadVector3();
            }

            if ((worldFormat & GeometryFormat.Normals) > 0)
            {
                normals = new Normal[vertices.Length];
                for (int i = 0; i < normals.Length; i++)
                {
                    normals[i] = Normal.ReadNew(reader);
                }
            }

            if ((worldFormat & GeometryFormat.Prelit) > 0)
            {
                colors = new IColor[vertices.Length];
                for (int i = 0; i < colors.Length; i++)
                {
                    colors[i] = IColor.ReadNew(reader);
                }
            }

            if ((worldFormat & GeometryFormat.Textured) > 0 ||
                (worldFormat & GeometryFormat.Textured2) > 0)
            {
                texCoords1 = new Vector2[vertices.Length];
                for (int i = 0; i < texCoords1.Length; i++)
                {
                    texCoords1[i] = reader.ReadVector2();
                }
            }

            if ((worldFormat & GeometryFormat.Textured2) > 0)
            {
                texCoords2 = new Vector2[vertices.Length];
                for (int i = 0; i < texCoords2.Length; i++)
                {
                    texCoords2[i] = reader.ReadVector2();
                }
            }

            for (int i = 0; i < triangles.Length; i++)
            {
                triangles[i] = VertexTriangle.ReadNew(reader);
            }
        }
Example #7
0
 internal void AddFace(VertexTriangle face)
 {
     //_triangles.Add(face);
 }
        public static void DecodePrimitives(
            Geometry geo,
            Matrix4 bindMatrix,
            InfluenceDef[] infList,
            out VertexShaderDesc info,
            out List <VertexPrimitive> lines,
            out List <VertexPolygon> faces)
        {
            info  = VertexShaderDesc.JustPositions();
            lines = new List <VertexPrimitive>();
            faces = new List <VertexPolygon>();

            Source src;
            int    boneCount = 0;

            if (infList != null)
            {
                HashSet <string> bones = new HashSet <string>();
                foreach (InfluenceDef inf in infList)
                {
                    for (int i = 0; i < inf.WeightCount; ++i)
                    {
                        bones.Add(inf.Weights[i].Bone);
                    }
                }
                boneCount = bones.Count;
            }
            info.BoneCount = boneCount;

            var m = geo.MeshElement;

            if (m == null)
            {
                return;
            }

            Vertices vertsElem;

            foreach (var prim in m.PrimitiveElements)
            {
                Dictionary <ESemantic, int> semanticCounts = new Dictionary <ESemantic, int>();
                Dictionary <ESemantic, Dictionary <int, Source> > inputSources = new Dictionary <ESemantic, Dictionary <int, Source> >();
                Dictionary <ESemantic, Source> vertexInputSources = new Dictionary <ESemantic, Source>();
                foreach (InputShared inp in prim.InputElements)
                {
                    if (inp.CommonSemanticType == ESemantic.VERTEX)
                    {
                        vertsElem = inp.Source.GetElement <Vertices>(inp.Root);
                        foreach (InputUnshared input in vertsElem.InputElements)
                        {
                            ESemantic semantic = input.CommonSemanticType;
                            if (semanticCounts.ContainsKey(semantic))
                            {
                                ++semanticCounts[semantic];
                            }
                            else
                            {
                                semanticCounts.Add(semantic, 1);
                            }

                            src = input.Source.GetElement <Source>(vertsElem.Root);
                            vertexInputSources[input.CommonSemanticType] = src;
                        }
                        continue;
                    }
                    else
                    {
                        ESemantic semantic = inp.CommonSemanticType;
                        if (semanticCounts.ContainsKey(semantic))
                        {
                            ++semanticCounts[semantic];
                        }
                        else
                        {
                            semanticCounts.Add(semantic, 1);
                        }

                        src = inp.Source.GetElement <Source>(inp.Root);
                        if (src != null)
                        {
                            if (!inputSources.ContainsKey(semantic))
                            {
                                inputSources.Add(semantic, new Dictionary <int, Source>());
                            }

                            int set = (int)inp.Set;
                            if (!inputSources[semantic].ContainsKey(set))
                            {
                                inputSources[semantic].Add(set, src);
                            }
                            else
                            {
                                inputSources[semantic][set] = src;
                            }
                        }
                    }
                }

                info.MorphCount = 0; //Morphs are stored in separate geometry entries, so they need to be combined later
                info.HasNormals = semanticCounts.ContainsKey(ESemantic.NORMAL) && semanticCounts[ESemantic.NORMAL] > 0;

                bool hasTexBinormal = semanticCounts.ContainsKey(ESemantic.TEXBINORMAL) && semanticCounts[ESemantic.TEXBINORMAL] > 0;
                bool hasBinormal    = semanticCounts.ContainsKey(ESemantic.BINORMAL) && semanticCounts[ESemantic.BINORMAL] > 0;
                info.HasBinormals = hasTexBinormal || hasBinormal;

                bool hasTexTangent = semanticCounts.ContainsKey(ESemantic.TEXTANGENT) && semanticCounts[ESemantic.TEXTANGENT] > 0;
                bool hasTangent    = semanticCounts.ContainsKey(ESemantic.TANGENT) && semanticCounts[ESemantic.TANGENT] > 0;
                info.HasTangents = hasTexTangent || hasTangent;

                info.ColorCount    = semanticCounts.ContainsKey(ESemantic.COLOR) ? semanticCounts[ESemantic.COLOR] : 0;
                info.TexcoordCount = semanticCounts.ContainsKey(ESemantic.TEXCOORD) ? semanticCounts[ESemantic.TEXCOORD] : 0;

                int maxSets = Math.Max(info.MorphCount + 1,
                                       Math.Max(info.ColorCount, info.TexcoordCount));

                Vertex[][] vertices = new Vertex[prim.PointCount][];
                int[]      indices  = prim?.IndicesElement?.StringContent?.Values;
                if (indices == null)
                {
                    WriteLine("Mesh has no face indices. Mesh will be empty.");
                    return;
                }

                Matrix4 invTranspBindMatrix = bindMatrix;
                if (info.HasNormals || info.HasBinormals || info.HasTangents)
                {
                    invTranspBindMatrix.Invert();
                    invTranspBindMatrix.Transpose();
                }

                foreach (var inp in prim.InputElements)
                {
                    int set    = (int)inp.Set;
                    int offset = (int)inp.Offset;

                    if (inp.CommonSemanticType == ESemantic.VERTEX)
                    {
                        foreach (ESemantic s in vertexInputSources.Keys)
                        {
                            src = vertexInputSources[s];
                            DecodeSource(src, s, offset, set, maxSets, prim, indices, vertices, infList, bindMatrix, invTranspBindMatrix);
                        }
                    }
                    else
                    {
                        src = inputSources[inp.CommonSemanticType][set];
                        DecodeSource(src, inp.CommonSemanticType, offset, set, maxSets, prim, indices, vertices, infList, bindMatrix, invTranspBindMatrix);
                    }
                }

                int setIndex = 0;
                switch (prim.Type)
                {
                case EColladaPrimitiveType.Lines:

                    VertexLine[] linesTemp = new VertexLine[vertices.Length / 2];
                    for (int i = 0, x = 0; i < vertices.Length; i += 2, ++x)
                    {
                        linesTemp[x] = new VertexLine(vertices[i][setIndex], vertices[i + 1][setIndex]);
                    }
                    lines.AddRange(linesTemp);

                    break;

                case EColladaPrimitiveType.Linestrips:
                    lines.Add(new VertexLineStrip(false, vertices.Select(x => x[setIndex]).ToArray()));
                    break;

                case EColladaPrimitiveType.Triangles:

                    VertexTriangle[] tris = new VertexTriangle[vertices.Length / 3];

                    for (int i = 0, x = 0; i < vertices.Length; i += 3, ++x)
                    {
                        tris[x] = new VertexTriangle(
                            vertices[i][setIndex],
                            vertices[i + 1][setIndex],
                            vertices[i + 2][setIndex]);
                    }

                    faces.AddRange(tris);
                    break;

                case EColladaPrimitiveType.Trifans:
                    faces.Add(new VertexTriangleFan(vertices.Select(x => x[setIndex]).ToArray()));
                    break;

                case EColladaPrimitiveType.Tristrips:
                    faces.Add(new VertexTriangleStrip(vertices.Select(x => x[setIndex]).ToArray()));
                    break;

                case EColladaPrimitiveType.Polylist:
                    Polylist   polyListPrim = (Polylist)prim;
                    PolyCounts countsElem   = polyListPrim.PolyCountsElement;
                    int[]      counts       = countsElem.StringContent.Values;

                    VertexPolygon[] polys = new VertexPolygon[counts.Length];

                    for (int vtxIndex = 0, polyIndex = 0; polyIndex < counts.Length; ++polyIndex)
                    {
                        int      count = counts[polyIndex];
                        Vertex[] verts = new Vertex[count];
                        for (int polyVtxIndex = 0; polyVtxIndex < count; ++polyVtxIndex, ++vtxIndex)
                        {
                            verts[polyVtxIndex] = vertices[vtxIndex][setIndex];
                        }
                        polys[polyIndex] = new VertexPolygon(verts);
                    }

                    faces.AddRange(polys);
                    break;

                default:
                case EColladaPrimitiveType.Polygons:
                    WriteLine($"Primitive type '{prim.Type.ToString()}' not supported. Mesh will be empty.");
                    break;
                }
            }
        }
Example #9
0
        protected override void Draw(GameTime gameTime)
        {
            Common.UpdateFPS(gameTime);
            GraphicsDevice.Clear(Color.Black);
            u.width   = 100;
            u.height  = 100;
            u.postion = (Common.Resolution - u.Size) / 2;
            u.Update();
            //Window.Title = u.mouseStatus.ToString() + mousePos[0].ToString();

            /*
             * var p = Common.MouseState.position;
             * vertices = new Vertex[]
             * {
             *  new Vertex(new Vector2(0 + p.X , 0f + p.Y), Color.White, new Vector2(0.5f, 0f)),
             *  new Vertex(new Vector2(5f + p.X, 200f + p.Y), Color.White, new Vector2(0.5f, 1f)),
             *  new Vertex(new Vector2(-5f + p.X, 200f + p.Y), Color.White, new Vector2(0.5f, 1f))
             * };
             * var z = Common.MouseState.position.Y / Common.Resolution.Y * 1.15f;
             * vertexBatch.Begin(swordFx.Texture, PrimitiveType.TriangleList);
             * //准备绘制,用到一个材质,顶点的类型是TriangleList
             * var v = new VertexTriangle(vertices).Rotate(timer, TriangleVertexType.A).RotationList(TriangleVertexType.A, 3.1415f).
             *  TransformPosition(Matrix.CreateScale(1f, z, 1f), Common.MouseState.position);
             * //声明新的三角形,将三角形绕点A(第一个点)旋转timer rad,从这个三角形生成RotationList,RotationList包含有序的经过旋转的三角形,索引增加三角形旋转的度数也增加,这里最多转3.1415 rad(也就是半圈)
             * //对Vertex进行变换,每个顶点x不变,y乘以z,这里z是小于1的
             * v = v.Transform(delegate (int index, Vertex vertex)
             * {
             *  return vertex.ChangeColor(vertex.Color * (index * 1f / v.vertex.Length));
             * });
             * //对Vertex进行变换,索引数值小的透明度低
             * vertexBatch.Draw(v);
             * //进行绘制
             * vertexBatch.End();
             * //真正地将它绘制到屏幕上,释放资源
             * vertexBatch.Begin(PrimitiveType.LineStrip);
             * //u.DrawBorder(vertexBatch);
             * vertexBatch.End();
             */
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            //Draw Texts
            //dtt.DrawString(spriteBatch, "开始游戏\n啥也开始不了", (Common.Resolution - dtt.MeasureString("开始游戏\n啥也开始不了", 1)) / 2, Color.White, default, 1);
            //Draw Bullets
            EntityManager.Draw(bullets, spriteBatch);
            if (mousePos[0] != mousePos[1])
            {
                float t = (mousePos[0] - mousePos[1]).Angle() + 0.7853f;
                Color c = Color.White.LinearTo(MeteorBullet.drawColor, 1, 2);
                MeteorBullet.flarefx.Draw(spriteBatch, mousePos[0], c, CenterType.MiddleCenter, 2f, t);
                MeteorBullet.flarefxAlt.Draw(spriteBatch, mousePos[0], c * 0.5f, CenterType.MiddleCenter, 5f, t);
                MeteorBullet.flarefxAlt.Draw(spriteBatch, (mousePos[0] + mousePos[1]) / 2, c * 0.5f, CenterType.MiddleCenter, 5f, t);
                MeteorBullet.flarefxAlt.Draw(spriteBatch, mousePos[1], c * 0.5f, CenterType.MiddleCenter, 5f, t);
                MeteorBullet.flarefxAlt.Draw(spriteBatch, mousePos[2], c * 0.5f, CenterType.MiddleCenter, 5f, t);
                MeteorBullet.flarefxAlt.Draw(spriteBatch, mousePos[4], c * 0.5f, CenterType.MiddleCenter, 5f, t);
            }
            else
            {
                MeteorBullet.flarefx.Draw(spriteBatch, mousePos[0], Color.White, CenterType.MiddleCenter, 1.5f, 0.7853f);
            }
            spriteBatch.End();
            //Draw Mouse
            var v = Helper.CatmullRom(mousePos, 3);

            if (Keyboard.GetState().IsKeyDown(Keys.Z))
            {
                throw new Exception(v.ToStringAlt());
            }
            vertexBatch.Begin(tex, BlendState.Additive, PrimitiveType.TriangleStrip);
            vertexBatch.Draw(VertexTriangle.Strip(v, 20, delegate(int index, Vertex vertex)
            {
                return(vertex.ChangeCoord(index * 0.25f / v.Length + (float)(Math.Sin(timer * 2) + 1) / 4, index % 2 == 0 ? 0f : 1f).ChangeColor(MeteorBullet.drawColor.LinearTo(MeteorBullet.drawColor2, index, v.Length * 0.8f) * (-index * 0.5f / v.Length + 1) * (-index * 0.5f / v.Length + 1) * 1.2f));
            }
                                                  ));
            vertexBatch.End();
            vertexBatch.Begin(tex2, BlendState.Additive, PrimitiveType.TriangleStrip);
            vertexBatch.Draw(VertexTriangle.Strip(v, 30, delegate(int index, Vertex vertex)
            {
                return(vertex.ChangeCoord((float)(Math.Sin(timer) + Math.Sin(index * 8f / v.Length) + 2) / 4, index % 2 == 0 ? 0.3f : 0.7f).ChangeColor(MeteorBullet.drawColor.LinearTo(MeteorBullet.drawColor2, index, v.Length * 0.8f) * (-index * 0.5f / v.Length + 1) * (-index * 0.5f / v.Length + 1) * 1.4f));
            }
                                                  ));
            vertexBatch.End();
            base.Draw(gameTime);
        }