Thread: OBJ normalize vectors

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    OBJ normalize vectors

    Maybe it's more about opengl (or math) problem than programming.
    How to normalize vectors that referred to QUADS and not TRIANGLES ?

    In triangle method cross product is a solution, but in quads method should i use also cross product with more vectors ?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    With a triangle the (normalized) cross-product is guaranteed to give you a normal perpendicular to the surface. The trouble with quads is that you can have a quad that is non-planar (i.e. the four points don't lie on a plane). In this case however I'm not sure if the quad is guaranteed to be rendered correctly (although most OpenGL implementations tend to render a quad as two triangles, so you just get a quad that is folded in half).

    But if your quads are planar (which they probably should be . . .) then you can take the cross-product of any two non-parallel edge vectors, normalize it, and that will give you a normal for the surface. (For example, the vector upper-left-corner => upper-right-corner crossed with upper-left-corner => bottom-left-corner would give you a normal, but upper-left-corner => upper-right-corner crossed with bottom-left-corner => bottom-right-corner could give you a zero vector if the top and bottom edges are parallel.)

    (Of course if you cross the vectors in the wrong order you'll get a normal pointing in the wrong direction. You can try to figure it out with complicated right-hand rules and whatnot, but I'd suggest just trying A x B and then if that doesn't work, B x A. )

    All of this applies to flat shading, of course, where you're expected to supply normals to surfaces/faces. If you want to use smooth (Gouraud) shading then you need to get normals to vertices instead of faces, and that requires knowledge of more than just one polygon at once.

    Sometimes it's easier to find vertex normals, however. Suppose you're trying to render a sphere with lots of quads. The normal for each quad is a bit tricky to find, but the normal for each vertex is easy: it's just the vector from the centre of the sphere to the vertex, normalized.

    I should mention that mathematically normals are supposed to have length 1, which is what is means to "normalize" a vector. You have to ensure that the vectors you pass to OpenGL as normals have length 1, unless you're feeling lazy in which case you can just call
    Code:
    glEnable(GL_RESCALE_NORMAL);
    But really, normalizing a vector is quite simple so you might as well do it yourself.
    Code:
    void Vector::normalize() {
        double the_length = length();
        
        set_x(get_x() / the_length);
        set_y(get_y() / the_length);
        set_z(get_z() / the_length);
    }
    Well, that post was more rambling than it had to be. Hope you have fun with OpenGL, and do ask more questions if you have any.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    In reality i was hoping for the magic solution.
    Aaaagggrrrr, i didn't plan to spend time in rendering because i had already done a triangle implementation for other obj files.

    Thanks dwks !

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The magic solution is to take each quad and break it into two triangles, and then use your existing code that works for triangles.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    First you never work with quads when it comes to normals. Of course there is a difference between a normalized vector and a surface normal but I think you are talking about surface normals. The problem always breaks down into triangles. If you take a quad composed of vertices A, B, C and D you would create a normal for the ABC triangle by doing this:

    1. A - B = edge1
    2. A - C = edge2
    3. Take the cross product of edge1 and edge2 to arrive at the surface normal N.
    4. Normalize N.

    *Note that the way in which you compute your edges must be identical or the math will not work. So if you arrive at edge1 by doing A - B then you must arrive at edge2 by doing A - C and vice versa.

    If you do this for triangle ABC and BCD and then average the two normals you have the average surface normal of quad ABCD. But the process always starts with triangles b/c as dwks pointed out so clearly 3 points in space are guaranteed to be co-planar. All other geometry can be composed of a series of triangles. Even systems like Scaleform which start out with Bezier curve equations eventually must triangulate the curve so the hardware can use it. We do not currently have hardware that works with anything but triangles.

    As a side note, in all actuality the best way to represent objects is through voxels and most high dollar medical imaging equipment uses voxels. However PC hardware does not natively support voxels as of yet and I'm not sure if they ever will. Due to the limitations of triangle based graphics textures and other texturing techniques are used to create varying levels of detail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using/ Searching through Multiple Vectors
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 08-03-2009, 08:35 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  5. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM

Tags for this Thread