public static void Run() { // ExStart:GenerateDataForMeshes // Load a 3ds file, 3ds file doesn't have normal data, but it has smoothing group Scene s = new Scene(RunExamples.GetDataFilePath("camera.3ds")); // Visit all nodes and create normal data for all meshes s.RootNode.Accept(delegate(Node n) { Mesh mesh = n.GetEntity <Mesh>(); if (mesh != null) { VertexElementNormal normals = PolygonModifier.GenerateNormal(mesh); mesh.VertexElements.Add(normals); } return(true); }); // ExEnd:GenerateDataForMeshes Console.WriteLine("\nNormal data generated successfully for all meshes."); }
/// <summary> /// Build the normal data from the scene, /// the vertex buffer stores the coordinate of the normal /// lines in world coordinate system. /// </summary> /// <param name="scene"></param> private void BuildNormals(Scene scene) { vecs.Clear(); scene.RootNode.Accept(delegate(Node n) { var mc = n.Entity as IMeshConvertible; if (mc == null) { return(true); } SetNode(n); var mesh = mc.ToMesh(); var ven = (VertexElementNormal)mesh.GetElement(VertexElementType.Normal); if (ven == null) { ven = PolygonModifier.GenerateNormal(mesh); } var indirect = ven.ReferenceMode == ReferenceMode.IndexToDirect; //The normal data is mapped by polygon if (ven.MappingMode == MappingMode.Polygon) { for (int i = 0; i < ven.Data.Count; i++) { var polygon = mesh.Polygons[i]; var center = CalculateCenter(polygon, mesh.ControlPoints); var idx = indirect ? ven.Indices[i] : i; var norm = ven.Data[idx]; DrawNormal(center, norm); } } //The normal data is mapped by polygon vertex else if (ven.MappingMode == MappingMode.PolygonVertex) { int vtx = 0; for (int p = 0; p < mesh.PolygonCount; p++) { var polygon = mesh.Polygons[p]; for (int v = 0; v < polygon.Length; v++, vtx++) { var idx = indirect ? ven.Indices[vtx] : vtx; var norm = ven.Data[idx]; var center = mesh.ControlPoints[polygon[v]]; DrawNormal(center, norm); } } } //The normal data is mapped by control point else if (ven.MappingMode == MappingMode.ControlPoint) { int vtx = 0; for (int p = 0; p < mesh.ControlPoints.Count; p++) { var center = mesh.ControlPoints[p]; var idx = indirect ? ven.Indices[p] : p; var norm = ven.Data[idx]; DrawNormal(center, norm); } } else { Console.WriteLine("Unsupported mapping mode"); } return(true); }); normals = vecs.Count / 2; //load the data to buffer buffer.LoadData(vecs.ToArray()); }