need help with normals

Discussion in 'Java' started by Thorsten Kiefer, Jun 24, 2009.

  1. Hi,
    I wrote normal calculator for IndexTriangleFanStrip, but it calculates
    partially wrong normals, can anyone help me with that ?

    public static Vector3f[] computeNormals(Point3f[] coords, int[]
    coordIndices, int[] stripCounts) {
    Vector3f[] normals = new Vector3f[coords.length];
    for (int i = 0; i < normals.length; ++i) {
    normals = new Vector3f();
    }
    Vector3f[] coordVs = new Vector3f[coords.length];
    for (int i = 0; i < coordVs.length; ++i) {
    coordVs = new Vector3f(coords);
    }

    int stripStart = 0;
    for (int i = 0; i < stripCounts.length; ++i) {
    Vector3f x0 = coordVs[coordIndices[stripStart]];
    for (int j = 2; j < stripCounts; ++j) {
    Vector3f x1 = coordVs[coordIndices[stripStart + j]];
    Vector3f x2 = coordVs[coordIndices[stripStart + j - 1]];
    Vector3f d1 = new Vector3f();
    Vector3f d2 = new Vector3f();
    Vector3f cp = new Vector3f();
    d1.sub(x1, x0);
    d2.sub(x2, x0);
    cp.cross(d1, d2);
    normals[coordIndices[stripStart]].add(cp);
    normals[coordIndices[stripStart + j]].add(cp);
    normals[coordIndices[stripStart + j - 1]].add(cp);
    }
    stripStart += stripCounts;
    }

    for (int i = 0; i < normals.length; ++i) {
    normals.normalize();
    }

    return normals;
    }


    Best Wishes
    Thorsten
     
    Thorsten Kiefer, Jun 24, 2009
    #1
    1. Advertising

  2. Thorsten Kiefer

    Daniel Pitts Guest

    Thorsten Kiefer wrote:
    > Hi,
    > I wrote normal calculator for IndexTriangleFanStrip, but it calculates
    > partially wrong normals, can anyone help me with that ?
    >
    > public static Vector3f[] computeNormals(Point3f[] coords, int[]
    > coordIndices, int[] stripCounts) {
    > Vector3f[] normals = new Vector3f[coords.length];
    > for (int i = 0; i < normals.length; ++i) {
    > normals = new Vector3f();
    > }
    > Vector3f[] coordVs = new Vector3f[coords.length];
    > for (int i = 0; i < coordVs.length; ++i) {
    > coordVs = new Vector3f(coords);
    > }
    >
    > int stripStart = 0;
    > for (int i = 0; i < stripCounts.length; ++i) {
    > Vector3f x0 = coordVs[coordIndices[stripStart]];
    > for (int j = 2; j < stripCounts; ++j) {
    > Vector3f x1 = coordVs[coordIndices[stripStart + j]];
    > Vector3f x2 = coordVs[coordIndices[stripStart + j - 1]];
    > Vector3f d1 = new Vector3f();
    > Vector3f d2 = new Vector3f();
    > Vector3f cp = new Vector3f();
    > d1.sub(x1, x0);
    > d2.sub(x2, x0);
    > cp.cross(d1, d2);
    > normals[coordIndices[stripStart]].add(cp);
    > normals[coordIndices[stripStart + j]].add(cp);
    > normals[coordIndices[stripStart + j - 1]].add(cp);
    > }
    > stripStart += stripCounts;
    > }
    >
    > for (int i = 0; i < normals.length; ++i) {
    > normals.normalize();
    > }
    >
    > return normals;
    > }
    >
    >
    > Best Wishes
    > Thorsten
    >


    Please provide an SSCCE. Actually, it would be even better in this case
    to provide a Unit Test, complete with assertEqual statements that show
    what values you are expecting, but not getting.

    Either way, it must be stand-alone compilable and easily runnable (I
    consider JUnit tests to be easily runnable, as well as standard "main"
    methods)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 24, 2009
    #2
    1. Advertising

  3. In article <cXu0m.36322$>,
    Daniel Pitts <> wrote:

    > Thorsten Kiefer wrote:
    > > I wrote normal calculator for IndexTriangleFanStrip, but it calculates
    > > partially wrong normals, can anyone help me with that ?
    > >

    [...]
    >
    > Please provide an SSCCE. Actually, it would be even better in this
    > case to provide a Unit Test, complete with assertEqual statements
    > that show what values you are expecting, but not getting.
    >
    > Either way, it must be stand-alone compilable and easily runnable (I
    > consider JUnit tests to be easily runnable, as well as standard
    > "main" methods)


    I'd be interested, too. Here's an example using the JScience library:

    <http://jscience.org/api/index.html>

    <code>
    import junit.framework.Assert;
    import org.jscience.mathematics.vector.Float64Vector;
    import org.junit.Test;

    public class VectorCrossTest {

    private static final double EPSILON = 1e-15;

    @Test
    public void testMain() {
    System.out.println("testing...");
    Float64Vector v1 = Float64Vector.valueOf(1, 0, 0);
    Float64Vector v2 = Float64Vector.valueOf(0, 1, 0);
    check(v1.cross(v2), 0, 0, 1);
    check(v2.cross(v1), 0, 0, -1);
    check(v1.cross(v2).cross(v1), 0, 1, 0);
    }

    private void check (Float64Vector v, double... values) {
    System.out.println(v);
    for (int i = 0 ; i < v.getDimension(); i++ ) {
    Assert.assertEquals(v.getValue(i), values, EPSILON);
    }
    }
    }
    </code>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 25, 2009
    #3
  4. John B. Matthews wrote:

    > In article <cXu0m.36322$>,
    > Daniel Pitts <> wrote:
    >
    >> Thorsten Kiefer wrote:
    >> > I wrote normal calculator for IndexTriangleFanStrip, but it calculates
    >> > partially wrong normals, can anyone help me with that ?
    >> >

    > [...]
    >>
    >> Please provide an SSCCE. Actually, it would be even better in this
    >> case to provide a Unit Test, complete with assertEqual statements
    >> that show what values you are expecting, but not getting.
    >>
    >> Either way, it must be stand-alone compilable and easily runnable (I
    >> consider JUnit tests to be easily runnable, as well as standard
    >> "main" methods)

    >
    > I'd be interested, too. Here's an example using the JScience library:
    >
    > <http://jscience.org/api/index.html>
    >
    > <code>
    > import junit.framework.Assert;
    > import org.jscience.mathematics.vector.Float64Vector;
    > import org.junit.Test;
    >
    > public class VectorCrossTest {
    >
    > private static final double EPSILON = 1e-15;
    >
    > @Test
    > public void testMain() {
    > System.out.println("testing...");
    > Float64Vector v1 = Float64Vector.valueOf(1, 0, 0);
    > Float64Vector v2 = Float64Vector.valueOf(0, 1, 0);
    > check(v1.cross(v2), 0, 0, 1);
    > check(v2.cross(v1), 0, 0, -1);
    > check(v1.cross(v2).cross(v1), 0, 1, 0);
    > }
    >
    > private void check (Float64Vector v, double... values) {
    > System.out.println(v);
    > for (int i = 0 ; i < v.getDimension(); i++ ) {
    > Assert.assertEquals(v.getValue(i), values, EPSILON);
    > }
    > }
    > }
    > </code>
    >


    OK I think i found the problem, I'm confused.
    In an IndexedTriangleFanStrip are the normals supplied per Vertex
    or per Index ? e.g a cube has 8 Vertices, but 24 indices with 4 indices per
    Triangle Fan -> 6 Fans (6 polys).
    Now do I have to compute 8 Normals or 24 ???

    tk
     
    Thorsten Kiefer, Jun 25, 2009
    #4
  5. Thorsten Kiefer wrote:

    > John B. Matthews wrote:
    >
    >> In article <cXu0m.36322$>,
    >> Daniel Pitts <> wrote:
    >>
    >>> Thorsten Kiefer wrote:
    >>> > I wrote normal calculator for IndexTriangleFanStrip, but it calculates
    >>> > partially wrong normals, can anyone help me with that ?
    >>> >

    >> [...]
    >>>
    >>> Please provide an SSCCE. Actually, it would be even better in this
    >>> case to provide a Unit Test, complete with assertEqual statements
    >>> that show what values you are expecting, but not getting.
    >>>
    >>> Either way, it must be stand-alone compilable and easily runnable (I
    >>> consider JUnit tests to be easily runnable, as well as standard
    >>> "main" methods)

    >>
    >> I'd be interested, too. Here's an example using the JScience library:
    >>
    >> <http://jscience.org/api/index.html>
    >>
    >> <code>
    >> import junit.framework.Assert;
    >> import org.jscience.mathematics.vector.Float64Vector;
    >> import org.junit.Test;
    >>
    >> public class VectorCrossTest {
    >>
    >> private static final double EPSILON = 1e-15;
    >>
    >> @Test
    >> public void testMain() {
    >> System.out.println("testing...");
    >> Float64Vector v1 = Float64Vector.valueOf(1, 0, 0);
    >> Float64Vector v2 = Float64Vector.valueOf(0, 1, 0);
    >> check(v1.cross(v2), 0, 0, 1);
    >> check(v2.cross(v1), 0, 0, -1);
    >> check(v1.cross(v2).cross(v1), 0, 1, 0);
    >> }
    >>
    >> private void check (Float64Vector v, double... values) {
    >> System.out.println(v);
    >> for (int i = 0 ; i < v.getDimension(); i++ ) {
    >> Assert.assertEquals(v.getValue(i), values, EPSILON);
    >> }
    >> }
    >> }
    >> </code>
    >>

    >
    > OK I think i found the problem, I'm confused.
    > In an IndexedTriangleFanStrip are the normals supplied per Vertex
    > or per Index ? e.g a cube has 8 Vertices, but 24 indices with 4 indices
    > per Triangle Fan -> 6 Fans (6 polys).
    > Now do I have to compute 8 Normals or 24 ???
    >
    > tk


    Hi,
    One can have arbitrarily many normals. It's just important to
    connect the right normal to the right vertex index. In deed I use
    smooth normals, so I just compute as many normals as there are
    vertices. My first mistake was that I didn't normalize the
    cross product before adding, which lead to useless normal
    weighting (do you know what i mean ?). My second mistake
    was that I didn't connect the right vertex index to the right
    normal index. I fixed this, and now it works fine.
    Are you interested in a X3D loader for Java, which supports http ?
    I wrote a pretty primitive one (no textures supported).

    Regards
    Thorsten
     
    Thorsten Kiefer, Jun 25, 2009
    #5
  6. In article <4a438857$0$29137$>,
    Thorsten Kiefer <> wrote:

    [...]
    > One can have arbitrarily many normals. It's just important to connect
    > the right normal to the right vertex index. In deed I use smooth
    > normals, so I just compute as many normals as there are vertices. My
    > first mistake was that I didn't normalize the cross product before
    > adding, which lead to useless normal weighting (do you know what i
    > mean ?). My second mistake was that I didn't connect the right vertex
    > index to the right normal index. I fixed this, and now it works fine.
    > Are you interested in a X3D loader for Java, which supports http ? I
    > wrote a pretty primitive one (no textures supported).


    Sorry, I have no knowledge of IndexedTriangleFanStrip, but I was curious
    about rendering a mesh of trigons in 3D using Java. I'd be grateful for
    any links.

    The last tool I used for this had a convenient feature that calculated
    the normal vector on the fly. It assumed a consistent ordering of the
    vertices: left-handed v. right-handed. The trick was finding the few odd
    triangles that had gotten flipped at the source.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 25, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,505
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    787
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    429
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    598
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    533
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page