void MeshData::GenerateNormals( const bool& InvertNormals) { // Sources const AttribData<vec3> & Vertices = m_VertexArray; const AttribData<poly> & Faces = m_Polygons; vec3* pNormals = new vec3[Vertices.Size()]; memset(pNormals, 0, Vertices.Size() * sizeof(vec3)); // Go through the polylists const uint NrPolyLists = Faces.Size(); for(uint p=0; p<NrPolyLists; p++) { const poly Polylist = Faces.GetElem(p); const uint NrFaces = Polylist.NrFaces(); for(uint f=0; f<NrFaces; f++) { // Get vertices const uint i1 = Polylist.GetFaces().GetElem(f).v1; const uint i2 = Polylist.GetFaces().GetElem(f).v2; const uint i3 = Polylist.GetFaces().GetElem(f).v3; const vec3 v1 = Vertices.GetElem(i1); const vec3 v2 = Vertices.GetElem(i2); const vec3 v3 = Vertices.GetElem(i3); // Get edges const vec3 edge1 = v2-v1; const vec3 edge2 = v3-v1; // Set Crossproduct vec3 facenormal; if(InvertNormals) { facenormal=normalize(glm::cross(edge1, edge2)); facenormal = facenormal * -1.0f; } else facenormal=normalize(glm::cross(edge1, edge2)); // Add vertexnormals pNormals[i1]+=facenormal; pNormals[i2]+=facenormal; pNormals[i3]+=facenormal; } } // Normalize vec3* pN = pNormals; for(uint n=0; n<Vertices.Size(); n++, pN++) *pN = normalize(*pN); MeshData::SetNormalArray().Load(pNormals, Vertices.Size()); delete [] pNormals; }