views:

140

answers:

1

Hello All,

I have a request about surface normals. At the following code snippet is from the URL : http://www.kindohm.com/technical/wpf3dtutorial.htm

private Model3DGroup CreateTriangleModel(Point3D p0, Point3D p1, Point3D p2)
{
    MeshGeometry3D mesh = new MeshGeometry3D();

    mesh.Positions.Add(p0);
    mesh.Positions.Add(p1);
    mesh.Positions.Add(p2);
    mesh.TriangleIndices.Add(0);
    mesh.TriangleIndices.Add(1);
    mesh.TriangleIndices.Add(2);
    Vector3D normal = CalculateNormal(p0, p1, p2);
    //
    mesh.Normals.Add(normal);
    mesh.Normals.Add(normal);
    mesh.Normals.Add(normal);
    //
    Material material = new DiffuseMaterial(
        new SolidColorBrush(Colors.DarkKhaki));
    GeometryModel3D model = new GeometryModel3D(
        mesh, material);
    Model3DGroup group = new Model3DGroup();
    group.Children.Add(model);
    return group;

}

private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)

{
    Vector3D v0 = new Vector3D(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);

    Vector3D v1 = new Vector3D(p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);

    return Vector3D.CrossProduct(v0, v1);
}

I can't understand why the code line "mesh.Normals.Add(normal);" is repeated three times.

Could you say me the reason.

Regards

J.Frank

+1  A: 

It looks like you're creating a triangle out of three points. The normals are normals for each point in that triangle. It just so happens that in this case all the normals point in the same direction. That is, they are the same as the surface normal.

There are times when you'd want your point normals to be different from the surface normal. Like, when the triangle is a part of an object like a sphere. Then you'd want the point normals to be an average of the surface normals around it. That would allow you to do smooth shading and highlighting instead of just flat shading.

rich
Thank you Rich ! If we have 3D mesh object different than a cube (the above example is for a cube object), how can we calculate the surface normals ?
Wolfgang
The code is already calculating the surface normal. The surface normal is the cross product of two adjacent vectors of the triangle. like the vector of p0 -> p1 and p0 -> p2. If you're looking to calculate the vector normal, you have to first get all the surface normals. Then average (add and normalize) all the surface normals that share that vertex. Hope this is clear.This might help some of the math concepts. http://www.gamedev.net/reference/articles/article1443.asp
rich