D3D Vertex Normals

Hello, I am working on my sciencefair project and I can’t seem to get vertex normal calculation working. I really need it to work for lighting purposes. Can somebody with C# / DirectX knowledge check over the code and maybe direct me where there is something wrong :slight_smile: It could be my lighting code but I highly doubt it…


public static Vector3[] FaceNormals(CustomVertex.PositionOnly[] vertices, int[] indexarray){
        Vector3[] Normals = new Vector3[indexarray.Length];
        for (int f = 0; f < indexarray.Length; f += 3)
        {
            //Get the points that make up a face
            int ai1 = f;
            int ai2 = f + 1;
            int ai3 = f + 2;
            if (ai1 >= indexarray.Length)
            {
                ai1 = ai1 - indexarray.Length;
            }
            if (ai2 >= indexarray.Length)
            {
                ai2 = ai2 - indexarray.Length;
            }
            if (ai3 >= indexarray.Length)
            {
                ai3 = ai3 - indexarray.Length;
            }
            Vector3 vertexA = vertices[indexarray[ai1]].Position;
            Vector3 vertexB = vertices[indexarray[ai2]].Position;
            Vector3 vertexC = vertices[indexarray[ai3]].Position;
            Vector3 v1 = vertexB - vertexA;
            Vector3 v2 = vertexC - vertexA;
            Vector3 FaceNormal = Vector3.Cross(v1, v2);
            FaceNormal.Normalize();
            Normals[ai1] = FaceNormal;
            Normals[ai2] = FaceNormal;
            Normals[ai3] = FaceNormal;
        }
        return Normals;
    }

public static CustomVertex.PositionNormalTextured[] PositionToTexturedNormal(CustomVertex.PositionOnly[] vertices, int[] indexarray)
    {
        CustomVertex.PositionNormalTextured[] output = new CustomVertex.PositionNormalTextured[vertices.Length];
        Vector3[] Normals = FaceNormals(vertices, indexarray);
        for (int v = 0; v<vertices.Length; v++){
            Vector3 VertexNormal = new Vector3(0, 0, 0);
            Vector3 NormalSum = new Vector3(0,0,0);
            for (int i = 0; i < indexarray.Length; i++)
            {
                if (indexarray* == v)
                {
                    NormalSum += Normals*;
                }
            }
            NormalSum.Normalize();
        
            output[v].Normal = NormalSum;
            output[v].Position = vertices[v].Position;
        }
        return output;
    }

Sorry if this is in the wrong forum :frowning: And this is what I was using for reference: http://www.devmaster.net/forums/showthread.php?t=1783

Am I missing a normalization? (if that is a word)