[17] 楼梯(Stairs)图形的生成算法

 感觉这图形怎么看怎么像搓衣板.

顶点数据的生成

 

bool                        YfBuildStairsVertices
(
    Yreal                   width, 
    Yreal                   length, 
    Yreal                   height, 
    Yuint                   stacks, 
    YeOriginPose            originPose,
    Yuint                   vertexStriding, 
    Yuint                   vertexPos, 
    void*                   pVerticesBuffer
)
{
    if (stacks < 1 || !pVerticesBuffer)
    {
        return false;
    }
    Yuint numVertices  = 2 + stacks * 4;
    //Yuint numTriangles = stacks * 8;

    char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
    YsVector3* curVertexPtr = NULL;
    Yuint nOffset = 0;

    YsVector3 vOriginOffset(-width / 2, -height / 2, -length / 2);
    if (originPose == YE_ORIGIN_POSE_TOP)
    {
        vOriginOffset.y = -height;
    }
    else if (originPose == YE_ORIGIN_POSE_BOTTOM)
    {
        vOriginOffset.y = 0.0f;
    }

    Yreal fStepLength = length / stacks;
    Yreal fStepHeight = height / stacks;

    for (Yuint i = 0; i <= stacks; i++)
    {
        nOffset = i * 4 * vertexStriding; 
        curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
        curVertexPtr->x = vOriginOffset.x;
        curVertexPtr->y = i * fStepHeight + vOriginOffset.y;
        curVertexPtr->z = i * fStepLength + vOriginOffset.z;

        nOffset += vertexStriding;  
        curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
        curVertexPtr->x = width + vOriginOffset.x;
        curVertexPtr->y = i * fStepHeight + vOriginOffset.y;
        curVertexPtr->z = i * fStepLength + vOriginOffset.z;

        if (i == stacks)
        {
            continue;
        }

        nOffset += vertexStriding;  
        curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
        curVertexPtr->x = vOriginOffset.x;
        curVertexPtr->y = (i+1) * fStepHeight + vOriginOffset.y;
        curVertexPtr->z = i * fStepLength + vOriginOffset.z;

        nOffset += vertexStriding;  
        curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
        curVertexPtr->x = width + vOriginOffset.x;
        curVertexPtr->y = (i+1) * fStepHeight + vOriginOffset.y;
        curVertexPtr->z = i * fStepLength + vOriginOffset.z;
    }

    return true;
}

 

三角形索引数据的生成

 

  1 bool                        YfBuildStairsTriIndices
  2 (
  3     Yuint                   stacks, 
  4     YeIndexType             indexType,
  5     Yuint                   indexStriding,  
  6     Yuint                   indexPos,
  7     void*                   pTriIndicesBuffer
  8 )
  9 {
 10     if (stacks < 1 || !pTriIndicesBuffer)
 11     {
 12         return false;
 13     }
 14     Yuint numVertices  = 2 + stacks * 4;
 15     Yuint numTriangles = stacks * 8;
 16     if (indexType == YE_INDEX_16_BIT && 
 17         numVertices > YD_MAX_UNSIGNED_INT16)
 18     {
 19         return false;
 20     }
 21 
 22     // 索引赋值
 23     char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
 24     Yuint nOffset = 0;
 25     if (indexType == YE_INDEX_16_BIT)
 26     {
 27         YsTriIndex16* triIndexPtr = NULL;
 28         for (Yuint i = 0; i < stacks; i++)
 29         {
 30             nOffset = 8 * i * indexStriding;
 31             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 32             triIndexPtr->index0 = i*4 + 0;
 33             triIndexPtr->index1 = i*4 + 2;
 34             triIndexPtr->index2 = i*4 + 1;
 35 
 36             nOffset += indexStriding;
 37             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 38             triIndexPtr->index0 = i*4 + 1;
 39             triIndexPtr->index1 = i*4 + 2;
 40             triIndexPtr->index2 = i*4 + 3;
 41 
 42             nOffset += indexStriding;
 43             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 44             triIndexPtr->index0 = i*4 + 2;
 45             triIndexPtr->index1 = i*4 + 4;
 46             triIndexPtr->index2 = i*4 + 3;
 47 
 48             nOffset += indexStriding;
 49             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 50             triIndexPtr->index0 = i*4 + 3;
 51             triIndexPtr->index1 = i*4 + 4;
 52             triIndexPtr->index2 = i*4 + 5;
 53 
 54             nOffset += indexStriding;
 55             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 56             triIndexPtr->index0 = i*4 + 0;
 57             triIndexPtr->index1 = i*4 + 1;
 58             triIndexPtr->index2 = i*4 + 5;
 59 
 60             nOffset += indexStriding;
 61             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 62             triIndexPtr->index0 = i*4 + 0;
 63             triIndexPtr->index1 = i*4 + 5;
 64             triIndexPtr->index2 = i*4 + 4;
 65 
 66             nOffset += indexStriding;
 67             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 68             triIndexPtr->index0 = i*4 + 0;
 69             triIndexPtr->index1 = i*4 + 4;
 70             triIndexPtr->index2 = i*4 + 2;
 71 
 72             nOffset += indexStriding;
 73             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 74             triIndexPtr->index0 = i*4 + 1;
 75             triIndexPtr->index1 = i*4 + 3;
 76             triIndexPtr->index2 = i*4 + 5;
 77         }
 78     }
 79     else
 80     {
 81         YsTriIndex32* triIndexPtr = NULL;
 82         for (Yuint i = 0; i < stacks; i++)
 83         {
 84             nOffset = 8 * i * indexStriding;
 85             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
 86             triIndexPtr->index0 = i*4 + 0;
 87             triIndexPtr->index1 = i*4 + 2;
 88             triIndexPtr->index2 = i*4 + 1;
 89 
 90             nOffset += indexStriding;
 91             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
 92             triIndexPtr->index0 = i*4 + 1;
 93             triIndexPtr->index1 = i*4 + 2;
 94             triIndexPtr->index2 = i*4 + 3;
 95 
 96             nOffset += indexStriding;
 97             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
 98             triIndexPtr->index0 = i*4 + 2;
 99             triIndexPtr->index1 = i*4 + 4;
100             triIndexPtr->index2 = i*4 + 3;
101 
102             nOffset += indexStriding;
103             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
104             triIndexPtr->index0 = i*4 + 3;
105             triIndexPtr->index1 = i*4 + 4;
106             triIndexPtr->index2 = i*4 + 5;
107 
108             nOffset += indexStriding;
109             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
110             triIndexPtr->index0 = i*4 + 0;
111             triIndexPtr->index1 = i*4 + 1;
112             triIndexPtr->index2 = i*4 + 5;
113 
114             nOffset += indexStriding;
115             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
116             triIndexPtr->index0 = i*4 + 0;
117             triIndexPtr->index1 = i*4 + 5;
118             triIndexPtr->index2 = i*4 + 4;
119 
120             nOffset += indexStriding;
121             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
122             triIndexPtr->index0 = i*4 + 0;
123             triIndexPtr->index1 = i*4 + 4;
124             triIndexPtr->index2 = i*4 + 2;
125 
126             nOffset += indexStriding;
127             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
128             triIndexPtr->index0 = i*4 + 1;
129             triIndexPtr->index1 = i*4 + 3;
130             triIndexPtr->index2 = i*4 + 5;
131         }
132     }
133 
134     return true;
135 }  

 

线框索引数据的生成

 

  1 bool                        YfBuildStairsWireIndices
  2 (
  3     Yuint                   stacks, 
  4     YeIndexType             indexType,
  5     Yuint                   indexStriding,  
  6     Yuint                   indexPos,
  7     void*                   pWireIndicesBuffer
  8 )
  9 {
 10     if (stacks < 3 || !pWireIndicesBuffer)
 11     {
 12         return false;
 13     }
 14 
 15     Yuint numVertices  = 2 + stacks * 4;
 16     Yuint numLines     = 1 + stacks * 8;
 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 < stacks; i++)
 30         {
 31             nOffset = (i * 8) * indexStriding;
 32             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 33             lineIndexPtr->index0 = i * 4 + 0;
 34             lineIndexPtr->index1 = i * 4 + 1;
 35 
 36             nOffset += indexStriding;
 37             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 38             lineIndexPtr->index0 = i * 4 + 2;
 39             lineIndexPtr->index1 = i * 4 + 3;
 40 
 41             nOffset += indexStriding;
 42             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 43             lineIndexPtr->index0 = i * 4 + 0;
 44             lineIndexPtr->index1 = i * 4 + 2;
 45             nOffset += indexStriding;
 46             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 47             lineIndexPtr->index0 = i * 4 + 4;
 48             lineIndexPtr->index1 = i * 4 + 2;
 49             nOffset += indexStriding;
 50             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 51             lineIndexPtr->index0 = i * 4 + 0;
 52             lineIndexPtr->index1 = i * 4 + 4;
 53 
 54             nOffset += indexStriding;
 55             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 56             lineIndexPtr->index0 = i * 4 + 1;
 57             lineIndexPtr->index1 = i * 4 + 3;
 58             nOffset += indexStriding;
 59             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 60             lineIndexPtr->index0 = i * 4 + 5;
 61             lineIndexPtr->index1 = i * 4 + 3;
 62             nOffset += indexStriding;
 63             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 64             lineIndexPtr->index0 = i * 4 + 1;
 65             lineIndexPtr->index1 = i * 4 + 5;
 66         }
 67 
 68         nOffset = (stacks * 8) * indexStriding;
 69         lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 70         lineIndexPtr->index0 = stacks * 4;
 71         lineIndexPtr->index1 = stacks * 4 + 1;
 72     }
 73     else
 74     {
 75         YsLineIndex32* lineIndexPtr = NULL;
 76         for (Yuint i = 0; i < stacks; i++)
 77         {
 78             nOffset = (i * 8) * indexStriding;
 79             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 80             lineIndexPtr->index0 = i * 4 + 0;
 81             lineIndexPtr->index1 = i * 4 + 1;
 82 
 83             nOffset += indexStriding;
 84             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 85             lineIndexPtr->index0 = i * 4 + 2;
 86             lineIndexPtr->index1 = i * 4 + 3;
 87 
 88             nOffset += indexStriding;
 89             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 90             lineIndexPtr->index0 = i * 4 + 0;
 91             lineIndexPtr->index1 = i * 4 + 2;
 92             nOffset += indexStriding;
 93             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 94             lineIndexPtr->index0 = i * 4 + 4;
 95             lineIndexPtr->index1 = i * 4 + 2;
 96             nOffset += indexStriding;
 97             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 98             lineIndexPtr->index0 = i * 4 + 0;
 99             lineIndexPtr->index1 = i * 4 + 4;
100 
101             nOffset += indexStriding;
102             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
103             lineIndexPtr->index0 = i * 4 + 1;
104             lineIndexPtr->index1 = i * 4 + 3;
105             nOffset += indexStriding;
106             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
107             lineIndexPtr->index0 = i * 4 + 5;
108             lineIndexPtr->index1 = i * 4 + 3;
109             nOffset += indexStriding;
110             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
111             lineIndexPtr->index0 = i * 4 + 1;
112             lineIndexPtr->index1 = i * 4 + 5;
113         }
114 
115         nOffset = (stacks * 8) * indexStriding;
116         lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
117         lineIndexPtr->index0 = stacks * 4;
118         lineIndexPtr->index1 = stacks * 4 + 1;
119     }
120 
121     return true;
122 } 

 

posted on 2013-11-09 08:59  叶飞影  阅读(668)  评论(0编辑  收藏  举报