我正在努力爲C++中的立方體計算8個頂點中的每個頂點的法線。 我想應當去這樣的: - 計算法線爲每個立方體 的6個面的 - 每個頂點接觸3個面,所以計算所有的3個面在C++中爲我的立方體計算8個頂點中的每個頂點的法線。
m_iNoVerts=8;
Vertex verts[8];
verts[0].position = XMFLOAT3(-1.0f, -1.0f, 1.0f);
verts[1].position = XMFLOAT3(-1.0f, 1.0f, 1.0f);
verts[2].position = XMFLOAT3(1.0f, -1.0f, 1.0f);
verts[3].position = XMFLOAT3(1.0f, 1.0f, 1.0f);
verts[4].position = XMFLOAT3(-1.0f, -1.0f, -1.0f);
verts[5].position = XMFLOAT3(-1.0f, 1.0f, -1.0f);
verts[6].position = XMFLOAT3(1.0f, -1.0f, -1.0f);
verts[7].position = XMFLOAT3(1.0f, 1.0f, -1.0f);
m_iNoIndices = 36;
int indices[36] = {0, 1, 2, // front face 0
1, 2, 3,
4, 5, 6, // back face 1
5, 6, 7,
4, 5, 0, // left face 2
5, 0, 1,
2, 3, 6, // right face 3
3, 6, 7,
1, 5, 3, // top face 4
5, 3, 7,
0, 4, 2, // bottom face 5
4, 2, 6};
// Calculate the normals for each face
XMVECTOR result[6]; // cross product of vec1 and vec2 represents the normal of the face
XMVECTOR vec1; // vec1 = B - A example first calc: B = verts[1] and A = verts[0]
XMVECTOR vec2; // vec2 = C - B
int ii = 0;
for(int i = 0; i < 6; ++i)
{
vec1.x = verts[indices[ii+1]].position.x - verts[indices[ii]].position.x;
vec1.y = verts[indices[ii+1]].position.y - verts[indices[ii]].position.y;
vec1.z = verts[indices[ii+1]].position.z - verts[indices[ii]].position.z;
vec2.x = verts[indices[ii+2]].position.x - verts[indices[ii+1]].position.x;
vec2.y = verts[indices[ii+2]].position.y - verts[indices[ii+1]].position.y;
vec2.z = verts[indices[ii+2]].position.z - verts[indices[ii+1]].position.z;
// calculate the cross product
//result[i].x = (vec1.y * vec2.z) - (vec1.z * vec2.y);
//result[i].y = (vec1.z * vec2.x) - (vec1.x * vec2.z);
//result[i].z = (vec1.x * vec2.y) - (vec1.y * vec2.x);
//result[i].w = 0;
result[i] = XMVector3Cross(vec1, vec2);
ii += 6; // increasing the counter for the indices to jump to the next face
}
// calculating the normals of each vertex
XMVECTOR normal[8];
// building the resulting vector of the 3 sites on each vertex
normal[0] = result[0] + result[2] + result[5];
normal[1] = result[0] + result[2] + result[4];
normal[2] = result[0] + result[3] + result[5];
normal[3] = result[0] + result[3] + result[4];
normal[4] = result[1] + result[2] + result[5];
normal[5] = result[1] + result[2] + result[4];
normal[6] = result[1] + result[3] + result[5];
normal[7] = result[1] + result[3] + result[4];
for(int i = 0; i < m_iNoVerts; ++i)
{
normal[i] = XMVector3Normalize(normal[i]); // normalization of the vector
verts[i].normal.x = normal[i].x;
verts[i].normal.y = normal[i].y;
verts[i].normal.z = normal[i].z;
}
時的歸一化矢量我檢查了法線在調試器,值是+ -0.577 .. 從我的老師給定的值是
0.0 0.5 0.5
0.0 0.5 0.5
0.0 -0.5 0.5
0.0 -0.5 0.5
0.0 0.5 -0.5
0.0 0.5 -0.5
0.0 -0.5 -0.5
0.0 -0.5 -0.5
請告訴我的事情我做錯了嗎? 謝謝!
很奇怪我的老師的法線沒有X分量。那麼左邊的頂點與右邊的頂點具有相同的法線?他們不應該以35度的角度彼此指向嗎? – Kevin