diff --git a/meshbuilder.cpp b/meshbuilder.cpp index ba8b53b..0640898 100644 --- a/meshbuilder.cpp +++ b/meshbuilder.cpp @@ -1,4 +1,5 @@ #include "meshbuilder.h" +#include void MeshBuilder::addPosition(float x, float y, float z) { @@ -80,6 +81,24 @@ int MeshBuilder::getNbGroups() return indiceGroups.size(); } +void MeshBuilder::computeNormals() +{ + normals.resize(positions.size()); + for(const Group &g : indiceGroups) + for (int i=0; i < g.indices.size(); i += 3) + { + int v0 = g.indices[i]; + int v1 = g.indices[i+1]; + int v2 = g.indices[i+2]; + glm::vec3 n = glm::cross(positions[v1] - positions[v0], positions[v2] - positions[v0]); + normals[v0] += n; + normals[v1] += n; + normals[v2] += n; + } + for(glm::vec3 &n : normals) + n = glm::normalize(n); +} + void MeshBuilder::computeTangents() { if(!hasTexCoords()) diff --git a/meshbuilder.h b/meshbuilder.h index 0f9b978..90334fe 100644 --- a/meshbuilder.h +++ b/meshbuilder.h @@ -26,6 +26,8 @@ public: void setCurrentGroupMaterial(Material* myMaterial); int getNbGroups(); + // require positions and indices + void computeNormals(); // require normals and texCoord void computeTangents(); };