cnr-isti-vclab/vcglib

Understanding what I'm doing wrong when generating meshes for display

jcelerier opened this issue · 1 comments

Hello, beginner here :)

I've tried to use the examples to generate some meshes but am failing to get the result I expect.

Here is my code to generate a cube with its normals:

// Create the box
vcg::Box3<float> box;
box.min = {-1, -1, -1};
box.max = {1, 1, 1};
vcg::tri::Box(mesh, box);

vcg::tri::Clean<TMesh>::RemoveUnreferencedVertex(mesh);
vcg::tri::Clean<TMesh>::RemoveZeroAreaFace(mesh);
vcg::tri::UpdateTopology<TMesh>::FaceFace(mesh);
vcg::tri::Clean<TMesh>::RemoveNonManifoldFace(mesh);
vcg::tri::UpdateNormal<TMesh>::PerVertexNormalized(mesh);

// Upload the box
float* pos_start = ...; // beginning of the array which will contain the positions
float* norm_start = ...; // beginning of the array which will contain the normals

  for (auto& fi : mesh.face)
  { // iterate each faces

    auto v0 = fi.V(0);
    auto v1 = fi.V(1);
    auto v2 = fi.V(2);

    auto p0 = v0->P();
    (*pos_start++) = p0.X() * scale;
    (*pos_start++) = p0.Y() * scale;
    (*pos_start++) = p0.Z() * scale;

    auto p1 = v1->P();
    (*pos_start++) = p1.X() * scale;
    (*pos_start++) = p1.Y() * scale;
    (*pos_start++) = p1.Z() * scale;

    auto p2 = v2->P();
    (*pos_start++) = p2.X() * scale;
    (*pos_start++) = p2.Y() * scale;
    (*pos_start++) = p2.Z() * scale;

    auto n0 = v0->N();
    (*norm_start++) = n0.X();
    (*norm_start++) = n0.Y();
    (*norm_start++) = n0.Z();

    auto n1 = v1->N();
    (*norm_start++) = n1.X();
    (*norm_start++) = n1.Y();
    (*norm_start++) = n1.Z();

    auto n2 = v2->N();
    (*norm_start++) = n2.X();
    (*norm_start++) = n2.Y();
    (*norm_start++) = n2.Z();
  } 

I set backface culling, counter-clockwise front face, GL_TRIANGLES yet I am failing to get an acceptable result when displaying.
Here are my vertex and fragment shaders: https://paste.ofcode.org/kvjs5dE3bQiUAdejYhqscW (I just used this method for spherical mapping: https://www.clicktorelease.com/blog/creating-spherical-environment-mapping-shader.html)

Yet here is how my shape looks: (tried a cube and a sphere) ; as you can see the faces aren't smooth maybe because of an issue with how I create the normals, but I am not sure how to debug this.

map.mp4

In particular what makes me suspect the normals is that if I look at them with renderdoc, they don't look very ... normal:

normals.mp4