<p>The Guy's answer is correct but incomplete, that's how you create <em>SURFACE</em> normals not <em>VERTEX</em> normals. to Create vertex normals see this post see the cyfex's answer. should be the second one.
<a href="https://gamedev.stackexchange.com/questions/133864/calculating-vertex-normals-in-opengl-c?newreg=310c1117667f4ec99cd28866d3a49149">https://gamedev.stackexchange.com/questions/133864/calculating-vertex-normals-in-opengl-c?newreg=310c1117667f4ec99cd28866d3a49149</a></p>

CC BY-SA 4.0
2022-11-05T21:17:37.117Z
the second half of the answer does explain smooth normals
pmw1234
CC BY-SA 4.0
2022-11-05T16:50:55.053Z
2022-11-05T16:52:16.383Z
2022-11-05T16:52:16.383Z
Shahrooz
<p>Computing the normal from vertex positions is quite simple using the vector cross product.</p>

<p>The <a href="https://en.wikipedia.org/wiki/Cross_product" rel="noreferrer">cross product</a> of two vectors $u$ and $v$ (noted $u \times v$, or sometimes $u \wedge v$) is a vector perpendicular to $u$ and $v$, of length $||u \times v|| = ||u|| \cdot ||v|| sin(\theta)$, with $\theta$ the angle between $u$ and $v$. The direction of the vector will depend on the order of the multiplication: $u \times v$ is the opposite of $v \times u$ (the two directions perpendicular to the plane).</p>

<p>If you are not familiar with the cross product, I invite you to read about it and get comfortable with it. Normals will then seem simple.</p>

<h1>Flat shading normals</h1>

<p>If you have a triangle $ABC$, $AB \times AC$ is a vector perpendicular to the triangle and with a length proportional to its area. Since the normal is the unit vector perpendicular to the triangle's plane, you can get the normal with:</p>

<p>$N = \dfrac{AB \times AC}{||AB \times AC||}$</p>

<p>In code, this would look like <code>n = normalize(cross(b-a, c-a))</code> for example. Just apply this over all your faces and you will have your normals per face.</p>

<pre><code>For each triangle ABC
    n := normalize(cross(B-A, C-A))
    A.n := n
    B.n := n
    C.n := n
</code></pre>

<p>Note that this assumes vertices are not shared between triangles. I am not familiar with the Kinect API; it's quite possible that they are shared, in which case you would have to duplicate them, or move on to the next solution:</p>

<h1>Smooth shading normals</h1>

<p>After lighting with normals computed as above, you will notice that triangle edges are apparent. If this in not desirable, you can compute smooth normals instead, by taking into account all the faces that share a same vertex.</p>

<p>The idea is that if a same vertex is shared by three triangles $T1$, $T2$ and $T3$ for example, the normal $N$ will be the average of $N1$, $N2$ and $N3$. Moreover, if $T1$ is a big triangle and $T2$ is a tiny one, you probably want $N$ to be more influenced by $N1$ than by $N2$.</p>

<p>Remember how the cross product is proportional to the area? If you add up the cross products then normalize the sum, it will do exactly the weighted sum we want. So the algorithm becomes:</p>

<pre><code>For each vertex
    vertex.n := (0, 0, 0)

For each triangle ABC
    // compute the cross product and add it to each vertex
    p := cross(B-A, C-A)
    A.n += p
    B.n += p
    C.n += p

For each vertex
    vertex.n := normalize(vertex.n)
</code></pre>

<p>This technique is explained in longer detail this article by Iñigo Quilez: <a href="http://www.iquilezles.org/www/articles/normals/normals.htm" rel="noreferrer">clever normalization of a mesh</a>.</p>

<hr>

<p>For more on normals, see also:</p>

<ul>
<li><a href="https://computergraphics.stackexchange.com/questions/1562/how-to-calculate-surface-normals-for-generated-geomtery/1576#1576">How to Calculate Surface Normals for Generated Geomtery</a></li>
</ul>

CC BY-SA 3.0
2016-09-24T11:36:58.767Z
I'll give this a go asap and return with the results
Allahjane
CC BY-SA 3.0
2016-09-25T12:20:40.937Z
It worked Thanks a tonn man, it worked exactly I wanted it to
Allahjane
CC BY-SA 3.0
2016-09-25T12:23:22.877Z
see the result here BEFORE https://i.gyazo.com/a314d1f7a0509788c272f83279c5077b.png   and AFTER https://i.gyazo.com/b879db7294d82a72b86cc4eaf0132a13.png
Allahjane
CC BY-SA 3.0
2016-09-25T16:07:43.850Z
@Allahjane: glad to hear it worked fine. :)
Julien Guertault
CC BY-SA 3.0
2016-09-23T17:59:18.003Z
2016-09-23T18:17:51.310Z
2017-04-13T13:00:43.660Z
Julien Guertault
<p>I'm working with Kinect face api, it provides an array of vertices and indices for triangles which is to be rendered to make the face image.</p>

<p>The no of vertices and their order in array as well as the indices given by kinect is always constant.</p>

<p>However the api gives no information on UV data and vertex normals.</p>

<p>The application requires me to keep the vertex order as given out by the kinect as their positions in 3d space change as per facial movement so generating uv and normals in a 3d editing software is out of question.</p>

<p>I managed to generate UV by projecting the vertex positions onto a 2D plane  since there were very few vertices on same plane.</p>

<p>However, I don't know how to generate vertex normals for the mesh, without vertex normal the face mesh draws with no depth of its features from prespective, although silhouette is visible as vertex positions are correct.</p>

<p>I understand that due to absence of vertex normals the lighting wont work correctly on it and hence the pale featureless mesh it looks right now.</p>

<p>so how do I generate vertex normals when all I have is just vertex position and the index of vertices to make triangles out of it?</p>

CC BY-SA 3.0
2016-10-05T23:17:56.073Z
n = (v1 - v0) x (v2 - v0), where v0, v1, and v2 are the vertices of the (triangle) face in question. The order is importan.  Normalize it if you really need to. (and `x` is cross product)
3Dave
<mesh><shading><normal-mapping>
Programmatically generating vertex normals
Allahjane
# Programmatically generating vertex normals

(Asked by: username_0 on 2016-09-23)

I'm working with Kinect face api, it provides an array of vertices and indices for triangles which is to be rendered to make the face image.

The no of vertices and their order in array as well as the indices given by kinect is always constant.

However the api gives no information on UV data and vertex normals.

The application requires me to keep the vertex order as given out by the kinect as their positions in 3d space change as per facial movement so generating uv and normals in a 3d editing software is out of question.

I managed to generate UV by projecting the vertex positions onto a 2D plane since there were very few vertices on same plane.

However, I don't know how to generate vertex normals for the mesh, without vertex normal the face mesh draws with no depth of its features from prespective, although silhouette is visible as vertex positions are correct.

I understand that due to absence of vertex normals the lighting wont work correctly on it and hence the pale featureless mesh it looks right now.

so how do I generate vertex normals when all I have is just vertex position and the index of vertices to make triangles out of it?

**Tags:** mesh, shading, normal-mapping

> **Comments:**
> **username_1**: n = (v1 - v0) x (v2 - v0), where v0, v1, and v2 are the vertices of the (triangle) face in question. The order is importan.  Normalize it if you really need to. (and `x` is cross product)  

---

## 2 answers

### Answer by username_4 on 2016-09-23. Score: 20 | Accepted answer

Computing the normal from vertex positions is quite simple using the vector cross product.

The [cross product](https://en.wikipedia.org/wiki/Cross_product) of two vectors $u$ and $v$ (noted $u \times v$, or sometimes $u \wedge v$) is a vector perpendicular to $u$ and $v$, of length $||u \times v|| = ||u|| \cdot ||v|| sin(\theta)$, with $\theta$ the angle between $u$ and $v$. The direction of the vector will depend on the order of the multiplication: $u \times v$ is the opposite of $v \times u$ (the two directions perpendicular to the plane).

If you are not familiar with the cross product, I invite you to read about it and get comfortable with it. Normals will then seem simple.

# Flat shading normals

If you have a triangle $ABC$, $AB \times AC$ is a vector perpendicular to the triangle and with a length proportional to its area. Since the normal is the unit vector perpendicular to the triangle's plane, you can get the normal with:

$N = \dfrac{AB \times AC}{||AB \times AC||}$

In code, this would look like `n = normalize(cross(b-a, c-a))` for example. Just apply this over all your faces and you will have your normals per face.

```
For each triangle ABC
    n := normalize(cross(B-A, C-A))
    A.n := n
    B.n := n
    C.n := n

```

Note that this assumes vertices are not shared between triangles. I am not familiar with the Kinect API; it's quite possible that they are shared, in which case you would have to duplicate them, or move on to the next solution:

# Smooth shading normals

After lighting with normals computed as above, you will notice that triangle edges are apparent. If this in not desirable, you can compute smooth normals instead, by taking into account all the faces that share a same vertex.

The idea is that if a same vertex is shared by three triangles $T1$, $T2$ and $T3$ for example, the normal $N$ will be the average of $N1$, $N2$ and $N3$. Moreover, if $T1$ is a big triangle and $T2$ is a tiny one, you probably want $N$ to be more influenced by $N1$ than by $N2$.

Remember how the cross product is proportional to the area? If you add up the cross products then normalize the sum, it will do exactly the weighted sum we want. So the algorithm becomes:

```
For each vertex
    vertex.n := (0, 0, 0)

For each triangle ABC
    // compute the cross product and add it to each vertex
    p := cross(B-A, C-A)
    A.n += p
    B.n += p
    C.n += p

For each vertex
    vertex.n := normalize(vertex.n)

```

This technique is explained in longer detail this article by Iñigo Quilez: [clever normalization of a mesh](http://www.iquilezles.org/www/articles/normals/normals.htm).

---

For more on normals, see also:

* [How to Calculate Surface Normals for Generated Geomtery](https://computergraphics.stackexchange.com/questions/1562/how-to-calculate-surface-normals-for-generated-geomtery/1576#1576)

> **Comments:**
> **username_0**: I'll give this a go asap and return with the results  
> **username_0**: It worked Thanks a tonn man, it worked exactly I wanted it to  
> **username_0**: see the result here BEFORE https://i.gyazo.com/a314d1f7a0509788c272f83279c5077b.png   and AFTER https://i.gyazo.com/b879db7294d82a72b86cc4eaf0132a13.png  
> **username_4**: @username_0: glad to hear it worked fine. :)  

---

### Answer by username_2 on 2022-11-05. Score: 1

The Guy's answer is correct but incomplete, that's how you create *SURFACE* normals not *VERTEX* normals. to Create vertex normals see this post see the cyfex's answer. should be the second one.
<https://gamedev.stackexchange.com/questions/133864/calculating-vertex-normals-in-opengl-c?newreg=310c1117667f4ec99cd28866d3a49149>

> **Comments:**
> **username_3**: the second half of the answer does explain smooth normals  

---

