Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ opengl: how to find normalized vectors for a quad?

can anyone assist me in finding the proper formula for quad normalization ?

using c++ with opengl.

thank you!

like image 401
ufk Avatar asked Dec 10 '22 16:12

ufk


2 Answers

Newell's method is usually your best choice for computing the normals of polygons which are nearly planar. It tends to be fairly robust to minor irregularities without being too expensive. See the Graphics Gems article. It's similar to that described above:

Vector3d normal(0,0,0) ;

for (int i=0; i<4; i++)
{
    int j = (i+1)%4;
    normal.x += (vertex[i].y - vertex[j].y)
               *(vertex[i].z + vertex[j].z);
    normal.y += (vertex[i].z - vertex[j].z)
               *(vertex[i].x + vertex[j].x);
    normal.z += (vertex[i].x - vertex[j].x)
               *(vertex[i].y + vertex[j].y);
}
normalize (normal) ;

It's probably not going to matter much for quads if they're reasonably well behaved, but I would certainly use it if you're dealing with more complex polygons.

like image 98
MPG Avatar answered Dec 15 '22 16:12

MPG


Assuming that you want the normal vector for a quad, this pseudo-code works

Vector3d vertex[4] = { ... }
Vector3d normal(0,0,0) ;

for (int i=0; i<4; i++)
{
    normal += cross (vertex[i], vertex[(i+1)%4]) ; // cross product
}
normalize (normal) ;
// normal is the unit normal to the quad

This gives you the formula n=A/|A|, where A = v0xv1 + v1xv2 + v2xv3 + v3xv0 and vi=vertex[i]). |A|/2 is also the area of the polygon. This can be generalized to arbitrary polygons, and will even give reasonable results for non-planar polygons, as long as they aren't too non-planar.

One reference is http://softsurfer.com/Archive/algorithm_0101/algorithm_0101.htm

If you know that the quad/polygon is planar, you only have to calculate the normal of the triangle formed by the first three vertices. This is A1/|A1|, where A1 = (v1-v0)x(v2-v0) = v0xv1 + v1xv2 + v2xv0.

If by "quad normalization" you meant something else, just ignore this answer.

EDIT: I found this related question: Get the Surface Area of a Polyhedron (3D object)

like image 23
brainjam Avatar answered Dec 15 '22 15:12

brainjam