private static void GLInit(IntPtr hwnd, out IntPtr hdc, out IntPtr hglrc) { gdi.PIXELFORMATDESCRIPTOR pfd = new gdi.PIXELFORMATDESCRIPTOR(); pfd.nSize = (ushort)Marshal.SizeOf(pfd); pfd.nVersion = 1; pfd.dwFlags = gdi.PFD_DRAW_TO_WINDOW | gdi.PFD_SUPPORT_OPENGL | gdi.PFD_DOUBLEBUFFER; pfd.iPixelType = gdi.PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cRedBits = 0; pfd.cRedShift = 0; pfd.cGreenBits = 0; pfd.cGreenShift = 0; pfd.cBlueBits = 0; pfd.cBlueShift = 0; pfd.cAlphaBits = 0; pfd.cAlphaShift = 0; pfd.cAccumBits = 0; pfd.cAccumRedBits = 0; pfd.cAccumGreenBits = 0; pfd.cAccumBlueBits = 0; pfd.cAccumAlphaBits = 0; pfd.cDepthBits = 24; pfd.cStencilBits = 8; pfd.cAuxBuffers = 0; pfd.iLayerType = 0; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; hdc = gdi.GetDC(hwnd); if (hdc == IntPtr.Zero) { throw new Exception("ThreadGLSurface error: Failed to get device context."); } int iPixelFormat = gdi.ChoosePixelFormat(hdc, out pfd); if (!gdi.SetPixelFormat(hdc, iPixelFormat, out pfd)) { throw new Exception("ThreadGLSurface error: Failed to set pixel format."); } hglrc = wgl.CreateContext(hdc); if (hglrc == IntPtr.Zero) { throw new Exception("ThreadGLSurface error: Failed to create OpenGL device context."); } if (!wgl.MakeCurrent(hdc, hglrc)) { throw new Exception("ThreadGLSurface error: Failed to make device context current."); } }
protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); gdi.PIXELFORMATDESCRIPTOR pfd = new gdi.PIXELFORMATDESCRIPTOR(); pfd.nSize = (ushort)Marshal.SizeOf(pfd); pfd.nVersion = 1; pfd.dwFlags = gdi.PFD_DRAW_TO_WINDOW | gdi.PFD_SUPPORT_OPENGL | gdi.PFD_DOUBLEBUFFER; pfd.iPixelType = gdi.PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cRedBits = 0; pfd.cRedShift = 0; pfd.cGreenBits = 0; pfd.cGreenShift = 0; pfd.cBlueBits = 0; pfd.cBlueShift = 0; pfd.cAlphaBits = 0; pfd.cAlphaShift = 0; pfd.cAccumBits = 0; pfd.cAccumRedBits = 0; pfd.cAccumGreenBits = 0; pfd.cAccumBlueBits = 0; pfd.cAccumAlphaBits = 0; pfd.cDepthBits = 24; pfd.cStencilBits = 8; pfd.cAuxBuffers = 0; pfd.iLayerType = 0; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; HDC = gdi.GetDC(Handle); if (HDC == IntPtr.Zero) { throw new Exception("OpenGLSurface error: Failed to get device context."); } int iPixelFormat = gdi.ChoosePixelFormat(HDC, out pfd); if (!gdi.SetPixelFormat(HDC, iPixelFormat, out pfd)) { throw new Exception("OpenGLSurface error: Failed to set pixel format."); } HGLRC = wgl.CreateContext(HDC); if (HGLRC == IntPtr.Zero) { throw new Exception("OpenGLSurface error: Failed to create OpenGL device context."); } if (!MakeCurrent()) { throw new Exception("OpenGLSurface error: Failed to make device context current."); } gl.Enable(GL.BLEND); gl.BlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA); gl.DepthMask(GL.FALSE); gl.MatrixMode(GL.PROJECTION); gl.LoadIdentity(); gl.Ortho(-SurfaceSize.Width / 2f, SurfaceSize.Width / 2f, -SurfaceSize.Height / 2f, SurfaceSize.Height / 2f, 0f, 1f); GLStart?.Invoke(this, EventArgs.Empty); }