/// <summary> /// Makes the render context current. /// </summary> public override void MakeCurrent() { if (RenderContextHandle != IntPtr.Zero) { Win32.wglMakeCurrent(DeviceContextHandle, RenderContextHandle); } }
// TODO: 此函数的'try to remove unused items from rc2ProcDict'部分尚需检测。 /// <summary> /// 设置Debug模式的回调函数。 /// <para>此函数的'try to remove unused items from rc2ProcDict'部分尚需检测。</para> /// </summary> /// <param name="callback"></param> /// <param name="userParam">建议使用<see cref="UnmanagedArray.Header"/></param> public static void DebugMessageCallback(DebugProc callback, IntPtr userParam) { if (innerCallbackProc == null) { innerCallbackProc = new DEBUGPROC(innerCallback); } IntPtr renderContext = Win32.wglGetCurrentContext(); IntPtr deviceContext = Win32.wglGetCurrentDC(); if (rc2ProcDict.ContainsKey(renderContext)) { rc2ProcDict[renderContext] = callback; rc2dcDict[renderContext] = deviceContext; } else { rc2ProcDict.Add(renderContext, callback); rc2dcDict.Add(renderContext, deviceContext); debugProcDictCount++; // try to remove unused items from rc2ProcDict if (debugProcDictCount > maxDebugProcDictCount) { List <IntPtr> unusedRCList = new List <IntPtr>(); foreach (var item in rc2dcDict) { if (!Win32.wglMakeCurrent(item.Value, item.Key))// 这种检测方式可行吗? { unusedRCList.Add(item.Key); } } foreach (var item in unusedRCList) { rc2ProcDict.Remove(item); rc2dcDict.Remove(item); } debugProcDictCount -= unusedRCList.Count; maxDebugProcDictCount = debugProcDictCount + 100; Win32.wglMakeCurrent(renderContext, deviceContext); } } GetDelegateFor <glDebugMessageCallback>()(innerCallbackProc, userParam); }
private static FontBitmapEntry CreateFontBitmapEntry(string faceName, int height) { // Make the OpenGL instance current. //GL.MakeCurrent(); IntPtr renderContext = Win32.wglGetCurrentContext(); IntPtr deviceContext = Win32.wglGetCurrentDC(); Win32.wglMakeCurrent(deviceContext, renderContext); // Create the font based on the face name. var hFont = Win32.CreateFont(height, 0, 0, 0, Win32.FW_DONTCARE, 0, 0, 0, Win32.DEFAULT_CHARSET, Win32.OUT_OUTLINE_PRECIS, Win32.CLIP_DEFAULT_PRECIS, Win32.CLEARTYPE_QUALITY, Win32.VARIABLE_PITCH, faceName); // Select the font handle. var hOldObject = Win32.SelectObject(deviceContext, hFont); // Create the list base. var listBase = OpenGL.GenLists(1); // Create the font bitmaps. bool result = Win32.wglUseFontBitmaps(deviceContext, 0, 255, listBase); // Reselect the old font. Win32.SelectObject(deviceContext, hOldObject); // Free the font. Win32.DeleteObject(hFont); // Create the font bitmap entry. var fbe = new FontBitmapEntry() { HDC = deviceContext, HRC = renderContext, FaceName = faceName, Height = height, ListBase = listBase, ListCount = 255 }; // Add the font bitmap entry to the internal list. fontBitmapEntries.Add(fbe); return(fbe); }
/// <summary> /// Only valid to be called after the render context is created, this function attempts to /// move the render context to the OpenGL version originally requested. If this is > 2.1, this /// means building a new context. If this fails, we'll have to make do with 2.1. /// </summary> protected void UpdateContextVersion() { // If the request version number is anything up to and including 2.1, standard render contexts // will provide what we need (as long as the graphics card drivers are up to date). // Now the none-trivial case. We must use the WGL_create_context extension to // attempt to create a 3.0+ context. int major, minor; GetHighestVersion(out major, out minor); if ((major > 2) || (major == 2 && minor > 1)) { try { //OpenGL.WGL_CONTEXT_MAJOR_VERSION_ARB, requestedVersionNumber.Major, kw //OpenGL.WGL_CONTEXT_MINOR_VERSION_ARB, requestedVersionNumber.Minor, //OpenGL.WGL_CONTEXT_FLAGS_ARB, OpenGL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, int[] attributes = { GL.WGL_CONTEXT_MAJOR_VERSION, major, GL.WGL_CONTEXT_MINOR_VERSION, minor, GL.WGL_CONTEXT_FLAGS, GL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT,// compatible profile //#if DEBUG // GL.WGL_CONTEXT_FLAGS, GL.WGL_CONTEXT_DEBUG_BIT,// this is a debug context //#endif 0 }; var wglCreateContextAttribs = GL.Instance.GetDelegateFor("wglCreateContextAttribsARB", GLDelegates.typeof_IntPtr_IntPtr_IntPtr_intN) as GLDelegates.IntPtr_IntPtr_IntPtr_intN; IntPtr hrc = wglCreateContextAttribs(this.DeviceContextHandle, IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(this.RenderContextHandle); Win32.wglMakeCurrent(this.DeviceContextHandle, hrc); this.RenderContextHandle = hrc; } catch (Exception ex) { Debug.WriteLine(ex); } } }
/// <summary> /// Only valid to be called after the render context is created, this function attempts to /// move the render context to the OpenGL version originally requested. If this is > 2.1, this /// means building a new context. If this fails, we'll have to make do with 2.1. /// </summary> protected void UpdateContextVersion() { // If the request version number is anything up to and including 2.1, standard render contexts // will provide what we need (as long as the graphics card drivers are up to date). var requestedVersionNumber = VersionAttribute.GetVersionAttribute(this.RequestedGLVersion); if (requestedVersionNumber.IsAtLeastVersion(3, 0) == false) { this.CreatedGLVersion = this.RequestedGLVersion; return; } // Now the none-trivial case. We must use the WGL_ARB_create_context extension to // attempt to create a 3.0+ context. try { int[] attributes = { OpenGL.WGL_CONTEXT_MAJOR_VERSION_ARB, requestedVersionNumber.Major, OpenGL.WGL_CONTEXT_MINOR_VERSION_ARB, requestedVersionNumber.Minor, OpenGL.WGL_CONTEXT_FLAGS_ARB, OpenGL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // compatible profile #if DEBUG OpenGL.WGL_CONTEXT_FLAGS_ARB, OpenGL.WGL_CONTEXT_DEBUG_BIT_ARB, // this is a debug context #endif 0 }; IntPtr hrc = OpenGL.GetDelegateFor <OpenGL.wglCreateContextAttribsARB>()(this.DeviceContextHandle, IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(this.RenderContextHandle); Win32.wglMakeCurrent(this.DeviceContextHandle, hrc); this.RenderContextHandle = hrc; } catch (Exception) { // TODO: can we actually get the real version? this.CreatedGLVersion = GLVersion.OpenGL2_1; } }
/// <summary> /// Only valid to be called after the render context is created, this function attempts to /// move the render context to the OpenGL version originally requested. If this is > 2.1, this /// means building a new context. If this fails, we'll have to make do with 2.1. /// </summary> /// <param name="parameters"></param> protected bool UpdateContextVersion(ContextGenerationParams parameters) { // If the request version number is anything up to and including 2.1, standard render contexts // will provide what we need (as long as the graphics card drivers are up to date). // Now the none-trivial case. We must use the WGL_create_context extension to // attempt to create a 3.0+ context. try { var wglChoosePixelFormatARB = GL.Instance.GetDelegateFor("wglChoosePixelFormatARB", GLDelegates.typeof_bool_IntPtr_intN_floatN_uint_intN_uintN) as GLDelegates.bool_IntPtr_intN_floatN_uint_intN_uintN; if (wglChoosePixelFormatARB == null) { return(false); } var wglCreateContextAttribs = GL.Instance.GetDelegateFor("wglCreateContextAttribsARB", GLDelegates.typeof_IntPtr_IntPtr_IntPtr_intN) as GLDelegates.IntPtr_IntPtr_IntPtr_intN; if (wglCreateContextAttribs == null) { return(false); } int major, minor; GetHighestVersion(out major, out minor); if ((major > 2) || (major == 2 && minor > 1)) { int[] attribList = new int[] { WinGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE, WinGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE, WinGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE, WinGL.WGL_ACCELERATION_ARB, WinGL.WGL_FULL_ACCELERATION_ARB, WinGL.WGL_PIXEL_TYPE_ARB, WinGL.WGL_TYPE_RGBA_ARB, WinGL.WGL_COLOR_BITS_ARB, parameters.ColorBits, WinGL.WGL_ACCUM_BITS_ARB, parameters.AccumBits, WinGL.WGL_ACCUM_RED_BITS_ARB, parameters.AccumRedBits, WinGL.WGL_ACCUM_GREEN_BITS_ARB, parameters.AccumGreenBits, WinGL.WGL_ACCUM_BLUE_BITS_ARB, parameters.AccumBlueBits, WinGL.WGL_ACCUM_ALPHA_BITS_ARB, parameters.AccumAlphaBits, WinGL.WGL_DEPTH_BITS_ARB, parameters.DepthBits, WinGL.WGL_STENCIL_BITS_ARB, parameters.StencilBits, 0, //End }; IntPtr dc = this.DeviceContextHandle; // Match an appropriate pixel format int[] pixelFormat = new int[1]; uint[] numFormats = new uint[1]; if (false == wglChoosePixelFormatARB(dc, attribList, null, 1, pixelFormat, numFormats)) { return(false); } // Sets the pixel format if (false == Win32.SetPixelFormat(dc, pixelFormat[0], new PixelFormatDescriptor())) { return(false); } int[] attributes = { WinGL.WGL_CONTEXT_MAJOR_VERSION_ARB, major, WinGL.WGL_CONTEXT_MINOR_VERSION_ARB, minor, WinGL.WGL_CONTEXT_PROFILE_MASK_ARB, WinGL.WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //#if DEBUG // GL.WGL_CONTEXT_FLAGS, GL.WGL_CONTEXT_DEBUG_BIT,// this is a debug context //#endif 0 }; IntPtr hrc = wglCreateContextAttribs(dc, IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(this.RenderContextHandle); Win32.wglMakeCurrent(dc, hrc); this.RenderContextHandle = hrc; } } catch (Exception ex) { Debug.WriteLine(ex); Log.Write(ex); } return(true); }