title: Generating vertices for a sphere
body: <p>In the DirectX mobile lighting sample, a cylinder is generated in the following manner:</p>
  
  <pre><code>for( DWORD i=0; i&lt;50; i++ )
              {
                  FLOAT theta = (2*D3DMX_PI*i)/(50-1);
                  pVertices[2*i+0].position = D3DMXVECTOR3( (float)sin(theta),-1.0f, (float)cos(theta) );
                  pVertices[2*i+0].normal   = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
                  pVertices[2*i+1].position = D3DMXVECTOR3( (float)sin(theta), 1.0f, (float)cos(theta) );
                  pVertices[2*i+1].normal   = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
              }
  </code></pre>
  
  <p>Is there a similar way to generate vertices for a sphere in DirectX Mobile(as a triangle strip or otherwise)? (AFAIK there's no D3DMXCreateSphere method)</p>
  
  <hr>
  
  <p>The final solution .Thanks to quarternion for all his help.</p>
  
  <pre><code>void CreateSphere()
  {
      const int iFactor = 20;
      int iPos = 0;
  
      arr_Vertices = new CUSTOMVERTEX[ui_VCount];
      ui_ShapeCount = iFactor *iFactor * 2; // use when rendering
  
      float arrV[iFactor* iFactor][3];
  
      for (DWORD j= 0; j &lt; iFactor; j ++)
      {
          FLOAT theta = (D3DMX_PI*j)/(iFactor);
  
          for( DWORD i=0; i&lt;iFactor; i++ )
          {
              iPos = j*iFactor+i;
              FLOAT phi = (2*D3DMX_PI*i)/(iFactor);
              arrV[iPos][0] = (float)(sin(theta)*cos(phi));
              arrV[iPos][1] = (float)(sin(theta)*sin(phi));
              arrV[iPos][2] = (float)(cos(theta));
  
              /*std::cout &lt;&lt; "[" &lt;&lt; j &lt;&lt;"][" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; arrV[iPos][0]  
                  &lt;&lt; "," &lt;&lt; arrV[iPos][1] &lt;&lt; "," &lt;&lt; arrV[iPos][2] &lt;&lt;std::endl;*/
          }
      }
  
      int iNext = 0;
  
      for (DWORD j= 0; j &lt; iFactor; j ++)
      { 
  
          for( DWORD i=0; i&lt;iFactor; i++ )
          {
              if (i == iFactor - 1)
                  iNext = 0;
              else iNext = i +1;
  
              iPos = (j*iFactor*6)+(i*6);
              arr_Vertices[iPos].position = D3DMXVECTOR3( arrV[j*iFactor+i][0], arrV[j*iFactor+i][1], arrV[j*iFactor+i][2]);
              arr_Vertices[iPos + 1].position = D3DMXVECTOR3( arrV[j*iFactor+iNext][0], arrV[j*iFactor+iNext][1], arrV[j*iFactor+iNext][2]);
  
  
              if (j != iFactor -1)
                  arr_Vertices[iPos + 2].position = D3DMXVECTOR3( arrV[((j+1)*iFactor)+i][0], arrV[((j+1)*iFactor)+i][1], arrV[((j+1)*iFactor)+i][2]);
              else
                  arr_Vertices[iPos + 2].position = D3DMXVECTOR3( 0, 0, -1); //Create a pseudo triangle fan for the last set of triangles
  
              arr_Vertices[iPos].normal = D3DMXVECTOR3( arr_Vertices[iPos].position.x, arr_Vertices[iPos].position.y, arr_Vertices[iPos].position.z);
              arr_Vertices[iPos + 1].normal = D3DMXVECTOR3( arr_Vertices[iPos+1].position.x, arr_Vertices[iPos+1].position.y, arr_Vertices[iPos+1].position.z);
              arr_Vertices[iPos + 2].normal = D3DMXVECTOR3( arr_Vertices[iPos+2].position.x, arr_Vertices[iPos+2].position.y, arr_Vertices[iPos+2].position.z);
  
              arr_Vertices[iPos + 3].position = D3DMXVECTOR3( arr_Vertices[iPos+2].position.x, arr_Vertices[iPos+2].position.y, arr_Vertices[iPos+2].position.z);
              arr_Vertices[iPos + 4].position = D3DMXVECTOR3( arr_Vertices[iPos+1].position.x, arr_Vertices[iPos+1].position.y, arr_Vertices[iPos+1].position.z);
  
              if (j != iFactor - 1)
                  arr_Vertices[iPos + 5].position = D3DMXVECTOR3( arrV[((j+1)*iFactor)+iNext][0], arrV[((j+1)*iFactor)+iNext][1], arrV[((j+1)*iFactor)+iNext][2]);
              else
                  arr_Vertices[iPos + 5].position = D3DMXVECTOR3( 0,0,-1);
  
              arr_Vertices[iPos + 3].normal = D3DMXVECTOR3( arr_Vertices[iPos+3].position.x, arr_Vertices[iPos+3].position.y, arr_Vertices[iPos+3].position.z);
              arr_Vertices[iPos + 4].normal = D3DMXVECTOR3( arr_Vertices[iPos+4].position.x, arr_Vertices[iPos+4].position.y, arr_Vertices[iPos+4].position.z);
              arr_Vertices[iPos + 5].normal = D3DMXVECTOR3( arr_Vertices[iPos+5].position.x, arr_Vertices[iPos+5].position.y, arr_Vertices[iPos+5].position.z);
  
              //std::cout &lt;&lt; "[" &lt;&lt; iPos &lt;&lt;"] = " &lt;&lt; arr_Vertices[iPos].position.x &lt;&lt; 
              //  "," &lt;&lt; arr_Vertices[iPos].position.y &lt;&lt;
              //  "," &lt;&lt; arr_Vertices[iPos].position.z &lt;&lt;std::endl;
  
              //std::cout &lt;&lt; "[" &lt;&lt; iPos + 1 &lt;&lt;"] = " &lt;&lt; arr_Vertices[iPos + 1].position.x &lt;&lt; 
              //  "," &lt;&lt; arr_Vertices[iPos+ 1].position.y &lt;&lt;
              //  "," &lt;&lt; arr_Vertices[iPos+ 1].position.z &lt;&lt;std::endl;
  
              //std::cout &lt;&lt; "[" &lt;&lt; iPos + 2 &lt;&lt;"] = " &lt;&lt; arr_Vertices[iPos].position.x &lt;&lt; 
              //  "," &lt;&lt; arr_Vertices[iPos + 2].position.y &lt;&lt;
              //  "," &lt;&lt; arr_Vertices[iPos + 2].position.z &lt;&lt;std::endl;
          }
      }
  }
  </code></pre>
  
  <p>Should be usable with only a few adjustments. This creates a TRIANGLELIST but could be altered to output a set of triangle strips</p>