0

Can anyone please help.

I have a cube which I have made in 3DS Max. I don't know the dimensions of the cube. Is there a way to get the vertices of each of the triangles of the faces of the cube? I am trying to get the normal to one of the faces of the cube to determine which way its pointing. So if I can determine the vertices I can get the normal for the face if I have 3 vertices, V1, V2 and V3, ordered in counterclockwise order, I can obtain the direction of the normal by computing (V2 - V1) x (V3 - V1), where x is the cross product of the two vectors.

I have looked in my models .fbx file and I can see a number of values there: Vertices: *24 { a: -15,-12.5,0,15,-12.5,0,-15,12.5,0,15,12.5,0,-15,-12.5,0.5,15,-12.5,0.5,-15,12.5,0.5,15,12.5,0.5}

PolygonVertexIndex: *36 { a: 0,2,-4,3,1,-1,4,5,-8,7,6,-5,0,1,-6,5,4,-1,1,3,-8,7,5,-2,3,2,-7,6,7,-4,2,0,-5,4,6,-3}

Are these my models vertices? Also, I would assume that Vertices: * 24 would be my list of vertices, but why is there only 24? Should a cube not have 36 vertices? And finally, if the coordinates for my vertices are PolygonVertexIndex: * 36 these values just seem off to me when I imagine the cube in my head with those dimensions?

Or alternatively, is there a automatic way to get the vertices of a cube without having to manually enter all the values for each vertex? I might have a couple of models to

Any help would be greatly appreciated

4

2 回答 2

1

我不知道你为什么需要那个......因为当你加载一个模型时,它是计算出来的,在内部每个顶点都会有法线,......

总之很容易计算...

前三个索引定义了面部的第一个三角形,接下来的三个索引定义了面部的另一个三角形。

您只需要一个三角形来计算法线...

因此,通过三个索引访问 veretex 数组并获得三个点... A、B 和 C

现在您的法线是由该顶点形成的两个向量之间的叉积的结果。

 Vector3 Normal = Vector3.Cross(B-A, C-B);

如果正常后退或前进将取决于 A、B、C 顺序,可以是 CounterClockWise 或 ClockWise,但模型的每个三角形都将以一种方式排序。所以你会尝试并修复它

于 2012-04-24T12:50:32.477 回答
0

您可以编写一个 XNA 程序来轻松读取您的法线。

但是,如果您仍想计算它们,请使用取自 FFWD 的此 C# 代码作为指南。检查 URL 以获取有关利弊的更详细讨论。就个人而言,我对结果不太满意,但目前它确实有效。当然,由于这段代码与 FFWD 相关(Unity 的 XNA API 的实现),它与 XNA 不完全匹配,但数学保持不变。

    /// <summary>
    /// Recalculates the normals.
    /// Implementation adapted from http://devmaster.net/forums/topic/1065-calculating-normals-of-a-mesh/
    /// </summary>
    public void RecalculateNormals()
    {
        Vector3[] newNormals = new Vector3[_vertices.Length];

        // _triangles is a list of vertex indices,
        // with each triplet referencing the three vertices of the corresponding triangle
        for (int i = 0; i < _triangles.Length; i = i + 3)
        {
            Vector3[] v = new Vector3[]
            {
                _vertices[_triangles[i]],
                _vertices[_triangles[i + 1]],
                _vertices[_triangles[i + 2]]
            };

            Vector3 normal = Vector3.Cross(v[1] - v[0], v[2] - v[0]);

            for (int j = 0; j < 3; ++j)
            {
                Vector3 a = v[(j+1) % 3] - v[j];
                Vector3 b = v[(j+2) % 3] - v[j];
                float weight = (float)Math.Acos(Vector3.Dot(a, b) / (a.magnitude * b.magnitude));
                newNormals[_triangles[i + j]] += weight * normal;
            }
        }

        foreach (Vector3 normal in newNormals)
        {
            normal.Normalize();
        }

        normals = newNormals;
    }
于 2012-04-25T09:04:14.187 回答