2011-09-29 26 views
2

在WPF中構建網格的機制非常低級。例如,你必須提供頂點和索引。 WPF中有助手還是.NET 4.0框架中的任何地方我都可以使用?還是我不得不求助於第三方庫?WPF或.NET 4.0中是否存在網格構建助手類?

+0

我不知道任何,但你想構建什麼類型的網格? (例如球體,立方體,錐體,別的東西?)我已經在XNA中編寫邏輯來計算頂點和索引。以上全部爲 – bporter

+0

。我正在創建一個用於從基本操作創建原始形狀的庫。我只是想確保我從正確的水平開始。但是,如果你想分享一些來自XNA的算法,我完全是遊戲! – cdiggins

回答

1

這是我寫的一個較舊的XNA 3.1代碼塊,用於構建一個球體。我在我的渲染循環中應用了一個轉換矩陣,使我可以對它進行拉伸和定向。計算頂點非常簡單...計算指數是我發現更困難的。不過,這應該有希望給你一個想法。其他基元(例如,錐體,圓柱體,立方體......)計算起來更簡單。

m_iSegments paremeter只是允許我定義要將球體分成多少個切片......越多的切片越多,頂點越平滑。

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 
     } 
相關問題