/// <summary> /// Sets up an ES2.0 material with a DisplayMaterial /// </summary> public void SetupMaterial(DisplayMaterial material) { float[] black = { 0.0f, 0.0f, 0.0f, 1.0f }; float[] pspec = Convert.ToBoolean(material.Shine) ? material.SpecularColor : black; if (m_Uniforms.rglLightAmbient >= 0) { if (material.AmbientColor[3] > 0) { GL.Uniform4(m_Uniforms.rglLightAmbient, 1, material.AmbientColor); } else { GL.Uniform4(m_Uniforms.rglLightAmbient, 1, black); } } if (m_Uniforms.rglDiffuse >= 0) { GL.Uniform4(m_Uniforms.rglDiffuse, 1, material.DiffuseColor); } if (m_Uniforms.rglSpecular >= 0) { GL.Uniform4(m_Uniforms.rglSpecular, 1, pspec); } if (m_Uniforms.rglEmission >= 0) { GL.Uniform4(m_Uniforms.rglEmission, 1, material.EmissionColor); } if (m_Uniforms.rglShininess >= 0) { GL.Uniform1(m_Uniforms.rglShininess, material.Shine); } if (m_Uniforms.rglUsesColors >= 0) { GL.Uniform1(m_Uniforms.rglUsesColors, 0); } if (material.Alpha < 1.0) { GL.Enable(EnableCap.Blend); } else { GL.Disable(EnableCap.Blend); } }
/// <summary> /// Initializes a DisplayMesh from a Rhino.Geometry.Mesh. /// </summary> public DisplayMesh(Mesh mesh, int partitionIndex, DisplayMaterial material, bool shouldCaptureVBOData, Guid fileObjectId) { Mesh = mesh; FileObjectId = fileObjectId; Material = material; m_partitionIndex = partitionIndex; BoundingBox = mesh.GetBoundingBox(true); // Check for normals... if (mesh.Normals.Count > 0) { HasVertexNormals = true; } else { HasVertexNormals = false; } // Check for colors... if (mesh.VertexColors.Count > 0) { HasVertexColors = true; } else { HasVertexColors = false; } m_initializationFailed = false; CaptureVBOData = shouldCaptureVBOData; IsClosed = false; IsClosed |= mesh.IsClosed; if (m_initializationFailed) { return; } CaptureVBOData = false; }
/// <summary> /// Draws all transparent meshes /// </summary> private void RenderTransparentObjects(RMModel model) { // Drawing transparent meshes is a 3 pass process... // // Pass #1: With depth buffer writing OFF // i. Draw all objects' backfaces // ii. Draw all "open" objects' front faces. // // Pass #2: With depth buffer writing ON // i. Draw all objects' front faces // // Pass #3: With depth buffer writing ON // i. Draw all "open" objects' back faces // // Provided we actually have a model to render... if (model != null) { CurrentMaterial = new DisplayMaterial(); //reset to the default (unset) material // ... render all transparent meshes... if (model.TransparentObjects.Count > 0) { //Pass #1 GL.DepthMask(false); GL.Enable(EnableCap.CullFace); // i. Draw all objects' backfaces GL.CullFace(CullFaceMode.Front); foreach (DisplayMesh mesh in model.TransparentObjects) { if (mesh != null) { RenderObject(mesh, Viewport, false); // ii. Draw all "open" objects' front faces. if (!mesh.IsClosed) { GL.CullFace(CullFaceMode.Back); RenderObject(mesh, Viewport, false); } } } // Pass #2: Draw all objects' front faces GL.DepthMask(true); GL.CullFace(CullFaceMode.Back); foreach (DisplayMesh mesh in model.TransparentObjects) { RenderObject(mesh, Viewport, false); } // Pass #3: Draw all "open" objects' back faces GL.CullFace(CullFaceMode.Front); foreach (DisplayMesh mesh in model.TransparentObjects) { if ((mesh != null) && (!mesh.IsClosed)) { RenderObject(mesh, Viewport, false); } } GL.Disable(EnableCap.CullFace); } // ...then render all transparent instance meshes... if (model.TransparentInstanceObjects.Count > 0) { //Pass #1 GL.DepthMask(false); GL.Enable(EnableCap.CullFace); // i. Draw all objects' backfaces GL.CullFace(CullFaceMode.Front); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) { if (instance.Mesh != null) { RenderObject(instance, Viewport, true); // ii. Draw all "open" objects' front faces. if (!instance.Mesh.IsClosed) { GL.CullFace(CullFaceMode.Back); RenderObject(instance, Viewport, true); } } } // Pass #2: Draw all objects' front faces GL.DepthMask(true); GL.CullFace(CullFaceMode.Back); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) { RenderObject(instance, Viewport, true); } // Pass #3: Draw all "open" objects' back faces GL.CullFace(CullFaceMode.Front); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) { if ((instance.Mesh != null) && (!instance.Mesh.IsClosed)) { RenderObject(instance, Viewport, true); } } GL.Disable(EnableCap.CullFace); } } }
public ES2Renderer() { Shaders = new List <RhGLShaderProgram> (); CurrentMaterial = new DisplayMaterial(); }
public ES2Renderer() { Shaders = new List<RhGLShaderProgram> (); CurrentMaterial = new DisplayMaterial (); }
/// <summary> /// Draws all transparent meshes /// </summary> private void RenderTransparentObjects(RMModel model) { // Drawing transparent meshes is a 3 pass process... // // Pass #1: With depth buffer writing OFF // i. Draw all objects' backfaces // ii. Draw all "open" objects' front faces. // // Pass #2: With depth buffer writing ON // i. Draw all objects' front faces // // Pass #3: With depth buffer writing ON // i. Draw all "open" objects' back faces // // Provided we actually have a model to render... if (model != null) { CurrentMaterial = new DisplayMaterial (); //reset to the default (unset) material // ... render all transparent meshes... if (model.TransparentObjects.Count > 0) { //Pass #1 GL.DepthMask (false); GL.Enable (EnableCap.CullFace); // i. Draw all objects' backfaces GL.CullFace (CullFaceMode.Front); foreach (DisplayMesh mesh in model.TransparentObjects) { if (mesh != null) { RenderObject (mesh, Viewport, false); // ii. Draw all "open" objects' front faces. if (!mesh.IsClosed) { GL.CullFace (CullFaceMode.Back); RenderObject (mesh, Viewport, false); } } } // Pass #2: Draw all objects' front faces GL.DepthMask (true); GL.CullFace (CullFaceMode.Back); foreach (DisplayMesh mesh in model.TransparentObjects) RenderObject (mesh, Viewport, false); // Pass #3: Draw all "open" objects' back faces GL.CullFace (CullFaceMode.Front); foreach (DisplayMesh mesh in model.TransparentObjects) { if ((mesh != null) && (!mesh.IsClosed)) RenderObject (mesh, Viewport, false); } GL.Disable (EnableCap.CullFace); } // ...then render all transparent instance meshes... if (model.TransparentInstanceObjects.Count > 0) { //Pass #1 GL.DepthMask (false); GL.Enable (EnableCap.CullFace); // i. Draw all objects' backfaces GL.CullFace (CullFaceMode.Front); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) { if (instance.Mesh != null) { RenderObject (instance, Viewport, true); // ii. Draw all "open" objects' front faces. if (!instance.Mesh.IsClosed) { GL.CullFace (CullFaceMode.Back); RenderObject (instance, Viewport, true); } } } // Pass #2: Draw all objects' front faces GL.DepthMask (true); GL.CullFace (CullFaceMode.Back); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) RenderObject (instance, Viewport, true); // Pass #3: Draw all "open" objects' back faces GL.CullFace (CullFaceMode.Front); foreach (DisplayInstanceMesh instance in model.TransparentInstanceObjects) { if ((instance.Mesh != null) && (!instance.Mesh.IsClosed)) RenderObject (instance, Viewport, true); } GL.Disable (EnableCap.CullFace); } } }
/// <summary> /// Sets up an ES2.0 material with a DisplayMaterial /// </summary> public void SetupMaterial(DisplayMaterial material) { float[] black = { 0.0f, 0.0f, 0.0f, 1.0f }; float[] pspec = Convert.ToBoolean (material.Shine) ? material.SpecularColor : black; if (m_Uniforms.rglLightAmbient >= 0) { if (material.AmbientColor[3] > 0) { GL.Uniform4 (m_Uniforms.rglLightAmbient, 1, material.AmbientColor); } else GL.Uniform4 (m_Uniforms.rglLightAmbient, 1, black); } if (m_Uniforms.rglDiffuse >= 0) GL.Uniform4 (m_Uniforms.rglDiffuse, 1, material.DiffuseColor); if (m_Uniforms.rglSpecular >= 0) GL.Uniform4 (m_Uniforms.rglSpecular, 1, pspec); if (m_Uniforms.rglEmission >= 0) { GL.Uniform4 (m_Uniforms.rglEmission, 1, material.EmissionColor); } if (m_Uniforms.rglShininess >= 0) GL.Uniform1 (m_Uniforms.rglShininess, material.Shine); if (m_Uniforms.rglUsesColors >= 0) GL.Uniform1 (m_Uniforms.rglUsesColors, 0); if (material.Alpha < 1.0) GL.Enable (EnableCap.Blend); else GL.Disable (EnableCap.Blend); }
/// <summary> /// These variables are difficult to archive so we restore them when reloading the model /// </summary> public void RestoreUsingMesh(Mesh mesh, DisplayMaterial newMaterial) { Material = newMaterial; BoundingBox = mesh.GetBoundingBox(true); }
/// <summary> /// NOTE: Not called in current implementation. /// Models with large meshes must partition the meshes before displaying them on the device. /// Partitioning meshes is a lengthy process and can take > 80% of the model loading time. /// We save the raw VBO data of a mesh in an archive after a mesh has been partitioned /// and use that archive to create the VBOs next time we display the model. /// </summary> protected virtual bool LoadMeshCaches(Mesh mesh, ObjectAttributes attr, DisplayMaterial material) { string meshGUIDString = attr.ObjectId.ToString (); string meshCachePath = SupportPathForName (meshGUIDString + ".meshes"); List<DisplayObject> displayMeshes = new List<DisplayObject> (); try { using (var meshFile = File.Open (meshCachePath, FileMode.Open)) { BinaryFormatter bin = new BinaryFormatter (); displayMeshes = (List<DisplayObject>)bin.Deserialize (meshFile); } } catch (IOException ex) { System.Diagnostics.Debug.WriteLine ("Could not Deserialize the DisplayMeshes with exception: {0}", ex.Message); Rhino.Runtime.HostUtils.ExceptionReport (ex); } if (displayMeshes == null) return false; foreach (DisplayMesh me in displayMeshes) me.RestoreUsingMesh (mesh, material); if (Math.Abs (material.Transparency) < double.Epsilon) DisplayObjects.AddRange (displayMeshes); else TransparentObjects.AddRange ((IEnumerable<DisplayMesh>)displayMeshes); return true; }