Created
July 11, 2014 07:58
-
-
Save fishkingsin/bbfb7e248b81635944be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Universal function which sets normals for the triangle mesh | |
void setNormals( ofMesh &mesh ){ | |
//The number of the vertices | |
int nV = mesh.getNumVertices(); | |
//The number of the triangles | |
int nT = mesh.getNumIndices() / 3; | |
vector<ofPoint> norm( nV ); //Array for the normals | |
//Scan all the triangles. For each triangle add its | |
//normal to norm's vectors of triangle's vertices | |
for (int t=0; t<nT; t++) { | |
//Get indices of the triangle t | |
int i1 = mesh.getIndex( 3 * t ); | |
int i2 = mesh.getIndex( 3 * t + 1 ); | |
int i3 = mesh.getIndex( 3 * t + 2 ); | |
//Get vertices of the triangle | |
const ofPoint &v1 = mesh.getVertex( i1 ); | |
const ofPoint &v2 = mesh.getVertex( i2 ); | |
const ofPoint &v3 = mesh.getVertex( i3 ); | |
//Compute the triangle's normal | |
ofPoint dir = ( (v2 - v1).crossed( v3 - v1 ) ).normalized(); | |
//Accumulate it to norm array for i1, i2, i3 | |
norm[ i1 ] += dir; | |
norm[ i2 ] += dir; | |
norm[ i3 ] += dir; | |
} | |
//Normalize the normal's length | |
for (int i=0; i<nV; i++) { | |
norm[i].normalize(); | |
} | |
//Set the normals to mesh | |
mesh.clearNormals(); | |
mesh.addNormals( norm ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment