/// <summary> /// Gets the version attribute of an enumeration value <paramref name="enumeration"/>. /// </summary> /// <param name="enumeration">The enumeration.</param> /// <returns>The <see cref="VersionAttribute"/> defined on <paramref name="enumeration "/>, or null of none exists.</returns> public static VersionAttribute GetVersionAttribute(Enum enumeration) { // Get the attribute from the enumeration value (if it exists). Type type = enumeration.GetType(); string str = enumeration.ToString(); MemberInfo[] members = type.GetMember(str); MemberInfo member = members.Single(); object[] objs = member.GetCustomAttributes(typeof(VersionAttribute), false); IEnumerable <VersionAttribute> attributes = objs.OfType <VersionAttribute>(); VersionAttribute firstOrDefault = attributes.FirstOrDefault(); return(firstOrDefault); }
/// <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="gl">The OpenGL instance.</param> 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(RequestedGLVersion); if (requestedVersionNumber.IsAtLeastVersion(3, 0) == false) { CreatedGLVersion = 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 = { GL.WGL_CONTEXT_MAJOR_VERSION_ARB, requestedVersionNumber.Major, GL.WGL_CONTEXT_MINOR_VERSION_ARB, requestedVersionNumber.Minor, GL.WGL_CONTEXT_FLAGS_ARB, GL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // compatible profile #if DEBUG GL.WGL_CONTEXT_FLAGS_ARB, GL.WGL_CONTEXT_DEBUG_BIT_ARB, // this is a debug context #endif 0 }; IntPtr hrc = GL.GetDelegateFor <GL.wglCreateContextAttribsARB>()(this.DeviceContextHandle, IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(RenderContextHandle); Win32.wglMakeCurrent(DeviceContextHandle, hrc); RenderContextHandle = hrc; } catch (Exception) { // TODO: can we actually get the real version? CreatedGLVersion = GLVersion.OpenGL2_1; } }