• [14] 齿轮(Gear Wheel)图形的生成算法



    顶点数据的生成

     1 bool                        YfBuildGearwheelVertices
     2 (
     3     Yreal                   radius, 
     4     Yreal                   assistRadius, 
     5     Yreal                   height, 
     6     Yuint                   slices,
     7     YeOriginPose            originPose,
     8     Yuint                   vertexStriding, 
     9     Yuint                   vertexPos,
    10     void*                   pVerticesBuffer
    11 )
    12 {
    13     if (slices < 2 || !pVerticesBuffer)
    14     {
    15         return false;
    16     }
    17 
    18     Yuint numVertices  = slices*4 + 2;
    19 
    20     // 顶点赋值
    21     char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
    22     YsVector3* curVertexPtr   = NULL;
    23     Yuint nOffset = 0;
    24 
    25     Yreal originOffsetY = 0.0f;
    26     if (originPose == YE_ORIGIN_POSE_TOP)
    27     {
    28         originOffsetY = -height;
    29     }
    30     else if (originPose == YE_ORIGIN_POSE_CENTER)
    31     {
    32         originOffsetY = -height * 0.5f;
    33     }
    34 
    35     // 柱顶顶点赋值
    36     {
    37         nOffset = 0;   
    38         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    39         curVertexPtr->x = 0.0f;
    40         curVertexPtr->y = height + originOffsetY;
    41         curVertexPtr->z = 0.0f;
    42     }
    43 
    44     // 柱底顶点赋值
    45     {
    46         nOffset = (numVertices - 1) * vertexStriding;   
    47         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    48         curVertexPtr->x = 0.0f;
    49         curVertexPtr->y = originOffsetY;
    50         curVertexPtr->z = 0.0f;
    51     }
    52 
    53     Yreal angleXZ;
    54     Yreal posX, posZ;        
    55     Yreal fRadius;
    56     for (Yuint i = 0; i < 2*slices; i++)
    57     {
    58         angleXZ = YD_REAL_TWAIN_PI * i / (slices*2);
    59         posX = yf_sin(angleXZ);
    60         posZ = yf_cos(angleXZ);
    61         fRadius = (i%2 == 0) ? radius : assistRadius;
    62 
    63         // 上顶点
    64         {
    65             nOffset = (1 + i) * vertexStriding; 
    66             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    67             curVertexPtr->x = fRadius * posX;
    68             curVertexPtr->y = height + originOffsetY;
    69             curVertexPtr->z = fRadius * posZ;
    70         }
    71 
    72         // 下顶点
    73         {
    74             nOffset = (1 + 2*slices + i) * vertexStriding; 
    75             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    76             curVertexPtr->x = fRadius * posX;
    77             curVertexPtr->y = originOffsetY;
    78             curVertexPtr->z = fRadius * posZ;
    79         }
    80     }
    81 
    82     return true;
    83 } 

    三角形索引数据的生成

     1 bool                        YfBuildGearwheelTriIndices
     2 (
     3     Yuint                   slices,
     4     YeIndexType             indexType,
     5     Yuint                   indexStriding,  
     6     Yuint                   indexPos,
     7     void*                   pTriIndicesBuffer
     8 )
     9 {
    10     if (slices < 2 || !pTriIndicesBuffer)
    11     {
    12         return false;
    13     }
    14 
    15     Yuint numVertices  = slices*4 + 2;
    16     if (indexType == YE_INDEX_16_BIT && 
    17         numVertices > YD_MAX_UNSIGNED_INT16)
    18     {
    19         return false;
    20     }
    21     Yuint numTriangles = slices * 8;
    22 
    23     // 索引赋值
    24     char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
    25     Yuint nOffset = 0;
    26     if (indexType == YE_INDEX_16_BIT)
    27     {
    28         YsTriIndex16* triIndexPtr = NULL;
    29         for (Yuint i = 0; i < 2 * slices; i++)
    30         {
    31             nOffset = (i * 4) * indexStriding;
    32             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
    33             triIndexPtr->index0 = 0;
    34             triIndexPtr->index1 = 1 + i;
    35             triIndexPtr->index2 = 1 + (i + 1) % (2*slices);
    36 
    37             nOffset += indexStriding;
    38             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
    39             triIndexPtr->index0 = numVertices - 1;
    40             triIndexPtr->index1 = 1 + slices*2 + (i + 1) % (2*slices);
    41             triIndexPtr->index2 = 1 + slices*2 + i;
    42 
    43             nOffset += indexStriding;
    44             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
    45             triIndexPtr->index0 = 1 + i;
    46             triIndexPtr->index1 = 1 + slices*2 + i;
    47             triIndexPtr->index2 = 1 + (i + 1) % (2*slices);
    48 
    49             nOffset += indexStriding;
    50             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
    51             triIndexPtr->index0 = 1 + (i + 1) % (2*slices);
    52             triIndexPtr->index1 = 1 + slices*2 + i;
    53             triIndexPtr->index2 = 1 + slices*2 + (i + 1) % (2*slices);
    54         }
    55     }
    56     else
    57     {
    58         YsTriIndex32* triIndexPtr = NULL;
    59         for (Yuint i = 0; i < 2 * slices; i++)
    60         {
    61             nOffset = (i * 4) * indexStriding;
    62             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
    63             triIndexPtr->index0 = 0;
    64             triIndexPtr->index1 = 1 + i;
    65             triIndexPtr->index2 = 1 + (i + 1) % (2*slices);
    66 
    67             nOffset += indexStriding;
    68             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
    69             triIndexPtr->index0 = numVertices - 1;
    70             triIndexPtr->index1 = 1 + slices*2 + (i + 1) % (2*slices);
    71             triIndexPtr->index2 = 1 + slices*2 + i;
    72 
    73             nOffset += indexStriding;
    74             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
    75             triIndexPtr->index0 = 1 + i;
    76             triIndexPtr->index1 = 1 + slices*2 + i;
    77             triIndexPtr->index2 = 1 + (i + 1) % (2*slices);
    78 
    79             nOffset += indexStriding;
    80             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
    81             triIndexPtr->index0 = 1 + (i + 1) % (2*slices);
    82             triIndexPtr->index1 = 1 + slices*2 + i;
    83             triIndexPtr->index2 = 1 + slices*2 + (i + 1) % (2*slices);
    84         }
    85     }
    86 
    87     return true;
    88 }

    线框索引数据的生成

     1 bool                        YfBuildGearwheelWireIndices
     2 (
     3     Yuint                   slices,
     4     YeIndexType             indexType,
     5     Yuint                   indexStriding, 
     6     Yuint                   indexPos,
     7     void*                   pWireIndicesBuffer
     8 )
     9 {
    10     if (slices < 2 || !pWireIndicesBuffer)
    11     {
    12         return false;
    13     }
    14 
    15     Yuint numVertices = (slices*2) * 2 + 2;
    16     Yuint numLines    = (slices*2) * 5;
    17     if (indexType == YE_INDEX_16_BIT && 
    18         numVertices > YD_MAX_UNSIGNED_INT16)
    19     {
    20         return false;
    21     }
    22 
    23     // 索引赋值
    24     char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
    25     Yuint nOffset = 0;
    26     if (indexType == YE_INDEX_16_BIT)
    27     {
    28         YsLineIndex16* lineIndexPtr = NULL;
    29         for (Yuint i = 0; i < 2 * slices; i++)
    30         {
    31             nOffset = (i * 3) * indexStriding;
    32             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
    33             lineIndexPtr->index0 = 1 + i;
    34             lineIndexPtr->index1 = 1 + (i + 1)%(slices*2);
    35 
    36             nOffset += indexStriding;
    37             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
    38             lineIndexPtr->index0 = 1 + slices*2 + i;
    39             lineIndexPtr->index1 = 1 + slices*2 + (i + 1)%(slices*2);
    40 
    41             nOffset += indexStriding;
    42             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
    43             lineIndexPtr->index0 = 1 + i;
    44             lineIndexPtr->index1 = 1 + slices*2 + i;
    45         }
    46         
    47         Yuint half = 6 * slices * indexStriding;
    48         for (Yuint i = 0; i < slices; i++)
    49         {
    50             nOffset = half + (i * 2) * indexStriding;
    51             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
    52             lineIndexPtr->index0 = 0;
    53             lineIndexPtr->index1 = 1 + i*2 + 1;
    54 
    55             nOffset += indexStriding;
    56             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
    57             lineIndexPtr->index0 = numVertices - 1;
    58             lineIndexPtr->index1 = 1 + slices*2 + i*2 + 1;
    59         }
    60     }
    61     else
    62     {
    63         YsLineIndex32* lineIndexPtr = NULL;
    64         for (Yuint i = 0; i < 2 * slices; i++)
    65         {
    66             nOffset = (i * 3) * indexStriding;
    67             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
    68             lineIndexPtr->index0 = 1 + i;
    69             lineIndexPtr->index1 = 1 + (i + 1)%(slices*2);
    70 
    71             nOffset += indexStriding;
    72             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
    73             lineIndexPtr->index0 = 1 + slices*2 + i;
    74             lineIndexPtr->index1 = 1 + slices*2 + (i + 1)%(slices*2);
    75 
    76             nOffset += indexStriding;
    77             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
    78             lineIndexPtr->index0 = 1 + i;
    79             lineIndexPtr->index1 = 1 + slices*2 + i;
    80         }
    81 
    82         Yuint half = 6 * slices * indexStriding;
    83         for (Yuint i = 0; i < slices; i++)
    84         {
    85             nOffset = half + (i * 2) * indexStriding;
    86             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
    87             lineIndexPtr->index0 = 0;
    88             lineIndexPtr->index1 = 1 + i*2 + 1;
    89 
    90             nOffset += indexStriding;
    91             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
    92             lineIndexPtr->index0 = numVertices - 1;
    93             lineIndexPtr->index1 = 1 + slices*2 + i*2 + 1;
    94         }
    95     }
    96 
    97     return true;
    98 }


     

  • 相关阅读:
    Android在onCreate()中获得控件尺寸
    Java根据出生年月日获取到当前日期的年月日
    Oracle 取随机数
    filter,orderBy等过滤器
    ng-repeat 指令
    三元表达式、逻辑表达式 与 &&、||的妙用
    ng-switch 指令
    sublime text 3.0 安装 HTML-CSS-JS Prettify
    JS相关环境搭建:Nodejs、karma测试框架、jsDuck、Express
    关于VSS上的项目源码管理的注意问题
  • 原文地址:https://www.cnblogs.com/WhyEngine/p/3415256.html
Copyright © 2020-2023  润新知