openframeworks / openframeworks/openFrameworks
useful normals for ofMesh::plane
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
I tried extruding an ofMesh::plane as returned by ofPlanePrimitive, then calculating the normals, but I think the winding alternates from triangle to triangle.
Here's what it looks like with ofPlanePrimitive:
And here's with custom code for generating the mesh:
Here are the two relevant excerpts from the code:
ofPlanePrimitive plane;
plane.set(w, h, w, h);
mesh = plane.getMesh();
mesh.clearNormals();
glm::vec3 center(w/2, h/2, 0);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
mesh.addVertex(glm::vec3(x, y, 0) - center);
}
}
for(int y = 0; y < h-1; y++) {
for(int x = 0; x < w-1; x++) {
int nw = y * w + x;
int ne = nw + 1;
int sw = nw + w;
int se = sw + 1;
mesh.addIndex(nw);
mesh.addIndex(ne);
mesh.addIndex(sw);
mesh.addIndex(sw);
mesh.addIndex(ne);
mesh.addIndex(se);
}
}
And normal calculation:
void addNormals(ofMesh & mesh){
auto& vertices = mesh.getVertices();
auto& normals = mesh.getNormals();
auto& indices = mesh.getIndices();
for(auto& v : vertices) {
mesh.addNormal({});
}
for(int i = 0; i < indices.size(); i+=3) {
int ia = indices[i];
int ib = indices[i+1];
int ic = indices[i+2];
auto e1 = vertices[ia] - vertices[ib];
auto e2 = vertices[ic] - vertices[ib];
auto no = glm::cross(e1, e2);
normals[ia] += no;
normals[ib] += no;
normals[ic] += no;
}
for(auto& normal : mesh.getNormals()) {
normal = glm::normalize(normal);
}
}
And here is a zoomed version of the ofPlanePrimitive one to show the triangulation:
If this is inherent to triangle strips, maybe there should be an option for building an OF_PRIMITIVE_TRIANGLES mesh instead.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting the ofPlanePrimitive mesh generation and ofMesh plane/index handling, then reproduce the reported normal calculation with the snippets in the issue. Compare triangle winding and resulting normals with the custom mesh; done means the cause is confirmed and the expected consistent-normal behavior or triangle-mesh option is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100