[21] Mesh法线的生成算法
1 // 生成顶点法线 2 bool YfCalculateVertexNormal 3 ( 4 void* pNormalsBuffer, 5 Yuint normalStriding, 6 Yuint normalPos, 7 const void* pVerticesBuffer, 8 Yuint vertexNum, 9 Yuint vertexStriding, 10 Yuint vertexPos, 11 void* pIndicesBuffer, 12 Yuint trianglesNum, 13 YeIndexType indexType, 14 Yuint indexStriding, 15 Yuint indexPos 16 ) 17 { 18 if (!pNormalsBuffer || !pVerticesBuffer || !pIndicesBuffer || 19 vertexNum < 3 || trianglesNum < 1) 20 { 21 return false; 22 } 23 24 char* vertexPtr = (char*)pVerticesBuffer + vertexPos; 25 char* normalPtr = (char*)pNormalsBuffer + normalPos; 26 char* indexPtr = (char*)pIndicesBuffer + indexPos; 27 28 const YsVector* curVertexPtr[3]; 29 YsVector* curNormalPtr[3]; 30 Yuint nOffset = 0; 31 YsVector vSub_1; 32 YsVector vSub_2; 33 YsVector vCross; 34 35 for (Yuint i = 0; i < vertexNum; i++) 36 { 37 curNormalPtr[0] = (YsVector*)(normalPtr + i * normalStriding); 38 curNormalPtr[0]->SetZero(); 39 } 40 41 if (indexType == YE_INDEX_16_BIT) 42 { 43 YsTriIndex16* triIndexPtr = NULL; 44 for (Yuint i = 0; i < trianglesNum; i++) 45 { 46 triIndexPtr = (YsTriIndex16*)(indexPtr + i * indexStriding); 47 curVertexPtr[0] = (YsVector*)(vertexPtr + triIndexPtr->index0 * vertexStriding); 48 curVertexPtr[1] = (YsVector*)(vertexPtr + triIndexPtr->index1 * vertexStriding); 49 curVertexPtr[2] = (YsVector*)(vertexPtr + triIndexPtr->index2 * vertexStriding); 50 curNormalPtr[0] = (YsVector*)(normalPtr + triIndexPtr->index0 * normalStriding); 51 curNormalPtr[1] = (YsVector*)(normalPtr + triIndexPtr->index1 * normalStriding); 52 curNormalPtr[2] = (YsVector*)(normalPtr + triIndexPtr->index2 * normalStriding); 53 54 vSub_1 = *curVertexPtr[1] - *curVertexPtr[0]; 55 vSub_2 = *curVertexPtr[2] - *curVertexPtr[0]; 56 YfVector3Cross(vCross, vSub_1, vSub_2); 57 58 for (Yuint j = 0; j < 3; j++) 59 { 60 curNormalPtr[j]->x += vCross.x; 61 curNormalPtr[j]->y += vCross.y; 62 curNormalPtr[j]->z += vCross.z; 63 } 64 } 65 } 66 else 67 { 68 YsTriIndex32* triIndexPtr = NULL; 69 for (Yuint i = 0; i < trianglesNum; i++) 70 { 71 triIndexPtr = (YsTriIndex32*)(indexPtr + i * indexStriding); 72 curVertexPtr[0] = (YsVector*)(vertexPtr + triIndexPtr->index0 * vertexStriding); 73 curVertexPtr[1] = (YsVector*)(vertexPtr + triIndexPtr->index1 * vertexStriding); 74 curVertexPtr[2] = (YsVector*)(vertexPtr + triIndexPtr->index2 * vertexStriding); 75 curNormalPtr[0] = (YsVector*)(normalPtr + triIndexPtr->index0 * normalStriding); 76 curNormalPtr[1] = (YsVector*)(normalPtr + triIndexPtr->index1 * normalStriding); 77 curNormalPtr[2] = (YsVector*)(normalPtr + triIndexPtr->index2 * normalStriding); 78 79 vSub_1 = *curVertexPtr[1] - *curVertexPtr[0]; 80 vSub_2 = *curVertexPtr[2] - *curVertexPtr[0]; 81 YfVector3Cross(vCross, vSub_1, vSub_2); 82 83 for (Yuint j = 0; j < 3; j++) 84 { 85 curNormalPtr[j]->x += vCross.x; 86 curNormalPtr[j]->y += vCross.y; 87 curNormalPtr[j]->z += vCross.z; 88 } 89 } 90 } 91 92 for (Yuint i = 0; i < vertexNum; i++) 93 { 94 curNormalPtr[0] = (YsVector*)(normalPtr + i * normalStriding); 95 curNormalPtr[0]->Normalize(); 96 } 97 98 return true; 99 }