void VisusIndexedData::computeVertexNormals() { uint32_t i,j; std::vector<IndexDataType>& poly = element(0); std::vector<VertexDataType> n; VertexDataType w; // First we reset all normals to 0 for (i=0;i<nrOfVertices();i++) { (*mVertices)[i][3] = 0; (*mVertices)[i][4] = 0; (*mVertices)[i][5] = 0; } // For each element we add the normal components weighted by // interior angle for (i=0;i<nrOfElements();i++) { poly = element(i); n = weightedNormal(vertex(poly.back()),vertex(poly[0]),vertex(poly[1])); (*mVertices)[poly[0]][3] += n[0]; (*mVertices)[poly[0]][4] += n[1]; (*mVertices)[poly[0]][5] += n[2]; for (j=1;j<poly.size()-1;j++) { n = weightedNormal(vertex(poly[j-1]),vertex(poly[j]),vertex(poly[j+1])); (*mVertices)[poly[j]][3] += n[0]; (*mVertices)[poly[j]][4] += n[1]; (*mVertices)[poly[j]][5] += n[2]; } n = weightedNormal(vertex(poly[poly.size()-2]),vertex(poly.back()),vertex(poly[0])); (*mVertices)[poly.back()][3] += n[0]; (*mVertices)[poly.back()][4] += n[1]; (*mVertices)[poly.back()][5] += n[2]; } for (i=0;i<nrOfVertices();i++) { w = vertex(i)[3]*vertex(i)[3] + vertex(i)[4]*vertex(i)[4] + vertex(i)[5]*vertex(i)[5]; if (w > 10e-8) { w = 1/sqrt(w); //fprintf(stderr,"weight %f\n",w); (*mVertices)[i][3] *= -w; (*mVertices)[i][4] *= -w; (*mVertices)[i][5] *= -w; } else { (*mVertices)[i][3] = 0; (*mVertices)[i][4] = 0; (*mVertices)[i][5] = -1; } //fprintf(stderr,"Normal %f %f %f \n",(*mVertices)[i][3],(*mVertices)[i][4],(*mVertices)[i][5]); } }
void MD5Surface::buildVertexNormals() { for (Indices::iterator j = _indices.begin(); j != _indices.end(); j += 3) { ArbitraryMeshVertex& a = _vertices[*(j + 0)]; ArbitraryMeshVertex& b = _vertices[*(j + 1)]; ArbitraryMeshVertex& c = _vertices[*(j + 2)]; Vector3 weightedNormal((c.vertex - a.vertex).crossProduct(b.vertex - a.vertex)); a.normal += weightedNormal; b.normal += weightedNormal; c.normal += weightedNormal; } // Normalise all normal vectors for (Vertices::iterator j = _vertices.begin(); j != _vertices.end(); ++j) { j->normal = Normal3f(j->normal.getNormalised()); } }