Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement flatNormals() for polyhedrons and low-poly meshes #5651

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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