/// <summary> /// Create the device context and set the pixel format. /// </summary> protected void CreateDeviceContext() { // Create device context _DeviceContext = DeviceContextFactory.Create(this); _DeviceContext.IncRef(); // Set pixel format DevicePixelFormatCollection pixelFormats = _DeviceContext.PixelsFormats; DevicePixelFormat controlReqFormat = new DevicePixelFormat(); controlReqFormat.ColorBits = (int)ColorBits; controlReqFormat.DepthBits = (int)DepthBits; controlReqFormat.StencilBits = (int)StencilBits; controlReqFormat.MultisampleBits = (int)MultisampleBits; controlReqFormat.DoubleBuffer = DoubleBuffer; List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException("unable to find a suitable pixel format"); } _DeviceContext.SetPixelFormat(matchingPixelFormats[0]); }
/// <summary> /// Actually create this GraphicsWindow resources. /// </summary> /// <param name="ctx"> /// A <see cref="GraphicsContext"/> used for allocating resources. /// </param> protected override void CreateObject(GraphicsContext ctx) { if (ctx != null) { throw new ArgumentException("context not required", "ctx"); } sLog.Info("Create rendering window '{0}'.", _RenderForm != null ? _RenderForm.Text : "untitled"); // Obtain device context (relative to window) _DeviceContext = DeviceContextFactory.Create(_RenderControl); _DeviceContext.IncRef(); // Choose "best" pixel format matching with surface configuration _DeviceFormat = _SurfaceFormat.ChoosePixelFormat(_DeviceContext, ValidPixelFormat); // Set device pixel format _DeviceContext.SetPixelFormat(_DeviceFormat); // Confirm surface configuration _SurfaceFormat.SetBufferConfiguration(_DeviceFormat); // Set swap interval SetSwapInterval(); }
/// <summary> /// Static constructor. /// </summary> static Gl() { // Cache imports & delegates _Delegates = GetDelegateList(typeof(Gl)); _ImportMap = GetImportMap(typeof(Gl)); // Load procedures (OpenGL desktop by default) @todo Really necessary? LoadProcDelegates(_ImportMap, _Delegates); // Create common hidden window _HiddenWindow = new System.Windows.Forms.Form(); // Create device context _HiddenWindowDevice = DeviceContextFactory.Create(_HiddenWindow); _HiddenWindowDevice.IncRef(); // Create basic OpenGL context IntPtr renderContext; if (_HiddenWindowDevice is WindowsDeviceContext) { renderContext = CreateWinSimpleContext(_HiddenWindowDevice); } else if (_HiddenWindowDevice is XServerDeviceContext) { renderContext = CreateX11SimpleContext(_HiddenWindowDevice); } else if (_HiddenWindowDevice is NativeDeviceContext) { renderContext = CreateEglSimpleContext(_HiddenWindowDevice); } else { throw new NotImplementedException(String.Format("{0} is not a supported device context", _HiddenWindowDevice.GetType())); } // Query OpenGL informations if (_HiddenWindowDevice.MakeCurrent(renderContext) == false) { throw new InvalidOperationException("unable to make current"); } // Obtain current OpenGL implementation string glVersion = Gl.GetString(StringName.Version); _CurrentVersion = KhronosVersion.Parse(glVersion); // Obtain current OpenGL Shading Language version string glslVersion = Gl.GetString(StringName.ShadingLanguageVersion); _CurrentShadingVersion = GlslVersion.Parse(glslVersion); // Query OpenGL extensions (current OpenGL implementation, CurrentCaps) _CurrentExtensions = new Extensions(); _CurrentExtensions.Query(); // Query OpenGL limits _CurrentLimits = Limits.Query(_CurrentExtensions); if (_HiddenWindowDevice is WindowsDeviceContext) { Wgl._CurrentExtensions = new Wgl.Extensions(); Wgl._CurrentExtensions.Query((WindowsDeviceContext)_HiddenWindowDevice); } else if (_HiddenWindowDevice is XServerDeviceContext) { Glx._CurrentExtensions = new Glx.Extensions(); Glx._CurrentExtensions.Query((XServerDeviceContext)_HiddenWindowDevice); } else if (_HiddenWindowDevice is NativeDeviceContext) { Egl._CurrentExtensions = new Egl.Extensions(); Egl._CurrentExtensions.Query((NativeDeviceContext)_HiddenWindowDevice); } // Before deletion, make uncurrent _HiddenWindowDevice.MakeCurrent(IntPtr.Zero); // Detroy context if (_HiddenWindowDevice.DeleteContext(renderContext) == false) { throw new InvalidOperationException("unable to delete OpenGL context"); } }