Skip to content

Commit

Permalink
Implement flatNormals() for polyhedrons and low-poly meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
hamoid committed Jun 8, 2017
1 parent 2c7b719 commit 7b455bd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/openFrameworks/3d/ofMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ class ofMesh_{
virtual bool usingNormals() const;

void smoothNormals( float angle );

/// \brief Duplicates vertices and updates normals to get a low-poly look.
void flatNormals();

/// \}
/// \name Faces
Expand Down
45 changes: 45 additions & 0 deletions libs/openFrameworks/3d/ofMesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,51 @@ void ofMesh_<V,N,C,T>::smoothNormals( float angle ) {
}
}

//--------------------------------------------------------------
template<class V, class N, class C, class T>
void ofMesh_<V,N,C,T>::flatNormals() {
if( getMode() == OF_PRIMITIVE_TRIANGLES) {

// get copy original mesh data
auto numIndices = getIndices().size();
auto verts = getVertices();
auto texCoords = getTexCoords();
auto colors = getColors();

// remove all data to start from scratch
clear();

// add mesh data back, duplicating vertices and recalculating normals
N normal;
for(ofIndexType i = 0; i < numIndices; i++) {
ofIndexType indexCurr = getIndex(i);

if(i % 3 == 0) {
ofIndexType indexNext1 = getIndex(i + 1);
ofIndexType indexNext2 = getIndex(i + 2);
auto e1 = verts[indexCurr] - verts[indexNext1];
auto e2 = verts[indexNext2] - verts[indexNext1];
normal = glm::normalize(glm::cross(e1, e2));
}

addIndex(i);
addNormal(normal);

if(indexCurr < texCoords.size()) {
addTexCoord(texCoords[indexCurr]);
}

if(indexCurr < verts.size()) {
addVertex(verts[indexCurr]);
}

if(indexCurr < colors.size()) {
addColor(colors[indexCurr]);
}
}
}
}

// PLANE MESH //


Expand Down

0 comments on commit 7b455bd

Please sign in to comment.