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....
Use 2 of the points on the quad as 3D vectors. Get the cross product of these 2 vectors (which is a vector perpendicular to the two input vectors). This perpendicular vector is the surface normal.
The algorithm to compute such vertex normals is as follows: First, allocate an array of normals, one for each vertex in the mesh, and initialize them to zero (Point3( 0,0,0)). Then for each face, compute its face normal, and add it into each of the three vertex normals that the face contributes to.
The normal to the plane is given by the cross product n=(r−b)×(s−b).
Normals are specified per-vertex, and since the normals for the three faces that share each vertex are orthogonal, you'll get some really wonky looking results by specifying a cube with just 8 vertices and averaging the three face normals to get the vertex normal. It'll be shaded as a sphere, but shaped like a cube.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With