private void SetupWindowForRendering(IWindow window) { var dc = GetDC(window.Pointer); if (GetPixelFormat(dc) != 0) {//already set a valid pixel format return; } //For OpenGL, we need to set pixel format for each win32 window before make it current. var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); if (!Application.EnableMSAA) { int pixelFormat = ChoosePixelFormat(dc, ref pixelformatdescriptor); SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor); } else { int[] iPixAttribs = { (int) WGL.WGL_SUPPORT_OPENGL_ARB, (int) GL.GL_TRUE, (int) WGL.WGL_DRAW_TO_WINDOW_ARB, (int) GL.GL_TRUE, (int) WGL.WGL_DOUBLE_BUFFER_ARB, (int) GL.GL_TRUE, (int) WGL.WGL_PIXEL_TYPE_ARB, (int) WGL.WGL_TYPE_RGBA_ARB, (int) WGL.WGL_ACCELERATION_ARB, (int) WGL.WGL_FULL_ACCELERATION_ARB, (int) WGL.WGL_COLOR_BITS_ARB, 24, (int) WGL.WGL_ALPHA_BITS_ARB, 8, (int) WGL.WGL_DEPTH_BITS_ARB, 24, (int) WGL.WGL_STENCIL_BITS_ARB, 8, (int) WGL.WGL_SWAP_METHOD_ARB, (int) WGL.WGL_SWAP_EXCHANGE_ARB, (int) WGL.WGL_SAMPLE_BUFFERS_ARB, (int) GL.GL_TRUE, //Enable MSAA (int) WGL.WGL_SAMPLES_ARB, 16, 0 }; int pixelFormat; uint numFormats; var result = Wgl.ChoosePixelFormatARB(dc, iPixAttribs, null, 1, out pixelFormat, out numFormats); if (result == false || numFormats == 0) { throw new Exception( $"wglChoosePixelFormatARB failed: error {Marshal.GetLastWin32Error()}"); } if (!DescribePixelFormat(dc, pixelFormat, (uint) Marshal.SizeOf<PIXELFORMATDESCRIPTOR>(), ref pixelformatdescriptor)) { throw new Exception( $"DescribePixelFormat failed: error {Marshal.GetLastWin32Error()}"); } if (!SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor)) { throw new Exception( $"SetPixelFormat failed: error {Marshal.GetLastWin32Error()}"); } } }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="bitCount">The bitcount.</param> protected virtual bool SetPixelFormat(IntPtr hDC, int bitCount) { // Create the big lame pixel format majoo. PIXELFORMATDESCRIPTOR pixelFormat = new PIXELFORMATDESCRIPTOR(); pixelFormat.Init(); // Set the values for the pixel format. pixelFormat.nVersion = 1; pixelFormat.dwFlags = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI); pixelFormat.iPixelType = Win32.PFD_TYPE_RGBA; pixelFormat.cColorBits = (byte)bitCount; pixelFormat.cDepthBits = 32; pixelFormat.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = Win32.ChoosePixelFormat(hDC, pixelFormat)) == 0) { return(false); } // Sets the pixel format if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return(false); } return(true); }
/// <summary> /// openGL initialization (pixel format and wgl context) /// </summary> private void InitializeOpenGl() { deviceContext = GetDC(hWnd); PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.Init( 24, // color bits 24, // depth bits 8 // stencil bits ); var pixelFormat = ChoosePixelFormat(deviceContext, ref pfd); if (!SetPixelFormat(deviceContext, pixelFormat, ref pfd)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } var renderingContext = wglCreateContext(deviceContext); if (renderingContext == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (!wglMakeCurrent(deviceContext, renderingContext)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } // dll call: initialize glad etc. if (!initialize()) { throw new Exception(GetDllError()); } }
private void CreateOpenGLContext(IntPtr hwnd) { this.hwnd = hwnd; this.hDC = GetDC(hwnd); var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); #if !USE_MSAA int pixelFormat; pixelFormat = ChoosePixelFormat(this.hDC, ref pixelformatdescriptor); SetPixelFormat(this.hDC, pixelFormat, ref pixelformatdescriptor); this.hglrc = wglCreateContext(this.hDC); wglMakeCurrent(this.hDC, this.hglrc); #else IntPtr tempContext = IntPtr.Zero; IntPtr tempHwnd = IntPtr.Zero; //Create temporary window IntPtr hInstance = Process.GetCurrentProcess().SafeHandle.DangerousGetHandle(); WNDCLASS wndclass = new WNDCLASS() { style = 0x0002 /*CS_HREDRAW*/ | 0x0001 /*CS_VREDRAW*/ | 0x0020 /*CS_OWNDC*/, lpfnWndProc = (hWnd, msg, wParam, lParam) => DefWindowProc(hWnd, msg, wParam, lParam), hInstance = hInstance, lpszClassName = "tmpWindowForMSAA~" + this.GetHashCode() }; ushort atom = RegisterClassW(ref wndclass); if (atom == 0) { throw new WindowCreateException(string.Format("RegisterClass error: {0}", Marshal.GetLastWin32Error())); } tempHwnd = CreateWindowEx( 0, new IntPtr(atom), "tmpWindow~", (uint)(WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN), 0, 0, 1, 1, hwnd, IntPtr.Zero, hInstance, IntPtr.Zero); if (tempHwnd == IntPtr.Zero) { throw new WindowCreateException(string.Format("CreateWindowEx for tempContext error: {0}", Marshal.GetLastWin32Error())); } IntPtr tempHdc = GetDC(tempHwnd); var tempPixelFormat = ChoosePixelFormat(tempHdc, ref pixelformatdescriptor); if (tempPixelFormat == 0) { throw new Exception(string.Format("ChoosePixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } if (!SetPixelFormat(tempHdc, tempPixelFormat, ref pixelformatdescriptor)) { throw new Exception(string.Format("SetPixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } tempContext = Wgl.CreateContext(tempHdc);//Crate temp context to load entry points if (tempContext == IntPtr.Zero) { throw new Exception(string.Format("wglCreateContext for tempHdc failed: error {0}", Marshal.GetLastWin32Error())); } if (!Wgl.MakeCurrent(tempHdc, tempContext)) { throw new Exception(string.Format("wglMakeCurrent for tempContext failed: error {0}", Marshal.GetLastWin32Error())); } //load wgl entry points for wglChoosePixelFormatARB new Wgl().LoadEntryPoints(tempHdc); int[] iPixAttribs = { (int)WGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_PIXEL_TYPE_ARB, (int)WGL.WGL_TYPE_RGBA_ARB, (int)WGL.WGL_ACCELERATION_ARB, (int)WGL.WGL_FULL_ACCELERATION_ARB, (int)WGL.WGL_COLOR_BITS_ARB, 32, (int)WGL.WGL_DEPTH_BITS_ARB, 24, (int)WGL.WGL_STENCIL_BITS_ARB, 8, (int)WGL.WGL_SWAP_METHOD_ARB, (int)WGL.WGL_SWAP_EXCHANGE_ARB, (int)WGL.WGL_SAMPLE_BUFFERS_ARB, (int)GL.GL_TRUE,//Enable MSAA (int)WGL.WGL_SAMPLES_ARB, 16, 0 }; int pixelFormat; uint numFormats; var result = Wgl.ChoosePixelFormatARB(this.hDC, iPixAttribs, null, 1, out pixelFormat, out numFormats); if (result == false || numFormats == 0) { throw new Exception(string.Format("wglChoosePixelFormatARB failed: error {0}", Marshal.GetLastWin32Error())); } if (!DescribePixelFormat(this.hDC, pixelFormat, (uint)Marshal.SizeOf <PIXELFORMATDESCRIPTOR>(), ref pixelformatdescriptor)) { throw new Exception(string.Format("DescribePixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } if (!SetPixelFormat(this.hDC, pixelFormat, ref pixelformatdescriptor)) { throw new Exception(string.Format("SetPixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } if ((this.hglrc = wglCreateContext(this.hDC)) == IntPtr.Zero) { throw new Exception(string.Format("wglCreateContext failed: error {0}", Marshal.GetLastWin32Error())); } Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.DeleteContext(tempContext); ReleaseDC(tempHwnd, tempHdc); DestroyWindow(tempHwnd); if (!wglMakeCurrent(this.hDC, this.hglrc)) { throw new Exception(string.Format("wglMakeCurrent failed: error {0}", Marshal.GetLastWin32Error())); } Utility.CheckGLError(); #endif GL.GetIntegerv(GL.GL_STENCIL_BITS, IntBuffer); var stencilBits = IntBuffer[0]; if (stencilBits != 8) { throw new Exception("Failed to set stencilBits to 9."); } PrintGraphicInfo(); }
/// <summary> /// Creates the render context provider. Must also create the OpenGL extensions. /// </summary> /// <param name="openGLVersion">The desired OpenGL version.</param> /// <param name="gl">The OpenGL context.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitDepth">The bit depth.</param> /// <param name="parameter">The parameter</param> /// <returns></returns> public override bool Create(GLVersion openGLVersion, int width, int height, int bitDepth, object parameter) { // Call the base. base.Create(openGLVersion, width, height, bitDepth, parameter); // Create a new window class, as basic as possible. WNDCLASSEX wndClass = new WNDCLASSEX(); wndClass.Init(); wndClass.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw | ClassStyles.OwnDC; wndClass.lpfnWndProc = wndProcDelegate; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = IntPtr.Zero; wndClass.hIcon = IntPtr.Zero; wndClass.hCursor = IntPtr.Zero; wndClass.hbrBackground = IntPtr.Zero; wndClass.lpszMenuName = null; wndClass.lpszClassName = "SharpGLRenderWindow"; wndClass.hIconSm = IntPtr.Zero; Win32.RegisterClassEx(ref wndClass); // Create the window. Position and size it. windowHandle = Win32.CreateWindowEx(0, "SharpGLRenderWindow", "", WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_POPUP, 0, 0, width, height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); // Get the window device context. DeviceContextHandle = Win32.GetDC(windowHandle); // Setup a pixel format. PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.Init(); pfd.nVersion = 1; pfd.dwFlags = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER; pfd.iPixelType = Win32.PFD_TYPE_RGBA; pfd.cColorBits = (byte)bitDepth; pfd.cDepthBits = 16; pfd.cStencilBits = 8; pfd.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = Win32.ChoosePixelFormat(DeviceContextHandle, pfd)) == 0) { return(false); } // Sets the pixel format if (Win32.SetPixelFormat(DeviceContextHandle, iPixelformat, pfd) == 0) { return(false); } // Create the render context. RenderContextHandle = Win32.wglCreateContext(DeviceContextHandle); // Make the context current. MakeCurrent(); // Update the context if required. UpdateContextVersion(); // Return success. return(true); }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="bitCount">The bitcount.</param> protected virtual bool SetPixelFormat(IntPtr hDC, int bitCount) { // Create the big lame pixel format majoo. PIXELFORMATDESCRIPTOR pixelFormat = new PIXELFORMATDESCRIPTOR(); pixelFormat.Init(); // Set the values for the pixel format. pixelFormat.nVersion = 1; pixelFormat.dwFlags = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI); pixelFormat.iPixelType = Win32.PFD_TYPE_RGBA; pixelFormat.cColorBits = (byte)bitCount; pixelFormat.cDepthBits = 32; pixelFormat.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = Win32.ChoosePixelFormat(hDC, pixelFormat)) == 0) return false; // Sets the pixel format if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return false; } return true; }
private void CreateOpenGLContext(IntPtr hwnd) { this.hwnd = hwnd; this.hDC = GetDC(hwnd); var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); if (!Application.EnableMSAA) { int pixelFormat; pixelFormat = ChoosePixelFormat(this.hDC, ref pixelformatdescriptor); SetPixelFormat(this.hDC, pixelFormat, ref pixelformatdescriptor); this.hglrc = wglCreateContext(this.hDC); wglMakeCurrent(this.hDC, this.hglrc); } else { Wgl.Init(hwnd); int[] iPixAttribs = { (int)WGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_PIXEL_TYPE_ARB, (int)WGL.WGL_TYPE_RGBA_ARB, (int)WGL.WGL_ACCELERATION_ARB, (int)WGL.WGL_FULL_ACCELERATION_ARB, (int)WGL.WGL_COLOR_BITS_ARB, 24, (int)WGL.WGL_ALPHA_BITS_ARB, 8, (int)WGL.WGL_DEPTH_BITS_ARB, 24, (int)WGL.WGL_STENCIL_BITS_ARB, 8, (int)WGL.WGL_SWAP_METHOD_ARB, (int)WGL.WGL_SWAP_EXCHANGE_ARB, (int)WGL.WGL_SAMPLE_BUFFERS_ARB, (int)GL.GL_TRUE,//Enable MSAA (int)WGL.WGL_SAMPLES_ARB, 16, 0 }; int pixelFormat; uint numFormats; var result = Wgl.ChoosePixelFormatARB(this.hDC, iPixAttribs, null, 1, out pixelFormat, out numFormats); if (result == false || numFormats == 0) { throw new Exception(string.Format("wglChoosePixelFormatARB failed: error {0}", Marshal.GetLastWin32Error())); } if (!DescribePixelFormat(this.hDC, pixelFormat, (uint)Marshal.SizeOf <PIXELFORMATDESCRIPTOR>(), ref pixelformatdescriptor)) { throw new Exception(string.Format("DescribePixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } if (!SetPixelFormat(this.hDC, pixelFormat, ref pixelformatdescriptor)) { throw new Exception(string.Format("SetPixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } int[] attributes = { (int)WGL.WGL_CONTEXT_MAJOR_VERSION_ARB, 4, (int)WGL.WGL_CONTEXT_MINOR_VERSION_ARB, 5, (int)WGL.WGL_CONTEXT_FLAGS_ARB, 0, (int)WGL.WGL_CONTEXT_PROFILE_MASK_ARB, (int)WGL.WGL_CONTEXT_CORE_PROFILE_BIT_ARB , 0 }; if ((this.hglrc = Wgl.CreateContextAttribsARB(this.hDC, IntPtr.Zero, attributes)) == IntPtr.Zero) { throw new Exception(string.Format("CreateContextAttribsARB failed: error {0}", Marshal.GetLastWin32Error())); } if (!Wgl.MakeCurrent(this.hDC, this.hglrc)) { throw new Exception(string.Format("Wgl.MakeCurrent failed: error {0}", Marshal.GetLastWin32Error())); } Utility.CheckGLError(); } int[] IntBuffer = { 0, 0, 0, 0 }; GL.GetFramebufferAttachmentParameteriv(GL.GL_DRAW_FRAMEBUFFER, GL.GL_STENCIL, GL.GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE, IntBuffer); Utility.CheckGLError(); var stencilBits = IntBuffer[0]; if (stencilBits != 8) { throw new Exception("Failed to set stencilBits to 8."); } PrintPixelFormat(hDC); PrintGraphicInfo(); }
/// <summary> /// Initialize WGL functions /// </summary> /// <param name="mainWindowHwnd"></param> /// <remarks> /// //refer to /// https://github.com/glfw/glfw/blob/3327050ca66ad34426a82c217c2d60ced61526b7/src/wgl_context.c#L408 /// </remarks> internal static void Init(IntPtr mainWindowHwnd) { if (EntryPoints != null)//already initialized { return; } IntPtr tempContext = IntPtr.Zero; IntPtr tempHwnd = IntPtr.Zero; //Create temporary window IntPtr hInstance = Process.GetCurrentProcess().SafeHandle.DangerousGetHandle(); WNDCLASS wndclass = new WNDCLASS() { style = 0x0002 /*CS_HREDRAW*/ | 0x0001 /*CS_VREDRAW*/ | 0x0020 /*CS_OWNDC*/, lpfnWndProc = (hWnd, msg, wParam, lParam) => DefWindowProc(hWnd, msg, wParam, lParam), hInstance = hInstance, lpszClassName = "tmpWindowForMSAA~" + tempHwnd.GetHashCode() }; ushort atom = RegisterClassW(ref wndclass); if (atom == 0) { throw new WindowCreateException(string.Format("RegisterClass error: {0}", Marshal.GetLastWin32Error())); } //TODO wgl entry point only need to be loaded once! tempHwnd = CreateWindowEx( 0, new IntPtr(atom), "tmpWindow~", (uint)(WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN), 0, 0, 1, 1, mainWindowHwnd, IntPtr.Zero, hInstance, IntPtr.Zero); if (tempHwnd == IntPtr.Zero) { throw new WindowCreateException(string.Format("CreateWindowEx for tempContext error: {0}", Marshal.GetLastWin32Error())); } IntPtr tempHdc = GetDC(tempHwnd); var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); var tempPixelFormat = ChoosePixelFormat(tempHdc, ref pixelformatdescriptor); if (tempPixelFormat == 0) { throw new Exception(string.Format("ChoosePixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } if (!SetPixelFormat(tempHdc, tempPixelFormat, ref pixelformatdescriptor)) { throw new Exception(string.Format("SetPixelFormat failed: error {0}", Marshal.GetLastWin32Error())); } tempContext = Wgl.CreateContext(tempHdc);//Crate temp context to load entry points if (tempContext == IntPtr.Zero) { throw new Exception(string.Format("wglCreateContext for tempHdc failed: error {0}", Marshal.GetLastWin32Error())); } if (!Wgl.MakeCurrent(tempHdc, tempContext)) { throw new Exception(string.Format("wglMakeCurrent for tempContext failed: error {0}", Marshal.GetLastWin32Error())); } //load wgl entry points for wglChoosePixelFormatARB new Wgl().LoadEntryPoints(tempHdc); Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); Wgl.DeleteContext(tempContext); ReleaseDC(tempHwnd, tempHdc); DestroyWindow(tempHwnd); }
/// <summary> /// Creates the render context provider. Must also create the OpenGL extensions. /// </summary> /// <param name="openGLVersion">The desired OpenGL version.</param> /// <param name="gl">The OpenGL context.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitDepth">The bit depth.</param> /// <param name="parameter">The parameter</param> /// <returns></returns> public override bool Create(GLVersion openGLVersion, int width, int height, int bitDepth, object parameter) { // Call the base. base.Create(openGLVersion, width, height, bitDepth, parameter); // Create a new window class, as basic as possible. WNDCLASSEX wndClass = new WNDCLASSEX(); wndClass.Init(); wndClass.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw | ClassStyles.OwnDC; wndClass.lpfnWndProc = wndProcDelegate; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = IntPtr.Zero; wndClass.hIcon = IntPtr.Zero; wndClass.hCursor = IntPtr.Zero; wndClass.hbrBackground = IntPtr.Zero; wndClass.lpszMenuName = null; wndClass.lpszClassName = "SharpGLRenderWindow"; wndClass.hIconSm = IntPtr.Zero; Win32.RegisterClassEx(ref wndClass); // Create the window. Position and size it. windowHandle = Win32.CreateWindowEx(0, "SharpGLRenderWindow", "", WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_POPUP, 0, 0, width, height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); // Get the window device context. DeviceContextHandle = Win32.GetDC(windowHandle); // Setup a pixel format. PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.Init(); pfd.nVersion = 1; pfd.dwFlags = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER; pfd.iPixelType = Win32.PFD_TYPE_RGBA; pfd.cColorBits = (byte)bitDepth; pfd.cDepthBits = 16; pfd.cStencilBits = 8; pfd.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = Win32.ChoosePixelFormat(DeviceContextHandle, pfd)) == 0) return false; // Sets the pixel format if (Win32.SetPixelFormat(DeviceContextHandle, iPixelformat, pfd) == 0) { return false; } // Create the render context. RenderContextHandle = Win32.wglCreateContext(DeviceContextHandle); // Make the context current. MakeCurrent(); // Update the context if required. UpdateContextVersion(); // Return success. return true; }
public void SetRenderingWindow(IWindow window) { if (hglrc == IntPtr.Zero) { throw new InvalidOperationException("OpenGL context hasn't been created."); } var dc = GetDC(window.Pointer); hDC = dc; //For OpenGL, we need to set pixel format for each win32 window before make it current. if (GetPixelFormat(dc) == 0)//Set a valid pixel format if haven't. { var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); if (!Application.EnableMSAA) { int pixelFormat = ChoosePixelFormat(dc, ref pixelformatdescriptor); SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor); } else { int[] iPixAttribs = { (int)WGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE, (int)WGL.WGL_PIXEL_TYPE_ARB, (int)WGL.WGL_TYPE_RGBA_ARB, (int)WGL.WGL_ACCELERATION_ARB, (int)WGL.WGL_FULL_ACCELERATION_ARB, (int)WGL.WGL_COLOR_BITS_ARB, 24, (int)WGL.WGL_ALPHA_BITS_ARB, 8, (int)WGL.WGL_DEPTH_BITS_ARB, 24, (int)WGL.WGL_STENCIL_BITS_ARB, 8, (int)WGL.WGL_SWAP_METHOD_ARB, (int)WGL.WGL_SWAP_EXCHANGE_ARB, (int)WGL.WGL_SAMPLE_BUFFERS_ARB, (int)GL.GL_TRUE, //Enable MSAA (int)WGL.WGL_SAMPLES_ARB, 16, 0 }; int pixelFormat; uint numFormats; var result1 = Wgl.ChoosePixelFormatARB(dc, iPixAttribs, null, 1, out pixelFormat, out numFormats); if (result1 == false || numFormats == 0) { throw new Exception( $"wglChoosePixelFormatARB failed: error {Marshal.GetLastWin32Error()}"); } if (!DescribePixelFormat(dc, pixelFormat, (uint)Marshal.SizeOf <PIXELFORMATDESCRIPTOR>(), ref pixelformatdescriptor)) { throw new Exception( $"DescribePixelFormat failed: error {Marshal.GetLastWin32Error()}"); } if (!SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor)) { throw new Exception( $"SetPixelFormat failed: error {Marshal.GetLastWin32Error()}"); } } } var result = Wgl.MakeCurrent(dc, hglrc); if (!result) { var lastError = Native.GetLastErrorString(); PrintPixelFormat(dc); throw new InvalidOperationException($"Wgl.MakeCurrent failed, error: {lastError}"); } var viewportSize = window.ClientSize; GL.Viewport(0, 0, (int)viewportSize.Width, (int)viewportSize.Height); GL.Scissor(0, 0, (int)viewportSize.Width, (int)viewportSize.Height); }