Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the vertex normals of a quad

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

like image 977
user1010005 Avatar asked Mar 21 '12 14:03

user1010005


People also ask

How do you find the normal of a quad?

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.

How do you find the vertex 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.

How do you calculate normals?

The normal to the plane is given by the cross product n=(r−b)×(s−b).

What are the normals of a cube?

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.


1 Answers

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

like image 68
jcoder Avatar answered Sep 22 '22 20:09

jcoder