added normal computation from positions and faces

This commit is contained in:
Anselme 2015-11-18 22:36:03 +01:00
parent 1cf5765152
commit 8f0d9d5fc7
2 changed files with 21 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "meshbuilder.h"
#include <glm/ext.hpp>
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())

View File

@ -26,6 +26,8 @@ public:
void setCurrentGroupMaterial(Material* myMaterial);
int getNbGroups();
// require positions and indices
void computeNormals();
// require normals and texCoord
void computeTangents();
};