gpt4 book ai didi

c++ - 在 C++ 中计算我的立方体的 8 个顶点中的每一个的法线

转载 作者:行者123 更新时间:2023-11-30 04:11:24 25 4
gpt4 key购买 nike

我正在努力用 C++ 为我的立方体的 8 个顶点中的每一个计算法线。我认为它应该是这样的:- 计算立方体 6 个面中每个面的法线- 每个顶点接触 3 个面,因此计算所有 3 个面的归一化 vector

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

我做错了什么?谢谢!

最佳答案

立方体面的法线很简单

[+-1, 0, 0],   [0,+-1,0],    [0,0,+-1]
(Left/Right), (Top/Bottom), (Front/Back)

vertex 的 8 条法线,如果这样的概念有意义的话,更简单的是 vertex - centervertex it self in这种情况下,因为中心在原点。

这当然可以通过将面部法线的 8 个线性组合相加并归一化来计算,给出

[+-1, +-1, +-1] / sqrt(3)

但本质上,并没有那么多需要计算的...

关于c++ - 在 C++ 中计算我的立方体的 8 个顶点中的每一个的法线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20243522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com