Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: normal-calculation

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    European Union
    Posts
    7

    normal-calculation

    I am having trouble with lightening calculation, I have traced the problems to the normal calculation but I can't find any errors with it.

    I have polygons defined as either triangles or quads, their vertices are placed in three vectors called p0, p1, p2 and p3.

    I have checked to see that the polygons are defined in CCW order and they are.

    Any help would be much appreciated.

    Here are the lines concerning the normal calculation:

    // compute normals, poly vertices defined in CCW order
    // n = (p2-p1) x (p0-p1)
    a[0] = p2[0] - p1[0];
    a[1] = p2[1] - p1[1];
    a[2] = p2[2] - p1[2];
    b[0] = p0[0] - p1[0];
    b[1] = p0[1] - p1[1];
    b[2] = p0[2] - p1[2];

    n[0] = a[1]*b[2] - a[2]*b[1];
    n[1] = a[2]*b[0] - a[0]*b[2];
    n[2] = a[0]*b[1] - a[1]*b[0];

    // normalize
    // n = (nx / sqrt(|n|), ny / sqrt(|n|), nz / sqrt(|n|))
    nabs = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
    n[0] /= nl; n[1] /= nl; n[2] /= nl;


    // Mattias

  2. #2
    Junior Member Regular Contributor
    Join Date
    Oct 2002
    Location
    San Diego, CA, USA
    Posts
    209

    Re: normal-calculation

    // normalize
    // n = (nx / sqrt(|n|), ny / sqrt(|n|), nz / sqrt(|n|))


    nabs = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);


    n[0] /= nl; n[1] /= nl; n[2] /= nl;


    // where is nl coming from?
    // shouldnt it be n[0] /= nabs; ... ??

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    European Union
    Posts
    7

    Re: normal-calculation

    Originally posted by 147-2:

    // normalize
    // n = (nx / sqrt(|n|), ny / sqrt(|n|), nz / sqrt(|n|))


    nabs = sqrt(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);


    n[0] /= nl; n[1] /= nl; n[2] /= nl;


    // where is nl coming from?
    // shouldnt it be n[0] /= nabs; ... ??

    Ah, sorry... mixed versions of the code, nabs was named nl before, but it is named nabs in all places of the real code, so this isn't the problem, I've tried with glEnable(GL_NORMALIZE); on, and the normals are still off.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    5

    Re: normal-calculation

    Hmmm, maybe it helps if you specify the problem more precisely. Is the tria/poly not lit? is the reflection not correct?

    My first guess would be, the vectors are not correctly defined. I think it should be anti clockwise. Use the right hand rule to check. But you said that you checked that already...

    No clue so far
    Regards
    xDigital

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    European Union
    Posts
    7

    Re: normal-calculation

    Originally posted by xDigital:
    Hmmm, maybe it helps if you specify the problem more precisely. Is the tria/poly not lit? is the reflection not correct?

    My first guess would be, the vectors are not correctly defined. I think it should be anti clockwise. Use the right hand rule to check. But you said that you checked that already...

    No clue so far
    Regards
    xDigital
    The reflection is not correct, drawing the normals yields that they are 90 degrees of, in some axis. The vertices are defined in CCW order, as said, I've verified this by drawing the back sides in wire frame.

    The normal is in the same plane as the polygon, if the polygon is defined in a plane parallel to the x-y, y-z or x-y planes.

    I can probably get a screenshot and post a link to it here if the description was fuzzy.

    [This message has been edited by kinslayer (edited 02-24-2004).]

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    5

    Re: normal-calculation

    k, try this calculation:

    typedef struct{
    float x,y,z;
    } VECTOR;

    VECTOR Crossproduct(VECTOR v0,VECTOR v1){
    VECTOR r;
    r.x = v0.y*v1.z-v0.z*v1.y;
    r.y = v0.z*v1.x-v0.x*v1.z;
    r.z = v0.x*v1.y-v0.y*v1.x;
    return r;
    }

    plus your normalization.

    Regards
    xDigital

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2004
    Location
    European Union
    Posts
    7

    Re: normal-calculation

    Very much thanks, although it did not help. The cross product in my original version also does very well in the unit-test I wrote.

    So, my cross product is right on, including the nomalization, but I still get this mysterious bug.

    I guess I'm off to more debugging.

    Originally posted by xDigital:
    k, try this calculation:

    typedef struct{
    float x,y,z;
    } VECTOR;

    VECTOR Crossproduct(VECTOR v0,VECTOR v1){
    VECTOR r;
    r.x = v0.y*v1.z-v0.z*v1.y;
    r.y = v0.z*v1.x-v0.x*v1.z;
    r.z = v0.x*v1.y-v0.y*v1.x;
    return r;
    }

    plus your normalization.

    Regards
    xDigital

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •