Results 1 to 7 of 7

Thread: normal vector. how to compute it?

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2002
    Posts
    15

    normal vector. how to compute it?

    hi,

    I have some difficulties to compute a normal vector between two vectors AB * AC. Is someone could help me please?

    thanks!!

  2. #2
    Member Contributor
    Join Date
    Feb 2002
    Location
    Wiesbaden, Germany
    Posts
    95

    Re: normal vector. how to compute it?

    hi,

    use the crossproduct!

    Code :
    void Vector3f::CrossProd(const Vector3f& vec,Vector3f& normal)
    {
    	normal.x = y*vec.z-z*vec.y;
    	normal.y = -x*vec.z+z*vec.x;
    	normal.z = x*vec.y-y*vec.x;
    }
    Snippet from my library.
    Happy coding,

    Martin

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2002
    Posts
    15

    Re: normal vector. how to compute it?

    here is my cross product function:

    void crossProduct(vertex3D a, vertex3D b, vertex3D c, vertex3D d)
    {
    d[0]=(b[1]-a[1])*(c[2]-a[2])-(c[1]-a[1])*(b[2]-a[2]);
    d[1]=(-(b[0]-a[0])*(c[2]-a[2])-(c[0]-a[0])*(b[2]-a[2]));
    d[2]=(b[0]-a[0])*(c[1]-a[1])-(c[0]-a[0])*(b[1]-a[1]);
    normal(d);
    }

    can you see a mistake?
    My normal vectors point into the wrong direction.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: normal vector. how to compute it?

    Hi, the cross product is like this ->
    Code :
    void vec3_t::CrossProduct(const vec3_t &v1,const vec3_t &v2)
    {
    	v[0] = ( v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1] );
    	v[1] = ( v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2] );
    	v[2] = ( v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0] );
    }
    Old GLman

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2001
    Location
    Gouda -> Netherlands
    Posts
    24

    Re: normal vector. how to compute it?

    Hi, I have this tick that I always exactly want to know what something does and why..

    i know what this does, but what is the mathamatical explaination (im just 15 years old so i dont have this on school yet) but can anyone post an URL on the mathamatical explaination of this crossproduct??

    thx alot

  6. #6
    Member Contributor
    Join Date
    Dec 2000
    Location
    Connecticut/New Jersey/Nevada
    Posts
    51

    Re: normal vector. how to compute it?

    The direction of the normal is dependent on the order of the three vertices to use. Switch a and c around and it will point in the correct direction.
    ~CGameProgrammer( );

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Dec 2007
    Posts
    1,077

    Re: normal vector. how to compute it?

    The dot product of two vectors is 0 if they are orthogonal to each other. You want a vector that is orthogonal to two vectors. Thus you have the equation system:

    v1 * normal = 0
    v2 * normal = 0

    v11 * normal1 + v12 * normal2 + v13 * normal3 = 0 (1)
    v21 * normal1 + v22 * normal2 + v23 * normal3 = 0 (2)

    Now set normal1 = 1, just to eliminate one unknown (this step will be done backwards at the end). You'll get:

    v11 + v12 * normal2 + v13 * normal3 = 0 (1)
    v21 + v22 * normal2 + v23 * normal3 = 0 (2)

    Solve (1) for normal2

    normal2 = (-v11 - v13 * normal3) / v12

    Insert into (2):

    v21 + v22 * (-v11 - v13*normal3)/v12 + v23 *normal3 = 0

    Solve for normal3:
    normal3 = (v11*v22-v12*v21)/(v12*v23-v13*v22)

    Solve (1) for normal3:
    normal3 = -(normal2*v12+v11)/v13

    Insert into (2):
    v21 + v22*normal2 + v23 * -(normal2*v12+v11)/v13 = 0

    Solve for normal2:
    normal2 = (v13*v21-v11*v23)/(v12*v23-v13*v22)

    Thus we have the vector:

    normal1 = 1
    normal2 = (v21*v13-v11*v23)/(v12*v23-v13*v22)
    normal3 = (v11*v12-v21*v12)/(v12*v23-v13*v12)

    Now we can scale the vector with (v12*v23-v13*v12), so we finally have:
    normal1 = (v12*v23-v13*v22)
    normal2 = (v13*v21-v11*v23)
    normal3 = (v11*v22-v12*v21)

    [This message has been edited by Michael Steinberg (edited 05-05-2002).]

Similar Threads

  1. Fastest solution to compute normal
    By a_dipino in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 05-03-2012, 08:38 AM
  2. Normal Vector
    By trin82 in forum OpenGL: General
    Replies: 6
    Last Post: 11-19-2010, 08:14 AM
  3. How to compute binormal( bitangent ) vector?
    By ehsan2004 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-10-2007, 06:11 AM
  4. RGB Normal Vector to HILO
    By Dakeyras in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 11-20-2002, 06:22 PM
  5. Replies: 3
    Last Post: 05-02-2000, 06:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean