void TriMesh::computeNormals() { // skip // Calculate triangle normals for(int i = 0; i < m_triangles.size(); i++){ glm::vec3 v1; glm::vec3 v2; glm::vec3 v3; HalfEdge* tempLeader = m_triangles[i]->getLeadingHalfEdge(); v1 = tempLeader->getSourceNode()->m_pos_; tempLeader = tempLeader->getNext(); v2 = tempLeader->getSourceNode()->m_pos_; tempLeader = tempLeader->getNext(); v3 = tempLeader->getSourceNode()->m_pos_; m_triangles[i]->m_N_ = calculateNormal(v1,v2,v3); } // Calculate vertex normals by averaging triange normals for(int i = 0; i < m_nodes.size(); i++){ Node n = m_nodes[i]; HalfEdge* startEdge = n.getLeadingHalfEdge(); Triangle* startTriangle = startEdge->getTriangle(); glm::vec3 accNormal = startTriangle->m_N_; int triangleCount = 1; for(HalfEdge* tempEdge = startEdge->getVtxRingNext(); tempEdge != startEdge; tempEdge = tempEdge->getVtxRingNext()){ if (tempEdge == NULL) break; accNormal += tempEdge->getTriangle()->m_N_; triangleCount++; } glm::vec3 count = glm::vec3(triangleCount); m_nodes[i].m_N_ = accNormal/count; } // unskip }
/** Return node no ix from triangle. * \param ix is either 0, 1 or 2. */ Node* getNode(size_t ix) { HalfEdge *he = m_he_; for(size_t i=0; i<ix; i++) { he = he->getNext(); } return he->getSourceNode(); }