void mesh3DS::calculateNormals(){
// Doesn't take smoothing groups into account yet
if(DEBUG_OUTPUT) std::cout<<"Calculating normals... ";
m_normals.assign(m_vertices.size(), 0.0f);
Vertex vtx1, vtx2, vtx3;
Vector3DS v1, v2, faceNormal;
for(int face=0; face < int(m_faces.size()); face+=3){
// Calculate face normal
vtx1.set(m_vertices[m_faces[face]*3], m_vertices[(m_faces[face]*3)+1], m_vertices[(m_faces[face]*3)+2]);
vtx2.set(m_vertices[m_faces[face+1]*3], m_vertices[(m_faces[face+1]*3)+1], m_vertices[(m_faces[face+1]*3)+2]);
vtx3.set(m_vertices[m_faces[face+2]*3], m_vertices[(m_faces[face+2]*3)+1], m_vertices[(m_faces[face+2]*3)+2]);
v1 = vtx2 - vtx1;
v2 = vtx3 - vtx1;
faceNormal = v1.crossProduct(v2);
// Add normal to all three vertex normals
m_normals[m_faces[face]*3] += faceNormal.x;
m_normals[(m_faces[face]*3)+1] += faceNormal.y;
m_normals[(m_faces[face]*3)+2] += faceNormal.z;
m_normals[m_faces[face+1]*3] += faceNormal.x;
m_normals[(m_faces[face+1]*3)+1] += faceNormal.y;
m_normals[(m_faces[face+1]*3)+2] += faceNormal.z;
m_normals[m_faces[face+2]*3] += faceNormal.x;
m_normals[(m_faces[face+2]*3)+1] += faceNormal.y;
m_normals[(m_faces[face+2]*3)+2] += faceNormal.z;
}
//normalize all normals
for(int n=0; n < int(m_normals.size()); n+=3){
faceNormal.set(m_normals[n], m_normals[n+1], m_normals[n+2]);
faceNormal.normalize();
m_normals[n] = faceNormal.x;
m_normals[n+1] = faceNormal.y;
m_normals[n+2] = faceNormal.z;
}
if(DEBUG_OUTPUT) std::cout<<"done"<<std::endl;
}