3

我有一个函数可以正确计算大多数基元的顶点法线。但它似乎无法处理多维数据集。

当我尝试使用漫反射光着色器时,它给了我这个结果(在右侧相同)

立方体漫反射光着色器

这是我计算法线的代码:

public virtual void GenerateNormals()
    {
        for (int i = 0; i < vertices.Length; i++)
            vertices[i].Normal = new Vector3(0, 0, 0);

        for (int i = 0; i < indices.Length / 3; i++)
        {
            Vector3 firstvec = vertices[indices[i * 3 + 1]].Position - vertices[indices[i * 3]].Position;
            Vector3 secondvec = vertices[indices[i * 3]].Position - vertices[indices[i * 3 + 2]].Position;
            Vector3 normal = Vector3.Cross(firstvec, secondvec);
            normal.Normalize();
            vertices[indices[i * 3]].Normal += normal;
            vertices[indices[i * 3 + 1]].Normal += normal;
            vertices[indices[i * 3 + 2]].Normal += normal;
        }

        for (int i = 0; i < vertices.Length; i++)
            if (vertices[i].Normal != Vector3.Zero) vertices[i].Normal.Normalize();
    }

立方体的顶点是这样生成的:

Vector3[] vectors = new Vector3[8];

        vectors[0] = new Vector3(-1, 1, -1);
        vectors[1] = new Vector3(1, 1, -1);
        vectors[2] = new Vector3(-1, 1, 1);
        vectors[3] = new Vector3(1, 1, 1);
        vectors[4] = new Vector3(-1, -1, -1);
        vectors[5] = new Vector3(1, -1, -1);
        vectors[6] = new Vector3(-1, -1, 1);
        vectors[7] = new Vector3(1, -1, 1);


        vertices = new VertexPositionNormalColored[24];

        //Top
        vertices[0].Position = vectors[0];
        vertices[0].Color = TopColor;
        vertices[1].Position = vectors[1];
        vertices[1].Color = TopColor;
        vertices[2].Position = vectors[2];
        vertices[2].Color = TopColor;
        vertices[3].Position = vectors[3];
        vertices[3].Color = TopColor;

        //Bottom
        vertices[4].Position = vectors[4];
        vertices[4].Color = BottomColor;
        vertices[5].Position = vectors[5];
        vertices[5].Color = BottomColor;
        vertices[6].Position = vectors[6];
        vertices[6].Color = BottomColor;
        vertices[7].Position = vectors[7];
        vertices[7].Color = BottomColor;

        //Left
        vertices[8].Position = vectors[2];
        vertices[8].Color = LeftColor;
        vertices[9].Position = vectors[0];
        vertices[9].Color = LeftColor;
        vertices[10].Position = vectors[6];
        vertices[10].Color = LeftColor;
        vertices[11].Position = vectors[4];
        vertices[11].Color = LeftColor;

        //Right
        vertices[12].Position = vectors[3];
        vertices[12].Color = RightColor;
        vertices[13].Position = vectors[1];
        vertices[13].Color = RightColor;
        vertices[14].Position = vectors[7];
        vertices[14].Color = RightColor;
        vertices[15].Position = vectors[5];
        vertices[15].Color = RightColor;

        //Back
        vertices[16].Position = vectors[0];
        vertices[16].Color = BackColor;
        vertices[17].Position = vectors[1];
        vertices[17].Color = BackColor;
        vertices[18].Position = vectors[4];
        vertices[18].Color = BackColor;
        vertices[19].Position = vectors[5];
        vertices[19].Color = BackColor;

        //Front
        vertices[20].Position = vectors[2];
        vertices[20].Color = FrontColor;
        vertices[21].Position = vectors[3];
        vertices[21].Color = FrontColor;
        vertices[22].Position = vectors[6];
        vertices[22].Color = FrontColor;
        vertices[23].Position = vectors[7];
        vertices[23].Color = FrontColor;

像这样的指数:

indices = new int[36];

        //Top
        indices[0] = 0;
        indices[1] = 1;
        indices[2] = 2;
        indices[3] = 2;
        indices[4] = 3;
        indices[5] = 1;

        //Back
        indices[6] = 16;
        indices[7] = 17;
        indices[8] = 18;
        indices[9] = 18;
        indices[10] = 19;
        indices[11] = 17;
        //Left
        indices[12] = 8;
        indices[13] = 9;
        indices[14] = 10;
        indices[15] = 10;
        indices[16] = 11;
        indices[17] = 9;
        //Front
        indices[18] = 20;
        indices[19] = 21;
        indices[20] = 22;
        indices[21] = 22;
        indices[22] = 23;
        indices[23] = 21;
        //Right
        indices[24] = 12;
        indices[25] = 13;
        indices[26] = 14;
        indices[27] = 14;
        indices[28] = 15;
        indices[29] = 13;
        //Bottom
        indices[30] = 4;
        indices[31] = 5;
        indices[32] = 6;
        indices[33] = 6;
        indices[34] = 7;
        indices[35] = 5;

我希望专家能帮助我找到解决方案。提前致谢!

4

2 回答 2

2

你得到这个结果是因为两个不同三角形中的顶点没有相同的顺序。

在右手系统中,您的第一个三角形{(-1,1,-1), (1,1,-1) , (-1,1,1)}按顺时针顺序排列,而第二个三角形按逆时针顺序排列{(-1,1,1), (1,1,1) , (1,1,-1)}

这些三角形的法线将有不同的方向。

于 2013-06-30T02:09:15.583 回答
0

嗯,看起来您正在计算(并求和)某些顶点的法线两次(因为您正在遍历索引数组,并且每个面都重用了一些顶点)。法线应该是相同的,规范化应该把它去掉。我怀疑计算出的法线在第一次时朝一个方向前进,然后在下一次时朝相反方向前进,导致法线为 (0,0,0)。

尝试跳过索引数组并直接遍历顶点数组,每个顶点只计算一次法线。

于 2013-06-29T23:44:57.310 回答