WPF 或 .NET 4.0 中是否有网格构建帮助器类?

发布于 2024-12-07 20:02:30 字数 92 浏览 0 评论 0原文

在 WPF 中构建网格的机制非常低级。例如,您必须提供顶点和索引。 WPF 或 .NET 4.0 框架中的任何位置是否有我可以使用的帮助程序?或者我必须求助于第三方库吗?

The mechanisms for building a mesh in WPF are quite low-level. For example you have to supply the vertexes and the indexes. Are there helpers in the WPF or anywhere in the .NET 4.0 framework I can use? Or do I have to resort to third party libraries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-12-14 20:02:30

这是我为构建 Sphere 编写的一段较旧的 XNA 3.1 代码。我在渲染循环中应用了一个变换矩阵,它允许我拉伸和定向它。计算顶点相当简单......我发现计算索引更困难。不过,这应该会给你一个想法。其他基元(例如圆锥体、圆柱体、立方体...)的计算要简单得多。

m_iSegments 参数仅允许我定义要将球体划分为多少个切片...段越多,顶点越多,球体越平滑。

m_Appearance 参数是我对着色器的包装。

        /// <summary>
        /// This method constructs ellipsoid vertices, indices, and normals.
        /// Equations are performed using the parameterized equations:
        /// 
        /// x = a cos(B)cos(L)
        /// y = b cos(B)sin(L)
        /// z = c sin(B)
        /// 
        /// Where:
        /// 
        /// B = latitude and,
        /// L = longitude
        /// 
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Ellipsoid">Wikipedia - Ellipsoid</seealso>
        public override void BuildVertices()
        {
            #region Declarations

            int iIndex = 0;                                     // Stores the index of the vertex array.
            int iBeta = 0;                                      // Stores the beta increment.
            int iLambda = 0;                                    // Stores the lambda increment.
            float Beta = 0.0f;                                  // Beta0 - Stores the latitude.
            float Lambda = 0.0f;                                // Lambda0 - Stores the longitude.
            float BetaStep = MathHelper.Pi / m_iSegments;       // Latitude Segements, in degrees.
            float LambdaStep = MathHelper.TwoPi / m_iSegments;  // Longitude Segments, in degrees.
            Vector3 vectPos = Vector3.Zero;                     // Vertex Position Vector
            Vector3 vectNor = Vector3.Zero;                     // Vertex Normal Vector
            Vector2 vectTex = Vector2.Zero;                     // Vertex Texture Coordinate

            #endregion

            #region Build the vertices.

            int[] iIndices = new int[6 * m_iSegments * m_iSegments];
            Vector3[] vVertices = new Vector3[(m_iSegments + 1) * (m_iSegments + 1)];
            Vector2[] vTexCrds  = new Vector2[vVertices.Length];

            iIndex = 0;

            for (iBeta = 0; iBeta <= m_iSegments; iBeta++)
            {
                // Compute the latitude.
                Beta = MathHelper.Clamp((-MathHelper.PiOver2) + (iBeta * BetaStep), -MathHelper.PiOver2, MathHelper.PiOver2);

                for (iLambda = 0; iLambda <= m_iSegments; iLambda++)
                {
                    // Compute the current longitude.
                    Lambda = MathHelper.Clamp((-MathHelper.Pi) + (iLambda * LambdaStep), -MathHelper.Pi, MathHelper.Pi);

                    // Compute the current vertex.
                    vVertices[iIndex] = new Vector3((float)(Math.Cos(Beta) * Math.Sin(Lambda)),
                                                    (float)(Math.Sin(Beta)),
                                                    (float)(Math.Cos(Beta) * Math.Cos(Lambda)));

                    // Compute the triangle indices.
                    if (iBeta < m_iSegments &&
                        iLambda < m_iSegments)
                    {
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 0] = iIndex;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 1] = iIndex + m_iSegments + 1;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 2] = iIndex + m_iSegments + 2;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 3] = iIndex;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 4] = iIndex + m_iSegments + 2;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 5] = iIndex + 1;
                    }

                    // Compute the texture coordinates.
                    vTexCrds[iIndex] = new Vector2((float)iLambda / (float)m_iSegments, 1.0f - (float)iBeta / (float)m_iSegments);

                    iIndex++;
                }

            }

            # endregion

            #region Build the normals.

            Vector3[] vNormals = new Vector3[vVertices.Length];
            for (iIndex = 0; iIndex < vVertices.Length; iIndex++)
            {
                vNormals[iIndex] = vVertices[iIndex] - this.AbsolutePosition;
                vNormals[iIndex].Normalize();
            }

            #endregion

            #region Build the buffers.

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[vVertices.Length];
            for (iIndex = 0; iIndex < vVertices.Length; iIndex++)
                vertices[iIndex] = new VertexPositionNormalTexture(vVertices[iIndex], vNormals[iIndex], vTexCrds[iIndex]);
            m_pAppearance.SetBuffers(vertices, iIndices);

            #endregion
       }

Here's an older chunk of XNA 3.1 code I wrote to build a Sphere. I apply a transformation matrix in my rendering loop that allows me to stretch and orient it. Computing the vertices is fairly straightforward... computing the indices are what I find more difficult. This should hopefully give you an idea, though. The other primitives (e.g. cone, cylinder, cube...) are much simpler to compute.

The m_iSegments paremeter just allows me to define how many slices I want to divide the sphere into... the more segments, the more vertices, the smoother the sphere.

The m_Appearance parameter is my wrapper for the shader.

        /// <summary>
        /// This method constructs ellipsoid vertices, indices, and normals.
        /// Equations are performed using the parameterized equations:
        /// 
        /// x = a cos(B)cos(L)
        /// y = b cos(B)sin(L)
        /// z = c sin(B)
        /// 
        /// Where:
        /// 
        /// B = latitude and,
        /// L = longitude
        /// 
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Ellipsoid">Wikipedia - Ellipsoid</seealso>
        public override void BuildVertices()
        {
            #region Declarations

            int iIndex = 0;                                     // Stores the index of the vertex array.
            int iBeta = 0;                                      // Stores the beta increment.
            int iLambda = 0;                                    // Stores the lambda increment.
            float Beta = 0.0f;                                  // Beta0 - Stores the latitude.
            float Lambda = 0.0f;                                // Lambda0 - Stores the longitude.
            float BetaStep = MathHelper.Pi / m_iSegments;       // Latitude Segements, in degrees.
            float LambdaStep = MathHelper.TwoPi / m_iSegments;  // Longitude Segments, in degrees.
            Vector3 vectPos = Vector3.Zero;                     // Vertex Position Vector
            Vector3 vectNor = Vector3.Zero;                     // Vertex Normal Vector
            Vector2 vectTex = Vector2.Zero;                     // Vertex Texture Coordinate

            #endregion

            #region Build the vertices.

            int[] iIndices = new int[6 * m_iSegments * m_iSegments];
            Vector3[] vVertices = new Vector3[(m_iSegments + 1) * (m_iSegments + 1)];
            Vector2[] vTexCrds  = new Vector2[vVertices.Length];

            iIndex = 0;

            for (iBeta = 0; iBeta <= m_iSegments; iBeta++)
            {
                // Compute the latitude.
                Beta = MathHelper.Clamp((-MathHelper.PiOver2) + (iBeta * BetaStep), -MathHelper.PiOver2, MathHelper.PiOver2);

                for (iLambda = 0; iLambda <= m_iSegments; iLambda++)
                {
                    // Compute the current longitude.
                    Lambda = MathHelper.Clamp((-MathHelper.Pi) + (iLambda * LambdaStep), -MathHelper.Pi, MathHelper.Pi);

                    // Compute the current vertex.
                    vVertices[iIndex] = new Vector3((float)(Math.Cos(Beta) * Math.Sin(Lambda)),
                                                    (float)(Math.Sin(Beta)),
                                                    (float)(Math.Cos(Beta) * Math.Cos(Lambda)));

                    // Compute the triangle indices.
                    if (iBeta < m_iSegments &&
                        iLambda < m_iSegments)
                    {
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 0] = iIndex;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 1] = iIndex + m_iSegments + 1;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 2] = iIndex + m_iSegments + 2;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 3] = iIndex;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 4] = iIndex + m_iSegments + 2;
                        iIndices[iIndex + (iIndex * 5) - (iBeta * 6) + 5] = iIndex + 1;
                    }

                    // Compute the texture coordinates.
                    vTexCrds[iIndex] = new Vector2((float)iLambda / (float)m_iSegments, 1.0f - (float)iBeta / (float)m_iSegments);

                    iIndex++;
                }

            }

            # endregion

            #region Build the normals.

            Vector3[] vNormals = new Vector3[vVertices.Length];
            for (iIndex = 0; iIndex < vVertices.Length; iIndex++)
            {
                vNormals[iIndex] = vVertices[iIndex] - this.AbsolutePosition;
                vNormals[iIndex].Normalize();
            }

            #endregion

            #region Build the buffers.

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[vVertices.Length];
            for (iIndex = 0; iIndex < vVertices.Length; iIndex++)
                vertices[iIndex] = new VertexPositionNormalTexture(vVertices[iIndex], vNormals[iIndex], vTexCrds[iIndex]);
            m_pAppearance.SetBuffers(vertices, iIndices);

            #endregion
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文