c# - WPF 或 .NET 4.0 中是否有网格构建帮助程序类?

标签 c# wpf 3d mesh

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

最佳答案

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

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
       }

关于c# - WPF 或 .NET 4.0 中是否有网格构建帮助程序类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602754/

相关文章:

opengl-es - 识别立方体表面上的下一个点

wpf - 什么是网格几何?

c# - Gitversion 回滚了 AzureDevops 构建管道中我的 NuGet 包的版本号

c# - 是否可以在 C# 中创建扩展字段?

c# - 未找到入口点异常

C#:从非事件窗口发送输入

ios - 使用 isgl3D 未正确进行 3D 对象 (.pod) 中的纹理映射

c# - 我是否正确使用了 "using"语句?

c# - 在用户看到之前在另一个线程中缓存图像

.net - 媒体元素不显示