/// <summary> /// Initialize OpenGL namespace static environment. This method shall be called before any other classes methods. /// </summary> public static void Initialize() { if (_Initialized == true) { return; // Already initialized } _Initialized = true; // Optional initialization string envGlInit = Environment.GetEnvironmentVariable("OPENGL_NET_INIT"); if (envGlInit != null && envGlInit == "NO") { return; } // Environment options LogComment("OpenGL.Net is initializing"); // Loader function OS API GL API // ------------------------------------------------------ // Supported platform: Windows // wglGetProcAddress WGL GL // wglGetProcAddress WGL GLES2+ (with WGL_create_context_es(2)?_profile_EXT) // eglGetProcAddress EGL(Angle) GLES2+ // ------------------------------------------------------ // Supported platform: Linux // glXGetProcAddress GLX GL // glXGetProcAddress GLX GLES2+ (with GLX_create_context_es(2)?_profile_EXT) // eglGetProcAddress EGL GLES2+ // ------------------------------------------------------ // Supported platform: Android // eglGetProcAddress EGL GL // eglGetProcAddress EGL GLES2+ try { #if !MONODROID // Determine whether use EGL as device context backend if (Egl.IsAvailable) { switch (Platform.CurrentPlatformId) { case Platform.Id.Linux: if (Glx.IsAvailable == false) { Egl.IsRequired = true; } break; } } #endif // Create native window for getting preliminary information on desktop systems // This instance will be used for creating contexts without explictly specify a window _NativeWindow = DeviceContext.CreateHiddenWindow(); // Create device context using (DeviceContext windowDevice = DeviceContext.Create()) { // Create basic OpenGL context IntPtr renderContext = windowDevice.CreateSimpleContext(); if (renderContext == IntPtr.Zero) { throw new NotImplementedException("unable to create a simple context"); } // Make contect current if (windowDevice.MakeCurrent(renderContext) == false) { throw new InvalidOperationException("unable to make current", windowDevice.GetPlatformException()); } #if !MONODROID // Reload platform function pointers, if required if (Egl.IsRequired == false) { switch (Platform.CurrentPlatformId) { case Platform.Id.WindowsNT: Wgl.BindAPI(); break; } } #endif // Query OpenGL informations string glVersion = GetString(StringName.Version); _CurrentVersion = KhronosVersion.Parse(glVersion); // Query OpenGL extensions (current OpenGL implementation, CurrentCaps) _CurrentExtensions = new Extensions(); _CurrentExtensions.Query(); // Query platform extensions windowDevice.QueryPlatformExtensions(); // Query OpenGL limits _CurrentLimits = Limits.Query(Gl.CurrentVersion, _CurrentExtensions); // Obtain current OpenGL Shading Language version string glslVersion = null; switch (_CurrentVersion.Api) { case KhronosVersion.ApiGl: if (_CurrentVersion >= Version_200 || _CurrentExtensions.ShadingLanguage100_ARB) { glslVersion = GetString(StringName.ShadingLanguageVersion); } break; case KhronosVersion.ApiGles2: glslVersion = GetString(StringName.ShadingLanguageVersion); break; } if (glslVersion != null) { _CurrentShadingVersion = GlslVersion.Parse(glslVersion, _CurrentVersion.Api); } // Vendor/Render information _Vendor = GetString(StringName.Vendor); _Renderer = GetString(StringName.Renderer); if (EnvDebug || EnvExperimental) { Debug.Assert(CurrentVersion != null && CurrentExtensions != null); CheckExtensionCommands <Gl>(CurrentVersion, CurrentExtensions, EnvExperimental); } // Before deletion, make uncurrent windowDevice.MakeCurrent(IntPtr.Zero); // Detroy context if (windowDevice.DeleteContext(renderContext) == false) { throw new InvalidOperationException("unable to delete OpenGL context"); } } LogComment("OpenGL.Net has been initialized"); } catch (Exception excepton) { _InitializationException = excepton; LogComment("Unable to initialize OpenGL.Net: {0}", _InitializationException.ToString()); } }
/// <summary> /// Initialize OpenGL namespace static environment. This method shall be called before any other classes methods. /// </summary> public static void Initialize() { if (_Initialized == true) { return; // Already initialized } _Initialized = true; #if DEBUG string envGlInit = Environment.GetEnvironmentVariable("GL_INIT"); if (envGlInit != null && envGlInit == "NO") { return; } #endif LogComment("OpenGL.Net is initializing"); // Loader function OS API GL API // ------------------------------------------------------ // Supported platform: Windows // wglGetProcAddress WGL GL // wglGetProcAddress WGL GLES2+ (with WGL_create_context_es(2)?_profile_EXT) // eglGetProcAddress EGL(Angle) GLES2+ // ------------------------------------------------------ // Supported platform: Linux // glXGetProcAddress GLX GL // glXGetProcAddress GLX GLES2+ (with GLX_create_context_es(2)?_profile_EXT) // ------------------------------------------------------ // Supported platform: Android // eglGetProcAddress EGL GL // eglGetProcAddress EGL GLES2+ try { // Create native window for getting preliminary information on desktop systems // This instance will be used for creating contexts without explictly specify a window _NativeWindow = DeviceContext.CreateHiddenWindow(); // Create device context using (DeviceContext windowDevice = DeviceContext.Create()) { // Create basic OpenGL context IntPtr renderContext = windowDevice.CreateSimpleContext(); if (renderContext == IntPtr.Zero) { throw new NotImplementedException("unable to create a simple context"); } // Make contect current if (windowDevice.MakeCurrent(renderContext) == false) { throw new InvalidOperationException("unable to make current", windowDevice.GetPlatformException()); } // Query OpenGL informations string glVersion = GetString(StringName.Version); _CurrentVersion = KhronosVersion.Parse(glVersion); // Obtain current OpenGL Shading Language version switch (_CurrentVersion.Api) { case KhronosVersion.ApiGl: case KhronosVersion.ApiGles2: string glslVersion = GetString(StringName.ShadingLanguageVersion); _CurrentShadingVersion = GlslVersion.Parse(glslVersion); break; } // Vendor/Render information _Vendor = GetString(StringName.Vendor); _Renderer = GetString(StringName.Renderer); // Query OpenGL extensions (current OpenGL implementation, CurrentCaps) _CurrentExtensions = new Extensions(); _CurrentExtensions.Query(); // Query OpenGL limits _CurrentLimits = Limits.Query(_CurrentExtensions); // Query platform extensions windowDevice.QueryPlatformExtensions(); // Before deletion, make uncurrent windowDevice.MakeCurrent(IntPtr.Zero); // Detroy context if (windowDevice.DeleteContext(renderContext) == false) { throw new InvalidOperationException("unable to delete OpenGL context"); } } LogComment("OpenGL.Net has been initialized"); } catch (Exception excepton) { LogComment("Unable to initialize OpenGL.Net: {0}", excepton.ToString()); } }
/// <summary> /// Create the device context and set the pixel format. /// </summary> private void CreateDeviceContext(DevicePixelFormat controlReqFormat) { #region Support ES API if (_ProfileType == ProfileType.Embedded) { DeviceContext.DefaultApi = KhronosVersion.ApiGles2; } #endregion #region Create device context _DeviceContext = DeviceContext.Create(GetDisplay(), this.Handle); _DeviceContext.IncRef(); #endregion #region Set pixel format DevicePixelFormatCollection pixelFormats = _DeviceContext.PixelsFormats; List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat); if ((matchingPixelFormats.Count == 0) && controlReqFormat.MultisampleBits > 0) { // Try to select the maximum multisample configuration int multisampleBits = 0; pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); }); controlReqFormat.MultisampleBits = multisampleBits; matchingPixelFormats = pixelFormats.Choose(controlReqFormat); } if ((matchingPixelFormats.Count == 0) && controlReqFormat.DoubleBuffer) { // Try single buffered pixel formats controlReqFormat.DoubleBuffer = false; matchingPixelFormats = pixelFormats.Choose(controlReqFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat))); } } else if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(controlReqFormat))); } _DeviceContext.SetPixelFormat(matchingPixelFormats[0]); #endregion #region Set V-Sync if (Gl.PlatformExtensions.SwapControl) { int swapInterval = SwapInterval; // Mask value in case it is not supported if (!Gl.PlatformExtensions.SwapControlTear && swapInterval == -1) { swapInterval = 1; } _DeviceContext.SwapInterval(swapInterval); } #endregion }
/// <summary> /// Bind the OpenGL delegates for the API corresponding to the current OpenGL context. /// </summary> public static void BindAPI() { BindAPI(DeviceContext.QueryContextVersion(), GetProcAddress.GetProcAddressGL); }