关于3d圆球顶点数据算法

来源:互联网 发布:虚无世界2java下载 编辑:程序博客网 时间:2024/04/25 09:12

//Establish constants used in sphere generation
 FLOAT rDeltaRingAngle = (D3DX_PI / m_nRings);
 FLOAT rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);

 //Generate the group of rings for the sphere
 for(nCurrentRing = 0; nCurrentRing < m_nRings + 1; nCurrentRing++)
 {
  FLOAT r0 = sinf(nCurrentRing * rDeltaRingAngle);
  FLOAT y0 = cosf(nCurrentRing * rDeltaRingAngle);

  //Generate the group of segments for the current ring
  for(nCurrentSegment = 0; nCurrentSegment < m_nSegments + 1; nCurrentSegment++)
  {
   FLOAT x0 = r0 * sinf(nCurrentSegment * rDeltaSegAngle);
   FLOAT z0 = r0 * cosf(nCurrentSegment * rDeltaSegAngle);

   vNormal.x = x0;
   vNormal.y = y0;
   vNormal.z = z0;
 
   D3DXVec3Normalize(&vNormal, &vNormal);

   //Add one vertex to the strip which makes up the sphere
   pVertex->x = x0;
   pVertex->y = y0;
   pVertex->z = z0;
   pVertex->nx = vNormal.x;
   pVertex->ny = vNormal.y;
   pVertex->nz = vNormal.z;
   pVertex->tu = 1.0f - ((FLOAT)nCurrentSegment / (FLOAT)m_nSegments);
   pVertex->tv = (FLOAT)nCurrentRing / (FLOAT)m_nRings;

   pVertex++;
   
   //Add two indices except for the last ring
   if(nCurrentRing != m_nRings)
   {
    *pIndices = wVertexIndex;
    pIndices++;
    
    *pIndices = wVertexIndex + (WORD)(m_nSegments + 1);
    pIndices++;
    
    wVertexIndex++;
   }
  }
 }

原创粉丝点击