/// <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.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> /// This is called immediately after the surface is first created. /// </summary> /// <param name="holder"> /// The SurfaceHolder whose surface is being created. /// </param> public void SurfaceCreated(ISurfaceHolder holder) { // Problem with static constructors? Ensure manual initialization Egl.Initialize(); Gl.Initialize(); // Get actual native window handle _NativeWindowHandle = ANativeWindow_fromSurface(JNIEnv.Handle, holder.Surface.Handle); // Create device context _DeviceContext = DeviceContext.Create(IntPtr.Zero, _NativeWindowHandle); _DeviceContext.IncRef(); // Set pixel format DevicePixelFormatCollection pixelFormats = _DeviceContext.PixelsFormats; DevicePixelFormat controlReqFormat = new DevicePixelFormat(); controlReqFormat.RgbaUnsigned = true; controlReqFormat.RenderWindow = true; controlReqFormat.ColorBits = 24; //controlReqFormat.DepthBits = (int)DepthBits; //controlReqFormat.StencilBits = (int)StencilBits; //controlReqFormat.MultisampleBits = (int)MultisampleBits; //controlReqFormat.DoubleBuffer = true; List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException("unable to find a suitable pixel format"); } _DeviceContext.SetPixelFormat(matchingPixelFormats[0]); // Create OpenGL context using compatibility profile if ((_RenderContext = _DeviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero) { throw new InvalidOperationException("unable to create render context"); } // Make context current if (_DeviceContext.MakeCurrent(_RenderContext) == false) { throw new InvalidOperationException("unable to make context current"); } // Raise relative event OnContextCreated(); StartRendering(30.0f); }
/// <summary> /// Get the APIs available on this host. /// </summary> /// <returns> /// It returns the list of the APIs available on the current host. /// </returns> public static IEnumerable <string> GetAvailableAPIs() { List <string> availableAPIs = new List <string>(); // Uses the default device using (DeviceContext deviceContext = DeviceContext.Create()) { availableAPIs.AddRange(deviceContext.AvailableAPIs); } #if INTEGRATES_EGL // Consider EGL availability if (!Egl.IsRequired && Egl.IsAvailable) { // Integrates EGL device context APIss Egl.IsRequired = true; try { if (Egl.IsRequired == true) { using (DeviceContext deviceContext = DeviceContext.Create()) { foreach (string availableAPI in deviceContext.AvailableAPIs) { if (!availableAPIs.Contains(availableAPI)) { availableAPIs.Add(availableAPI); } } } } } finally { Egl.IsRequired = false; } } else { } #endif return(availableAPIs); }
/// <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 !NETSTANDARD1_1 // Optional initialization string envGlInit = Environment.GetEnvironmentVariable("OPENGL_NET_INIT"); if (envGlInit != null && envGlInit == "NO") { return; } #endif // 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> /// Create the device context and set the pixel format. /// </summary> private void CreateDeviceContext(DevicePixelFormat controlReqFormat) { #region Support ES/SC API switch (_ProfileType) { case ProfileType.Embedded: DeviceContext.DefaultAPI = KhronosVersion.ApiGles2; break; case ProfileType.SecurityCritical2: DeviceContext.DefaultAPI = KhronosVersion.ApiGlsc2; break; } #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> /// 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()); } }