GeoMipMap地形万能索引生成器

此函数用于生成GeoMipMap地形算法的索引Buffer,可以生成一个由参数指定的特定Patch的所引数据,生成的几何体为三角形条带,生成三角形中有部分退化三角形,用于三角形条带的连接。生成的三角形是CCW或者CW一致的。

pIndex     :指向索引Buffer当前填充位置
wWidth    :Patch的大小,取值范围9,17,33......
wLevel     :Lod级别,取值范围0,1,2,3......,数字越小细节级越高,0为最高细节级
bUp,bDown,bLeft,bRight:上下左右四个方向是否连接低细节级Patch
返回值    :返回填充的索引数据个数,由于生成的是三角形条带,实际生成的三角形数目为返回值减2

int GenerateStripIndex(WORD* pIndex, WORD wWidth, WORD wLevel,  bool bUp,  bool bDown,  bool bLeft,  bool bRight)
{
     WORD i, j;
     WORD w1, w2;
     WORD wPower = 1;

     for( i=0; i<wLevel; i++ )
          wPower *= 2;

     WORD wGridWdith = (wWidth - 1) / wPower;
     WORD wPitch = wGridWdith * 2 + 4;
     WORD *p = pIndex;
     w1 = 0;

     for( j=0; j < wGridWdith / 2; j++ )
     {
          w2 = w1 + wWidth * wPower;
          for( i=0; i < wGridWdith; i++)
          {
               *p++ = w1;
               *p++ = w2;
               w1 += wPower;
               w2 += wPower;
          }

          *p++ = w1;
          *p++ = w2, *p++ = w2, *p++ = w2;

          w1 = w2 + wWidth * wPower;
          for( i=0; i < wGridWdith; i++)
          {
               *p++ = w1;
               *p++ = w2;
               w1 -= wPower;
               w2 -= wPower;
          }

          *p++ = w1;
          *p++ = w2, *p++ = w1, *p++ = w1;
     }


     if( bDown )
     {
          p = pIndex + 2;
          for( i=0; i<wGridWdith / 2; i++)
          {
               (*p) -= wPower;
               p += 4;
          }

     }


     if( bUp )
     {
          p = pIndex + wPitch * (wGridWdith - 1) + 2;
          for( i=0; i<wGridWdith / 2; i++)
          {
               (*p) += wPower;
               p += 4;
          }

     }


     if( bRight )
     {
          for( i=0; i<wGridWdith / 2; i++)
          {
               p = pIndex + wPitch * 2 * i + wPitch - 3;
               p[0] += wWidth * wPower;
               p[1] += wWidth * wPower;
               p[2] += wWidth * wPower;
               p[4] += wWidth * wPower;
          }

     }


     if( bLeft )
     {
          for( i=0; i<wGridWdith / 2; i++)
          {
               p = pIndex + wPitch * 2 * i;
               p[1] = p[0];
               p[wPitch * 2 - 3] -= wWidth * wPower;
          }

     }


     return (wGridWdith * 2 + 4) * wGridWdith - 2;
}

猜你喜欢

转载自blog.csdn.net/aoying59595512/article/details/8987790