private static void DrawWithWireOverlay(Mesh meshToRender, RenderTypes renderType) { GLMeshTrianglePlugin glMeshPlugin = GLMeshTrianglePlugin.Get(meshToRender); GL.Enable(EnableCap.PolygonOffsetFill); GL.PolygonOffset(1, 1); DrawToGL(meshToRender); GL.Color4(0, 0, 0, 255); GL.PolygonOffset(0, 0); GL.Disable(EnableCap.PolygonOffsetFill); GL.Disable(EnableCap.Lighting); GL.DisableClientState(ArrayCap.TextureCoordArray); GLMeshWirePlugin glWireMeshPlugin = null; if (renderType == RenderTypes.Outlines) { glWireMeshPlugin = GLMeshWirePlugin.Get(meshToRender, MathHelper.Tau / 8); } else { glWireMeshPlugin = GLMeshWirePlugin.Get(meshToRender); } VectorPOD <WireVertexData> edegLines = glWireMeshPlugin.edgeLinesData; GL.EnableClientState(ArrayCap.VertexArray); #if true unsafe { fixed(WireVertexData *pv = edegLines.Array) { GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(pv)); GL.DrawArrays(BeginMode.Lines, 0, edegLines.Count); } } #else GL.InterleavedArrays(InterleavedArrayFormat.V3f, 0, edegLines.Array); GL.DrawArrays(BeginMode.Lines, 0, edegLines.Count); #endif GL.DisableClientState(ArrayCap.VertexArray); GL.Enable(EnableCap.Lighting); }
public static void EnsureUpdated(Mesh meshToRender, Matrix4X4 transform) { var faces = meshToRender.Faces; if (faces?.Count < 1) { return; } if (!meshToRender.PropertyBag.ContainsKey("Face0WorldZAngle")) { meshToRender.PropertyBag.Add("Face0WorldZAngle", new NormalZ()); } var normalZ = meshToRender.PropertyBag["Face0WorldZAngle"] as NormalZ; var face0Normal = faces[0].normal.TransformNormal(transform).GetNormal(); var error = .0001; if (normalZ.z < face0Normal.Z - error || normalZ.z > face0Normal.Z + error) { meshToRender.MarkAsChanged(); normalZ.z = face0Normal.Z; } // change the color to be the right thing per face normal GLMeshTrianglePlugin.Get( meshToRender, (normal) => { normal = normal.TransformNormal(transform).GetNormal(); double startColor = 223.0 / 360.0; double endColor = 5.0 / 360.0; double delta = endColor - startColor; var polyColor = ColorF.FromHSL(startColor, .99, .49).ToColor(); if (normal.Z < 0) { polyColor = ColorF.FromHSL(startColor - delta * normal.Z, .99, .49).ToColor(); } return(polyColor); }); }
private static void DrawToGL(Mesh meshToRender, bool isTransparent, Matrix4X4?meshToViewTransform, bool blendTexture = true, bool allowBspRendering = true) { if (!blendTexture) { // Turn off default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_REPLACE); } if (meshToViewTransform != null && isTransparent && meshToRender.FaceBspTree != null && meshToRender.Faces.Count > 0 && allowBspRendering) { var invMeshToViewTransform = meshToViewTransform.Value; invMeshToViewTransform.Invert(); DrawToGLZSorted(meshToRender, meshToViewTransform.Value, invMeshToViewTransform); if (!blendTexture) { // Restore default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_MODULATE); } return; } var glMeshPlugin = GLMeshTrianglePlugin.Get(meshToRender); for (int i = 0; i < glMeshPlugin.subMeshs.Count; i++) { SubTriangleMesh subMesh = glMeshPlugin.subMeshs[i]; // Make sure the GLMeshPlugin has a reference to hold onto the image so it does not go away before this. if (subMesh.texture != null) { if (subMesh.texture.HasTransparency) { GL.Enable(EnableCap.Blend); } var glPlugin = ImageGlPlugin.GetImageGlPlugin(subMesh.texture, true); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, glPlugin.GLTextureHandle); GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.Disable(EnableCap.Texture2D); GL.DisableClientState(ArrayCap.TextureCoordArray); } if (subMesh.UseVertexColors) { GL.EnableClientState(ArrayCap.ColorArray); } GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.VertexArray); unsafe { fixed(VertexTextureData *pTextureData = subMesh.textureData.Array) { fixed(VertexColorData *pColorData = subMesh.colorData.Array) { fixed(VertexNormalData *pNormalData = subMesh.normalData.Array) { fixed(VertexPositionData *pPosition = subMesh.positionData.Array) { GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(pPosition)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(pNormalData)); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(pTextureData)); if (pColorData != null) { GL.ColorPointer(3, ColorPointerType.UnsignedByte, 0, new IntPtr(pColorData)); } GL.DrawArrays(BeginMode.Triangles, 0, subMesh.positionData.Count); } } } } } GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.DisableClientState(ArrayCap.ColorArray); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(0)); GL.ColorPointer(3, ColorPointerType.UnsignedByte, 0, new IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(0)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(0)); if (subMesh.texture != null) { GL.DisableClientState(ArrayCap.TextureCoordArray); } } if (!blendTexture) { // Restore default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_MODULATE); } }
private static void DrawToGL(Mesh meshToRender) { GLMeshTrianglePlugin glMeshPlugin = GLMeshTrianglePlugin.Get(meshToRender); for (int i = 0; i < glMeshPlugin.subMeshs.Count; i++) { SubTriangleMesh subMesh = glMeshPlugin.subMeshs[i]; // Make sure the GLMeshPlugin has a reference to hold onto the image so it does not go away before this. if (subMesh.texture != null) { ImageGlPlugin glPlugin = ImageGlPlugin.GetImageGlPlugin(subMesh.texture, true); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, glPlugin.GLTextureHandle); GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.Disable(EnableCap.Texture2D); GL.DisableClientState(ArrayCap.TextureCoordArray); } #if true GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.VertexArray); unsafe { fixed(VertexTextureData *pTextureData = subMesh.textrueData.Array) { fixed(VertexNormalData *pNormalData = subMesh.normalData.Array) { fixed(VertexPositionData *pPosition = subMesh.positionData.Array) { GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(pTextureData)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(pNormalData)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(pPosition)); GL.DrawArrays(BeginMode.Triangles, 0, subMesh.positionData.Count); } } } } #else GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f, 0, subMesh.vertexDatas.Array); if (subMesh.texture != null) { //GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexData.Stride, subMesh.vertexDatas.Array); //GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.DisableClientState(ArrayCap.TextureCoordArray); } #endif GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(0)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(0)); if (subMesh.texture != null) { GL.DisableClientState(ArrayCap.TextureCoordArray); } } }