POV-Ray : Newsgroups : povray.programming : MESH object fields Server Time
17 May 2024 07:21:26 EDT (-0400)
  MESH object fields (Message 1 to 2 of 2)  
From: David Curtis
Subject: MESH object fields
Date: 13 Jun 2005 09:52:07
Message: <42ad8f87@news.povray.org>
Hi. I am writing here as a last resort. I've been looking at the source for
days now and I haven't seen anything that clues me in to what I'm doing
wrong.

I'm converting a mesh object and am having problems with vertex orientation
and normals. I had hoped that it was as easy as doing this:

void Add_Mesh(MESH *pObj)
{
  VECTOR p1, p2, p3;
  VECTOR n1, n2, n3;
  MESH_TRIANGLE *mt = pObj->Data->Triangles;
  SNGL_VECT *v = pObj->Data->Vertices;
  SNGL_VECT *n = pObj->Data->Normals;
  int i, j;

  for (i = 0, j = pObj->Data->Number_Of_Triangles; i < j; i++) {
    Assign_Vector(p1, v[mt[i].P1], v[mt[i].P1], v[mt[i].P1]);
    Assign_Vector(p2, v[mt[i].P2], v[mt[i].P2], v[mt[i].P2]);
    Assign_Vector(p3, v[mt[i].P3], v[mt[i].P3], v[mt[i].P3]);
    Assign_Vector(n1, n[mt[i].N1], n[mt[i].N1], n[mt[i].N1]);
    Assign_Vector(n2, n[mt[i].N2], n[mt[i].N2], n[mt[i].N2]);
    Assign_Vector(n3, n[mt[i].N3], n[mt[i].N3], n[mt[i].N3]);

    ll->AddItem(p1, p2);
    ll->AddItem(p2, p3);
    ll->AddItem(p1, p3);
    f3l->AddItem(p1, n1, p2, n2, p3, n2);
  }
}

This code produces the correct points on the mesh but the normals seem to be 
wrong and the front side of the triangle get flipped when crossing the x = z 
plane.
The reason I'm asking this question here is i'm wondering what I can do with
the Mesh vector Perp and smooth. Can I use them to decide if I need to swap
two vertices or flip normals? I need to have the front side defined
counter-clockwise..

I'd really appreciate any help on this issue. BTW this is for a opengl patch 
i'm working on.
 Thanks, Dave.


Post a reply to this message

From: Warp
Subject: Re: MESH object fields
Date: 13 Jun 2005 11:21:36
Message: <42ada480@news.povray.org>
David Curtis <dcu### [at] mycybernetnet> wrote:
> The reason I'm asking this question here is i'm wondering what I can do with
> the Mesh vector Perp and smooth. Can I use them to decide if I need to swap
> two vertices or flip normals? I need to have the front side defined
> counter-clockwise..

  You can calculate consistent normal vectors for a whole mesh (if we
assume that there's no normal information or that the existing normal
information is inconsistent, ie. some normals point outwards and some
inwards) by two alternative methods. However, neither one is trivial
to implement.

  The first method works only for closed surfaces (it does not work
correctly for open meshes) but is more difficult to implement.
Shortly: For each triangle calculate one of its normals and, starting
from the surface of that triangle shoot a ray in the direction of that
normal and see how many other triangles it hits. If it hit an even
number of other triangles then that normal was correct, else you have
to invert it.
  A problem which might arise is that the ray hits exactly the edge
of a triangle pair. Getting it to count it correctly may need some
tweaking.

  The second method is easier to implement and doesn't suffer from
the problems of the first method, but this method requires that you
know the correct normal for one of the triangles (if you get it wrong
then all normals will be inverted).
  The idea is also quite simple: From the known triangle traverse to
the three triangles adjacent to it, skipping triangles which already
have been handled (a flag can be set to handled triangles) and then
calculate the correct normal vector for the adjacent triangle by
comparing the order of the two vertices shared with the previous triangle
(which we know is correct). Repeat this for each of the three triangles
recursively.
  It requires knowing which triangles are adjacent to each other (ie. you
need edge information in practice, which requires you to build an edge
data structure).

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.