Generating Smooth Normals from active Vertex Array

岁酱吖の 提交于 2019-12-04 15:22:06

Depending on your vertex array building procedure, indices would be the only relevant information for building your normals.

Difining normal average for one vertex is simple if you add a normal field in your vertex array, and sum all the normal calculations parsing your indices array.

You have than to divide each normal sum by the number of repetition in indices, count that you can save in a temporary array following vertex indices (incremented each time a normal is added to the vertex)

so to be more clear:

Vertex[vertexCount]: {Pos,Normal}
normalCount[vertexCount]: int count

Indices[indecesCount]: int vertexIndex

You may have 6 normals per vertex so add a temporary array of normal array to averrage those for each vertex:

NormalTemp[vertexCount][6] {x,y,z}

than parsing your indice array (if it's triangle):

for i=0 to indicesCount step 3
   for each triangle top (t from 0 to 2)
       NormalTemp[indices[i + t]][normalCount[indices[i+t]]+1] = normal calculation with cross product of vectors ending with other summits or this triangle
       normalCount[indices[i+t]]++

than you have to divide your sums by the count

for i=0 to vertexCount step 1
    for j=0 to NormalCount[i] step 1
        sum += NormalTemp[i][j]
    normal[i] = sum / normacount[i]

While I like and have voted up the j-p's answer I would still like to point out that you could get away with calculating one normal per face and just using for all 3 vertices. It would be faster, and easier, and sometimes even more accurate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!