# Having some trouble implementing D3DXComputeNormals... any hints/advice?

Henri Verbeet hverbeet at gmail.com
Mon Jul 12 07:21:00 CDT 2010

```On 12 July 2010 04:48, Misha Koshelev <misha680 at gmail.com> wrote:
> Index Buffer:
>
>        11,21,12,
>                {0,1,-0.5},
>                {0,1,-0.45},
>                {0.587785,0.809017,-0.5},
>                        {0.00954915,0.0293893,0},
>        20,30,11,
>                {-0.587785,0.809017,-0.5},
>                {-0.587785,0.809017,-0.45},
>                {0,1,-0.5},
>                        {-0.00954915,0.0293893,0},
>        11,30,21,
>                {0,1,-0.5},
>                {-0.587785,0.809017,-0.45},
>                {0,1,-0.45},
>                        {-0.00954915,0.0293893,0},
>
> Averaged Normals:
>
>        11      {-0.107677,0.994186,0}  {-1.99491e-008,1,0},
>

The part of the mesh you're interested in looks like this:

20---11---12
|   /|   /|
|B / |A / |
| / C| /  |
|/   |/   |
30---21---22

The "real" vertex normal at a given vertex would be {x, y, 0} for this
shape (for an open cylinder anyway, for a closed cylinder you'd have
{0, 0, 1.0} and {0, 0, -1.0} for the vertices from the caps), so the
{-1.99491e-008, 1.0, 0.0} d3dx calculates is pretty close.

Vertex 11 is part of the faces A, B and C. Notice that faces B and C
(must) have the same face normal. If you simply calculate the average
(mean) of the normals for faces A, B and C, the resulting vector would
be biased towards B/C. That's what happens with your code. You can
mitigate that by assigning weights to the faces based on the area of
the triangles and the angle between the edges at the vertex you're
interested in. I'd expect d3dx to do something similar to that.

However, note that for simple, regular shapes like these it's *much*
simpler to just generate the normals together with the vertices. E.g.
for a cylinder, you'd simply have normalize(x, y, 0.0), {0.0, 0.0,
1.0} and {0.0, 0.0, -1.0}, for a sphere you'd have normalize{x, y, z},
etc.

```