/// <summary> /// Return an array of DisplayMesh objects created from the parameters /// </summary> public static Object[] CreateWithMesh(Mesh mesh, ObjectAttributes attr, Material material, int materialIndex) { // If our render material is the default material, modify our material to match the Rhino default material if (materialIndex == -1) { material.DiffuseColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.White); } // create vertex normals if missing if (mesh.Normals.Count == 0) { mesh.Normals.ComputeNormals(); } bool didCreatePartitions = false; try { // The minus 3 here is the Paranoid (TM) Constant... // Since we don't know how well CreatePartition deals with edge cases, // we give the routine a slightly smaller parameter than the absolutely largest possible didCreatePartitions = mesh.CreatePartitions(ushort.MaxValue - 3, int.MaxValue - 3); } catch (Exception ex) { Rhino.Runtime.HostUtils.ExceptionReport(ex); } if (!didCreatePartitions) { System.Diagnostics.Debug.WriteLine("Unable to create partitions on mesh {0}", attr.ObjectId.ToString()); //Rhino.Runtime.HostUtils.ExceptionReport (new Exception ("Unable to create partitions on mesh")); return(null); //invalid mesh, ignore } List <DisplayMesh> displayMeshes = new List <DisplayMesh> (); displayMeshes.Capacity = mesh.PartitionCount; //System.Diagnostics.Debug.WriteLine("Mesh {0} VertexCount: {1}, FaceCount: {2}, Partition has {3} parts.", attr.ObjectId.ToString(), mesh.Vertices.Count, mesh.Faces.Count, mesh.PartitionCount); for (int i = 0; i < mesh.PartitionCount; i++) { var displayMaterial = new DisplayMaterial(material, materialIndex); var newMesh = new DisplayMesh(mesh, i, displayMaterial, mesh.PartitionCount > 1, attr.ObjectId); if (newMesh != null) { newMesh.GUID = attr.ObjectId; newMesh.IsVisible = attr.Visible; newMesh.LayerIndex = attr.LayerIndex; displayMeshes.Add(newMesh); } } return(displayMeshes.ToArray()); }
/// <summary> /// Creates an instance of a DisplayMesh with a new transform. /// </summary> public DisplayInstanceMesh(DisplayMesh mesh, Transform xform) { Mesh = mesh; IsVisible = mesh.IsVisible; LayerIndex = mesh.LayerIndex; m_xform = xform; Transform zeroXForm = CreateZeroTransform(); if (m_xform.Equals(zeroXForm)) { m_xform = Transform.Identity; } }
/// <summary> /// Renders the object in a viewport /// </summary> private void RenderObject(DisplayObject obj, Rhino.DocObjects.ViewportInfo viewport, bool isInstance) { // If the object is invisible, return. if (!obj.IsVisible) { return; } // If the layer that the object is on is turned off, return. if (!Model.LayerIsVisibleAtIndex(obj.LayerIndex)) { return; } DisplayMesh displayMesh = isInstance ? ((DisplayInstanceMesh)obj).Mesh : (DisplayMesh)obj; if (displayMesh.WillFitOnGPU == false) { return; } uint vertex_buffer = 0; uint index_buffer = 0; if (displayMesh != null) { // Material setup, if necessary... displayMesh.Material.AmbientColor = new [] { 0.0f, 0.0f, 0.0f, 1.0f }; //We want to ignore the ambient color if (CurrentMaterial.RuntimeId != displayMesh.Material.RuntimeId) { ActiveShader.SetupMaterial(displayMesh.Material); } CurrentMaterial = displayMesh.Material; if (displayMesh.IndexBufferHandle == Globals.UNSET_HANDLE) { // Generate the Index VBO GL.GenBuffers(1, out index_buffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, index_buffer); displayMesh.IndexBufferHandle = index_buffer; } if (displayMesh.VertexBufferHandle == Globals.UNSET_HANDLE) { // Generate the VertexBuffer GL.GenBuffers(1, out vertex_buffer); GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer); displayMesh.VertexBufferHandle = vertex_buffer; displayMesh.LoadDataForVBOs(displayMesh.Mesh); // If CheckGLError turns up an error (likely an OutOfMemory warning because the mesh won't fit on the GPU) // we need to delete the buffers associated with that displayMesh and mark it as too big. bool causesGLError = CheckGLError("GL.BufferData"); if (causesGLError) { GL.DeleteBuffers(1, ref vertex_buffer); GL.DeleteBuffers(1, ref index_buffer); displayMesh.WillFitOnGPU = false; GC.Collect(); } else { displayMesh.WillFitOnGPU = true; } // If we have drawn all the partitions associated with the underlying openNURBS mesh in the ModelFile, we can delete it... if (displayMesh.Mesh.PartitionCount == displayMesh.PartitionIndex + 1) { Model.ModelFile.Objects.Delete(displayMesh.FileObjectId); } displayMesh.Mesh = null; } if (displayMesh.WillFitOnGPU == false) { return; } // Vertices // ORDER MATTERS...if you don't do things in this order, you will get very frusterated. // First, enable the VertexAttribArray for positions int rglVertex = ActiveShader.RglVertexIndex; GL.EnableVertexAttribArray(rglVertex); // Second, Bind the ArrayBuffer GL.BindBuffer(BufferTarget.ArrayBuffer, displayMesh.VertexBufferHandle); // Third, tell GL where to look for the data... GL.VertexAttribPointer(rglVertex, 3, VertexAttribPointerType.Float, false, displayMesh.Stride, IntPtr.Zero); CheckGLError("GL.VertexAttribPointer"); // Normals if (displayMesh.HasVertexNormals) { int rglNormal = ActiveShader.RglNormalIndex; GL.EnableVertexAttribArray(rglNormal); GL.VertexAttribPointer(rglNormal, 3, VertexAttribPointerType.Float, false, displayMesh.Stride, (IntPtr)(Marshal.SizeOf(typeof(Rhino.Geometry.Point3f)))); CheckGLError("GL.VertexAttribPointer"); } // Colors if (displayMesh.HasVertexColors) { int rglColor = ActiveShader.RglColorIndex; GL.EnableVertexAttribArray(rglColor); GL.VertexAttribPointer(rglColor, 4, VertexAttribPointerType.Float, false, displayMesh.Stride, (IntPtr)(Marshal.SizeOf(typeof(Rhino.Display.Color4f)))); CheckGLError("GL.VertexAttribPointer"); } if (isInstance) { if (!FastDrawing) { // Check for inversions on transforms, but only if we are drawing the final "high-quality" frame // (because our PerPixel shader does not flip normals based on modelView matrices). if ((obj as DisplayInstanceMesh).XForm.Determinant < -Globals.ON_ZERO_TOLERANCE) { // an inversion (happens in mirrored instances, etc.) // this means the transformation will turn the mesh // inside out, so we have to reverse our front face // convention. GL.FrontFace(FrontFaceDirection.Cw); } else { GL.FrontFace(FrontFaceDirection.Ccw); } } ActiveShader.SetModelViewMatrix((obj as DisplayInstanceMesh).XForm); } // Bind Indices GL.BindBuffer(BufferTarget.ElementArrayBuffer, displayMesh.IndexBufferHandle); CheckGLError("GL.BindBuffer"); // Draw... #if __ANDROID__ GL.DrawElements(All.Triangles, displayMesh.IndexBufferLength, All.UnsignedShort, IntPtr.Zero); #endif #if __IOS__ GL.DrawElements(BeginMode.Triangles, displayMesh.IndexBufferLength, DrawElementsType.UnsignedShort, IntPtr.Zero); #endif // Disable any and all arrays and buffers we might have used... GL.BindBuffer(BufferTarget.ArrayBuffer, displayMesh.VertexBufferHandle); GL.DisableVertexAttribArray(ActiveShader.RglColorIndex); GL.DisableVertexAttribArray(ActiveShader.RglNormalIndex); GL.DisableVertexAttribArray(ActiveShader.RglVertexIndex); GL.BindBuffer(BufferTarget.ElementArrayBuffer, displayMesh.IndexBufferHandle); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } }