Recently, I learned about face normals, and i'm a bit confused. I need to use them for a directional light, but I've been having trouble calculating them. I would appreciate if someone types the code out for calculating them and explains it.
This is my code for generating a pyramid:
GeneratePyramid(float baseWidth, float baseDepth, float apexHeight, MeshData& meshData) {
float halfWidth = baseWidth * 0.5f;
float halfDepth = baseDepth * 0.5f;
Vertex v[5];
/*
v[2]
/\\
/ \\
v[4] / \\ v[3]
v[0] /______\/ v[1]
*/
v[0] = Vertex(-halfWidth, 0, -halfDepth);
v[1] = Vertex(halfWidth, 0, -halfDepth);
v[2] = Vertex(0, apexHeight, 0); // apex
v[3] = Vertex(halfWidth, 0, halfDepth);
v[4] = Vertex(-halfWidth, 0, halfDepth);
meshData.vertices.assign(&v[0], &v[5]);
unsigned int i[18] = {
0, 2, 1,
1, 2, 3,
3, 2, 4,
4, 2, 0,
0, 1, 4,
1, 3, 4
};
meshData.indices.assign(&i[0], &i[18]);
}
the struct MeshData is simply defined as:
struct MeshData {
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
};
and the Vertex struct is defined as:
struct Vertex {
Vertex() {};
Vertex(float x, float y, float z);
XMFLOAT3 pos;
};
I would like GeneratePyramid() to also calculate the normals. Should I add an "XMFLOAT3 normal" member to the Vertex struct? And if so, how would I use that to calculate them in GeneratePyramid()?
Thanks in advance!
Extra:
I read that you don't need vertex normals for pyramids since it has sharp edges, but I would also like to know how you would calculate them.