how to calculate the normals

Hi everyone I am making my first game in 3d.
Now I have my world rendering fine but now want to added lighting.

However to do that I need to calculate the normal of each vertex. Can somone point me in the right direction on how to do this as I don’t really know what I’m looking for.

Thanks Matt

Are you using a 3D model loaded from file or a mesh generated from code?

i am just generating vertexes in code, then sending them to the gpu memory for rendering the world, us drawuaerprimitives

if you have A,B,C triangle you get the edges ab & cb
var ab = A - B; var cb = C - B;
normalize,
ab.Normalize(); cb.Normalize();
and then get a vector perpendicular to those two edges
return Vector3.Cross(ab, cb);

Riemer’s tutorials are great for learning 3D graphics in XNA:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/ShortTuts/Normal_generation.php
The above link contains more expanded description of what nkast wrote.

When you’re done with your normals it’s very easy to get lighting (e.g. Lambertian diffuse model) and more complex methods used in PBR.

You can crib some code from the MonoGame content pipeline too…

Just wanted to say thank you to all of you for the help much appreciated. :slight_smile: