zoukankan      html  css  js  c++  java
  • [20] 鼓状物(Drum)图形的生成算法


    顶点数据的生成

     1 bool   YfBuildDrumVertices
     2 (
     3     Yreal                   radius, 
     4     Yreal                   assistRadius,
     5     Yuint                   slices,
     6     Yuint                   stacks, 
     7     YeOriginPose            originPose,  
     8     Yuint                   vertexStriding, 
     9     Yuint                   vertexPos,
    10     void*                   pVerticesBuffer
    11 )
    12 {
    13     if (slices < 2 || stacks < 3 || !pVerticesBuffer)
    14     {
    15         return false;
    16     }
    17 
    18     Yuint numVertices  = slices * stacks + 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 = -radius;
    29     }
    30     else if (originPose == YE_ORIGIN_POSE_BOTTOM)
    31     {
    32         originOffsetY = radius;
    33     }
    34 
    35     Yreal* pSinList = YD_NEW_ARRAY(Yreal, slices);
    36     Yreal* pCosList = YD_NEW_ARRAY(Yreal, slices);
    37     Yreal angleXZ;
    38     for (Yuint j = 0; j < slices; j++)
    39     {
    40         angleXZ = YD_REAL_TWAIN_PI * j / slices;
    41         pSinList[j] = yf_sin(angleXZ);
    42         pCosList[j] = yf_cos(angleXZ);
    43     }
    44 
    45     // 赋值
    46     {
    47         // 第一个顶点
    48         {
    49             nOffset = 0;            
    50             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    51             curVertexPtr->x = 0.0f;
    52             curVertexPtr->y = radius + originOffsetY;
    53             curVertexPtr->z = 0.0f;
    54         }
    55 
    56         // 最后一个顶点
    57         {        
    58             nOffset = (numVertices - 1) * vertexStriding; 
    59             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    60             curVertexPtr->x = 0.0f;
    61             curVertexPtr->y = -radius + originOffsetY;
    62             curVertexPtr->z = 0.0f;
    63         }
    64 
    65         for (Yuint i = 0; i < stacks; i++)
    66         {
    67             Yreal angleY = YD_REAL_PI * i / (stacks - 1);
    68             Yreal posY = radius * yf_cos(angleY);
    69             Yreal radiusXZ = assistRadius + radius * yf_sin(angleY);
    70             Yreal posX, posZ;
    71 
    72             for (Yuint j = 0; j < slices; j++)
    73             {
    74                 posX = radiusXZ * pSinList[j];
    75                 posZ = radiusXZ * pCosList[j];
    76                 nOffset = (i * slices + j + 1) * vertexStriding; 
    77                 curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    78                 curVertexPtr->x = posX;
    79                 curVertexPtr->y = posY + originOffsetY;
    80                 curVertexPtr->z = posZ;
    81             }
    82         }
    83     }
    84 
    85     YD_SAFE_DELETE_ARRAY(pSinList);
    86     YD_SAFE_DELETE_ARRAY(pCosList);
    87 
    88     return true;
    89 }

    三角形索引数据的生成和线框索引数据的生成算法与球的类似

     1 bool   YfBuildDrumTriIndices
     2 (
     3     Yuint                   slices,
     4     Yuint                   stacks, 
     5     YeIndexType             indexType,
     6     Yuint                   indexStriding,  
     7     Yuint                   indexPos,
     8     void*                   pTriIndicesBuffer
     9 )
    10 {
    11     return YfBuildSphereTriIndices(
    12         slices,
    13         stacks + 2, 
    14         indexType,
    15         indexStriding,  
    16         indexPos,
    17         pTriIndicesBuffer);
    18 }
    19 
    20 bool   YfBuildDrumWireIndices
    21 (
    22     Yuint                   slices,
    23     Yuint                   stacks, 
    24     YeIndexType             indexType,
    25     Yuint                   indexStriding,  
    26     Yuint                   indexPos,
    27     void*                   pWireIndicesBuffer
    28 )
    29 {
    30     return YfBuildSphereWireIndices(
    31         slices,
    32         stacks + 2, 
    33         indexType,
    34         indexStriding,  
    35         indexPos,
    36         pWireIndicesBuffer);
    37 }


     

  • 相关阅读:
    2017-3-31 操作属性 定时器 操作内容 操作相关元素 元素创建添加删除
    2017-3-30 Js实现导航栏,选项卡,图片轮播的制作
    2017-3-30 DOM查找元素 点击,鼠标移入,移除事件 样式控制
    Js产生随机数的几种方法
    2017-3-29 Js语法 DOM操作
    纯【css下拉菜单】
    2017-3-25 css样式表 去除按钮点点击是的绿色边框
    2017-3-23 网页中的表单 框架 标题栏小图标 描点 插入视频音频 简单的滚动条 css样式表
    【2017-2-21】C#分支语句,分支嵌套,变量的作用域
    【2017-2-20】C#运算符
  • 原文地址:https://www.cnblogs.com/WhyEngine/p/3415268.html
Copyright © 2011-2022 走看看