I need to calcuate the normals of some triangles where I have a vector of vertices where each vertex has x, y, z coordinates. i1, i2, i3 are the indices in the vector of the three vertices of a triangle.
I'm using <DirectXMath.h>
and wrote this which seems to work.
XMFLOAT3 normal;
///
XMVECTOR v1 = XMLoadFloat3(&XMFLOAT3(verts[i1].x, verts[i1].y, verts[i1].z));
XMVECTOR v2 = XMLoadFloat3(&XMFLOAT3(verts[i2].x, verts[i2].y, verts[i2].z));
XMVECTOR v3 = XMLoadFloat3(&XMFLOAT3(verts[i3].x, verts[i3].y, verts[i3].z));
XMVECTOR n = XMVector3Cross(XMVectorSubtract(v2 ,v1), XMVectorSubtract(v3 ,v1));
XMStoreFloat3(&normal, n);
However it appears to have more Loads and Stores than actual calculations and was wondering if there was a better way to actually do this? Or are the load and stores "cheap" operations?
I have to run this for every triangle and it's taking a large amount of time relative to the rest of my code so speed improvements would be welcome.