- procedure T3DXMesh.CalcVertexNormals;
- var
- i, j,
- matches :integer;
- vnormal,
- tmpnormal :TSimple3DVector;
- length :single;
- begin
- CalcFaceNormals;
- SetLength(VertexNormals, FVertexCount);
- matches:=0;
- vnormal.x:=0;
- vnormal.y:=0;
- vnormal.z:=0;
- for i:=0 to High(Vertices) do //iterate through all vertices
- begin
- for j:=0 to High(Vertices) do //and compare them...
- begin
- if (Vertices[j].x = Vertices[i].x) and
- (Vertices[j].y = Vertices[i].z) and
- (Vertices[j].z = Vertices[i].z) then
- begin
- if Vertices[j].normal <> nil then tmpnormal:=Vertices[j].normal^
- else tmpnormal:=Vertices[Vertices[j].nindex].normal^;
- vnormal.x:=vnormal.x + tmpnormal.x;
- vnormal.y:=vnormal.y + tmpnormal.y;
- vnormal.z:=vnormal.z + tmpnormal.z;
- inc(matches);
- end;
- end;
- vnormal.x:=vnormal.x / matches;
- vnormal.y:=vnormal.y / matches;
- vnormal.z:=vnormal.z / matches;
- length:=sqrt((sqr(vnormal.x)+sqr(vnormal.y)+sqr(vnormal.z)));
- vnormal.x:=vnormal.x / length;
- vnormal.y:=vnormal.y / length;
- vnormal.z:=vnormal.z / length;
- VertexNormals[i]:=vnormal;
- Vertices[i].normal:=@VertexNormals[i];
- matches:=0;
- vnormal.x:=0;
- vnormal.y:=0;
- vnormal.z:=0;
- end;
- end;