Calculating the vertex normals of a quad

user1010005 picture user1010005 · Mar 21, 2012 · Viewed 8.1k times · Source

Lets say that i have the following array :

float QuadVertices[4 * 2];
float QuadNormals[4 * 2];

Which i fill this way :

//Fill vertices for a 2d quad
Renderer->FillVertices(QuadVertices,GL_QUADS,x,y,width,height);

Now at this point everything is ok i can render a quad , texture it , stretch it and all that.

But now i want to calculate the normals of the quad :

for (int i = 0; i < 8;i++)
{
    QuadNormals[i] = ??
}

BUT i can't figure out how on earth i am supposed to calculate the normals of a simple 2d vertice array that contains either 4vertices of GL_QUADS or 6vertices of GL_TRIANGLES....

Answer

jcoder picture jcoder · Mar 21, 2012

If you have this -

   v1        v2
    +---------+
    |         | 
    |         |
    +---------+
    v3        v4

Where v1..v4 are the vertices of your quad then to calculate the normal at v1 you should calculate the vectors along the two edges it is on, and then calculate the cross product of those vertices.

So, the normal at v1 is

CrossProduct((v2-v1), (v3-v1))

You can repeat this for each vertex, although they will all be the same if the quad is "flat"

If you have other quads connected to this one, you may want to calculate the normal for each quad, and then assign the average of all the connected quads to be the normal for that vertex