openframeworks / openframeworks/openFrameworks

useful normals for ofMesh::plane

Open
#5,989 1 comment 1 reaction 0 assignees View on GitHub

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:

screen shot 2018-04-26 at 19 05 46

And here's with custom code for generating the mesh:

screen shot 2018-04-26 at 19 06 38

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:

screen shot 2018-04-26 at 18 47 58

If this is inherent to triangle strips, maybe there should be an option for building an OF_PRIMITIVE_TRIANGLES mesh instead.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.