• [2] 立方体(Box)图形的生成算法



    顶点数据的生成

     1 bool                        YfBuildBoxVertices
     2 (
     3     Yreal                   extentX,
     4     Yreal                   extentY,  
     5     Yreal                   extentZ, 
     6     YeOriginPose            originPose,
     7     Yuint                   vertexStriding,
     8     Yuint                   vertexPos,
     9     void*                   pVerticesBuffer
    10 )
    11 {
    12     if (!pVerticesBuffer)
    13     {
    14         return false;
    15     }
    16 
    17     Yuint numVertices = 8;
    18 
    19     // 顶点赋值
    20     char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
    21 
    22     YsVector3* curVertexPtr   = NULL;
    23     YsVector3* curNormalPtr   = NULL;
    24     YsVector2* curTexcoordPtr = NULL;
    25 
    26     Yuint nOffset = 0;
    27 
    28     YsVector3 vOriginOffset(-extentX / 2, -extentY / 2, -extentZ / 2);
    29     if (originPose == YE_ORIGIN_POSE_TOP)
    30     {
    31         vOriginOffset.y = -extentY;
    32     }
    33     else if (originPose == YE_ORIGIN_POSE_BOTTOM)
    34     {
    35         vOriginOffset.y = 0.0f;
    36     }
    37 
    38     for (Yuint k = 0; k < 2; k++)
    39     {
    40         for (Yuint j = 0; j < 2; j++)
    41         {
    42             for (Yuint i = 0; i < 2; i++)
    43             {
    44                 nOffset = (k*4 + j*2 + i) * vertexStriding;
    45                 curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
    46                 curVertexPtr->x = extentX*i + vOriginOffset.x;
    47                 curVertexPtr->z = extentZ*j + vOriginOffset.z;
    48                 curVertexPtr->y = extentY*k + vOriginOffset.y;
    49             }
    50         }
    51     }
    52 
    53     return true;
    54 }  

    三角形索引数据的生成

     1 bool                        YfBuildBoxTriIndices
     2 (
     3     YeIndexType             indexType,
     4     Yuint                   indexStriding,  
     5     Yuint                   indexPos,
     6     void*                   pTriIndicesBuffer
     7 )
     8 {
     9     if (!pTriIndicesBuffer)
    10     {
    11         return false;
    12     }
    13 
    14     char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
    15     if (indexType == YE_INDEX_16_BIT)
    16     {
    17         YsTriIndex16* triIndexPtr[12];
    18         for (Yuint i = 0; i < 12; i++)
    19         {
    20             triIndexPtr[i] = (YsTriIndex16*)(indexPtr + i * indexStriding);
    21         }
    22 
    23         // bottom
    24         triIndexPtr[0]->index0 = 0; triIndexPtr[0]->index1 = 1; triIndexPtr[0]->index2 = 3;
    25         triIndexPtr[1]->index0 = 0; triIndexPtr[1]->index1 = 3; triIndexPtr[1]->index2 = 2;
    26 
    27         // top                                                   
    28         triIndexPtr[10]->index0= 4; triIndexPtr[10]->index1= 6; triIndexPtr[10]->index2= 7;
    29         triIndexPtr[11]->index0= 4; triIndexPtr[11]->index1= 7; triIndexPtr[11]->index2= 5;
    30 
    31         // front side
    32         triIndexPtr[2]->index0 = 0; triIndexPtr[2]->index1 = 4; triIndexPtr[2]->index2 = 5;
    33         triIndexPtr[3]->index0 = 0; triIndexPtr[3]->index1 = 5; triIndexPtr[3]->index2 = 1;
    34 
    35         // back side
    36         triIndexPtr[4]->index0 = 2; triIndexPtr[4]->index1 = 3; triIndexPtr[4]->index2 = 7;
    37         triIndexPtr[5]->index0 = 2; triIndexPtr[5]->index1 = 7; triIndexPtr[5]->index2 = 6;
    38 
    39         // right side
    40         triIndexPtr[6]->index0 = 1; triIndexPtr[6]->index1 = 5; triIndexPtr[6]->index2 = 7;
    41         triIndexPtr[7]->index0 = 1; triIndexPtr[7]->index1 = 7; triIndexPtr[7]->index2 = 3;
    42 
    43         // left side                                                       
    44         triIndexPtr[8]->index0 = 0; triIndexPtr[8]->index1 = 2; triIndexPtr[8]->index2 = 6;
    45         triIndexPtr[9]->index0 = 0; triIndexPtr[9]->index1 = 6; triIndexPtr[9]->index2 = 4;
    46     }
    47     else
    48     {
    49         YsTriIndex32* triIndexPtr[12];
    50         for (Yuint i = 0; i < 12; i++)
    51         {
    52             triIndexPtr[i] = (YsTriIndex32*)(indexPtr + i * indexStriding);
    53         }
    54 
    55         // bottom
    56         triIndexPtr[0]->index0 = 0; triIndexPtr[0]->index1 = 1; triIndexPtr[0]->index2 = 3;
    57         triIndexPtr[1]->index0 = 0; triIndexPtr[1]->index1 = 3; triIndexPtr[1]->index2 = 2;
    58 
    59         // top                                                   
    60         triIndexPtr[10]->index0= 4; triIndexPtr[10]->index1= 6; triIndexPtr[10]->index2= 7;
    61         triIndexPtr[11]->index0= 4; triIndexPtr[11]->index1= 7; triIndexPtr[11]->index2= 5;
    62 
    63         // front side
    64         triIndexPtr[2]->index0 = 0; triIndexPtr[2]->index1 = 4; triIndexPtr[2]->index2 = 5;
    65         triIndexPtr[3]->index0 = 0; triIndexPtr[3]->index1 = 5; triIndexPtr[3]->index2 = 1;
    66 
    67         // back side
    68         triIndexPtr[4]->index0 = 2; triIndexPtr[4]->index1 = 3; triIndexPtr[4]->index2 = 7;
    69         triIndexPtr[5]->index0 = 2; triIndexPtr[5]->index1 = 7; triIndexPtr[5]->index2 = 6;
    70 
    71         // right side
    72         triIndexPtr[6]->index0 = 1; triIndexPtr[6]->index1 = 5; triIndexPtr[6]->index2 = 7;
    73         triIndexPtr[7]->index0 = 1; triIndexPtr[7]->index1 = 7; triIndexPtr[7]->index2 = 3;
    74 
    75         // left side                                                       
    76         triIndexPtr[8]->index0 = 0; triIndexPtr[8]->index1 = 2; triIndexPtr[8]->index2 = 6;
    77         triIndexPtr[9]->index0 = 0; triIndexPtr[9]->index1 = 6; triIndexPtr[9]->index2 = 4;
    78     }
    79 
    80     return true;
    81 }  

    线框索引数据的生成

     1 bool                        YfBuildBoxWireIndices
     2 (
     3     YeIndexType             indexType,
     4     Yuint                   indexStriding,  
     5     Yuint                   indexPos,
     6     void*                   pWireIndicesBuffer
     7 )
     8 {
     9     if (!pWireIndicesBuffer)
    10     {
    11         return false;
    12     }
    13 
    14     char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
    15     if (indexType == YE_INDEX_16_BIT)
    16     {
    17         YsLineIndex16* lineIndexPtr[12];
    18         for (Yuint i = 0; i < 12; i++)
    19         {
    20             lineIndexPtr[i] = (YsLineIndex16*)(indexPtr + i * indexStriding);
    21         }
    22 
    23         // bottom
    24         lineIndexPtr[0]->index0 = 0; lineIndexPtr[0]->index1 = 1; 
    25         lineIndexPtr[1]->index0 = 0; lineIndexPtr[1]->index1 = 2; 
    26         lineIndexPtr[2]->index0 = 3; lineIndexPtr[2]->index1 = 1; 
    27         lineIndexPtr[3]->index0 = 3; lineIndexPtr[3]->index1 = 2; 
    28 
    29         // center
    30         lineIndexPtr[4]->index0 = 0; lineIndexPtr[4]->index1 = 4; 
    31         lineIndexPtr[5]->index0 = 1; lineIndexPtr[5]->index1 = 5; 
    32         lineIndexPtr[6]->index0 = 2; lineIndexPtr[6]->index1 = 6; 
    33         lineIndexPtr[7]->index0 = 3; lineIndexPtr[7]->index1 = 7; 
    34                                         
    35         // top                                               
    36         lineIndexPtr[8]->index0 = 4; lineIndexPtr[8]->index1 = 5; 
    37         lineIndexPtr[9]->index0 = 4; lineIndexPtr[9]->index1 = 6; 
    38         lineIndexPtr[10]->index0= 7; lineIndexPtr[10]->index1= 5; 
    39         lineIndexPtr[11]->index0= 7; lineIndexPtr[11]->index1= 6;    
    40     }
    41     else
    42     {
    43         YsLineIndex32* lineIndexPtr[12];
    44         for (Yuint i = 0; i < 12; i++)
    45         {
    46             lineIndexPtr[i] = (YsLineIndex32*)(indexPtr + i * indexStriding);
    47         }
    48 
    49         // bottom
    50         lineIndexPtr[0]->index0 = 0; lineIndexPtr[0]->index1 = 1; 
    51         lineIndexPtr[1]->index0 = 0; lineIndexPtr[1]->index1 = 2; 
    52         lineIndexPtr[2]->index0 = 3; lineIndexPtr[2]->index1 = 1; 
    53         lineIndexPtr[3]->index0 = 3; lineIndexPtr[3]->index1 = 2; 
    54 
    55         // center
    56         lineIndexPtr[4]->index0 = 0; lineIndexPtr[4]->index1 = 4; 
    57         lineIndexPtr[5]->index0 = 1; lineIndexPtr[5]->index1 = 5; 
    58         lineIndexPtr[6]->index0 = 2; lineIndexPtr[6]->index1 = 6; 
    59         lineIndexPtr[7]->index0 = 3; lineIndexPtr[7]->index1 = 7; 
    60                                         
    61         // top                                               
    62         lineIndexPtr[8]->index0 = 4; lineIndexPtr[8]->index1 = 5; 
    63         lineIndexPtr[9]->index0 = 4; lineIndexPtr[9]->index1 = 6; 
    64         lineIndexPtr[10]->index0= 7; lineIndexPtr[10]->index1= 5; 
    65         lineIndexPtr[11]->index0= 7; lineIndexPtr[11]->index1= 6;  
    66     }
    67 
    68     return true;
    69 }  
  • 相关阅读:
    [Luogu5042/UOJ #100][国家集训队互测2015]丢失的题面/ydc的题面
    [51nod1773]A国的贸易
    [GZOI2019&GXOI2019]省选GG记
    [51nod1659]数方块
    [51nod1052]最大M子段和
    [51nod1201]整数划分
    [51nod1084]矩阵取数问题 V2
    [51nod1020]逆序排列
    [BZOJ3000]Big Number
    [BZOJ1684][Usaco2005 Oct]Close Encounter
  • 原文地址:https://www.cnblogs.com/WhyEngine/p/3415235.html
Copyright © 2020-2023  润新知