/// <summary> /// Move the managed data to GPU's memory /// </summary> /// <param name="buf"></param> public void Synchronize(IVertexBuffer buf) { if (dirty) { buf.LoadData(lines.ToArray()); dirty = false; } }
/// <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()); }