/// <summary> /// Initializes the RenderContext. /// </summary> public void Initialize() { _windowHandle = SGL.QueryComponents <RenderTarget>().Handle; _deviceContext = NativeMethods.GetDC(_windowHandle); var pfd = new PixelFormatDescriptor(); pfd.Size = (short)Marshal.SizeOf(pfd); pfd.Version = 1; pfd.FormatDescription = PixelFormatDescription.DrawToWindow | PixelFormatDescription.SupportOpenGL | PixelFormatDescription.DoubleBuffer; pfd.PixelType = 0; pfd.ColorBits = 24; pfd.RedBits = 0; pfd.RedShift = 0; pfd.GreenBits = 0; pfd.GreenShift = 0; pfd.BlueBits = 0; pfd.BlueShift = 0; pfd.AlphaBits = 0; pfd.AlphaShift = 0; pfd.AccumBits = 0; pfd.AccumRedBits = 0; pfd.AccumGreenBits = 0; pfd.AccumBlueBits = 0; pfd.AccumAlphaBits = 0; pfd.DepthBits = 24; pfd.StencilBits = 8; pfd.AuxBuffers = 0; pfd.LayerType = 0; pfd.Reserved = 0; pfd.LayerMask = 0; pfd.VisibleMask = 0; pfd.DamageMask = 0; int pixelFormat; if ((pixelFormat = OpenGLInterops.ChoosePixelFormat(_deviceContext, ref pfd)) == 0) { throw new GraphicsException("Unable to choose pixel format."); } if (!OpenGLInterops.SetPixelFormat(_deviceContext, pixelFormat, ref pfd)) { throw new GraphicsException("Unable to set pixel format."); } _renderContext = OpenGLInterops.wglCreateContext(_deviceContext); OpenGLInterops.wglMakeCurrent(_deviceContext, _renderContext); try { int[] attributes = { (int)ContextAttributes.MajorVersion, 3, (int)ContextAttributes.MinorVersion, 3, (int)ContextAttributes.Flags, (int)ContextAttributes.ForwardCompatible, 0 }; var hrc = OpenGLInterops.CreateContextWithAttributes(_deviceContext, IntPtr.Zero, attributes); OpenGLInterops.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); OpenGLInterops.wglDeleteContext(_renderContext); OpenGLInterops.wglMakeCurrent(_deviceContext, hrc); _renderContext = hrc; } catch (MissingMethodException) { throw new GraphicsException("Unable to create OpenGL 3.3 context."); } }
public static extern bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PixelFormatDescriptor pixelFormatDescriptor);
/// <summary> /// Initializes the RenderContext. /// </summary> public void Initialize() { _windowHandle = SGL.QueryComponents<RenderTarget>().Handle; _deviceContext = NativeMethods.GetDC(_windowHandle); var pfd = new PixelFormatDescriptor(); pfd.Size = (short)Marshal.SizeOf(pfd); pfd.Version = 1; pfd.FormatDescription = PixelFormatDescription.DrawToWindow | PixelFormatDescription.SupportOpenGL | PixelFormatDescription.DoubleBuffer; pfd.PixelType = 0; pfd.ColorBits = 24; pfd.RedBits = 0; pfd.RedShift = 0; pfd.GreenBits = 0; pfd.GreenShift = 0; pfd.BlueBits = 0; pfd.BlueShift = 0; pfd.AlphaBits = 0; pfd.AlphaShift = 0; pfd.AccumBits = 0; pfd.AccumRedBits = 0; pfd.AccumGreenBits = 0; pfd.AccumBlueBits = 0; pfd.AccumAlphaBits = 0; pfd.DepthBits = 24; pfd.StencilBits = 8; pfd.AuxBuffers = 0; pfd.LayerType = 0; pfd.Reserved = 0; pfd.LayerMask = 0; pfd.VisibleMask = 0; pfd.DamageMask = 0; int pixelFormat; if ((pixelFormat = OpenGLInterops.ChoosePixelFormat(_deviceContext, ref pfd)) == 0) { throw new GraphicsException("Unable to choose pixel format."); } if (!OpenGLInterops.SetPixelFormat(_deviceContext, pixelFormat, ref pfd)) { throw new GraphicsException("Unable to set pixel format."); } _renderContext = OpenGLInterops.wglCreateContext(_deviceContext); OpenGLInterops.wglMakeCurrent(_deviceContext, _renderContext); try { int[] attributes = { (int) ContextAttributes.MajorVersion, 3, (int) ContextAttributes.MinorVersion, 3, (int) ContextAttributes.Flags, (int) ContextAttributes.ForwardCompatible, 0 }; var hrc = OpenGLInterops.CreateContextWithAttributes(_deviceContext, IntPtr.Zero, attributes); OpenGLInterops.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); OpenGLInterops.wglDeleteContext(_renderContext); OpenGLInterops.wglMakeCurrent(_deviceContext, hrc); _renderContext = hrc; } catch (MissingMethodException) { throw new GraphicsException("Unable to create OpenGL 3.3 context."); } }
public static extern int ChoosePixelFormat(IntPtr deviceContext, ref PixelFormatDescriptor pixelFormatDescriptor);