1. ## 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. 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.

3. 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. The magic solution is to take each quad and break it into two triangles, and then use your existing code that works for triangles.

5. 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.