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

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

Open
jcelerier opened this issue Aug 12, 2022 · 1 comment
Open

Comments

@jcelerier
Copy link

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
@jcelerier
Copy link
Author

jcelerier commented Aug 12, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant