1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
4 using System
.Collections
.Generic
;
8 using System
.Runtime
.InteropServices
;
9 using System
.Windows
.Forms
;
13 public class AAOpenGLControl
: Control
15 protected void MakeCurrent()
17 WGL
.wglMakeCurrent(m_hDC
, m_hRC
);
20 protected override void OnCreateControl()
22 base.OnCreateControl();
25 int pixelFormat
= GetAAPixelFormat(ref samples
);
27 PIXELFORMATDESCRIPTOR pfd
= new PIXELFORMATDESCRIPTOR();
28 pfd
.nSize
= (ushort)Marshal
.SizeOf(typeof(PIXELFORMATDESCRIPTOR
));
30 pfd
.dwFlags
= PixelFlag
.PFD_DRAW_TO_WINDOW
| PixelFlag
.PFD_SUPPORT_OPENGL
| PixelFlag
.PFD_DOUBLEBUFFER
;
31 pfd
.iPixelType
= PixelType
.PFD_TYPE_RGBA
;
34 m_hDC
= GDI
.GetDC(Handle
);
36 GDI
.SetPixelFormat(m_hDC
, pixelFormat
, ref pfd
);
37 m_hRC
= WGL
.wglCreateContext(m_hDC
);
39 WGL
.wglMakeCurrent(m_hDC
, m_hRC
);
42 protected override void OnPaint(PaintEventArgs e
)
48 GDI
.SwapBuffers(m_hDC
);
51 protected virtual void glDraw()
55 private static GDI
.WndProc s_defWindowProc
= GDI
.DefWindowProc
;
56 private static string s_dummyClass
= GetDummyClass();
58 static private string GetDummyClass()
60 GDI
.WNDCLASSEX wc
= new GDI
.WNDCLASSEX();
63 wc
.cbSize
= (ushort)Marshal
.SizeOf(typeof(GDI
.WNDCLASSEX
));
64 wc
.style
= GDI
.ClassStyles
.OwnDC
;
65 wc
.lpfnWndProc
= s_defWindowProc
;
66 wc
.hInstance
= GDI
.GetModuleHandle(null);
67 wc
.lpszClassName
= "DummyGLWindowClass";
69 if (GDI
.RegisterClassEx(ref wc
) == 0)
72 return wc
.lpszClassName
;
75 return wc
.lpszClassName
;
78 static private int GetAAPixelFormat(ref int samples
)
80 PIXELFORMATDESCRIPTOR pfd
= new PIXELFORMATDESCRIPTOR();
81 pfd
.nSize
= (ushort)Marshal
.SizeOf(typeof(PIXELFORMATDESCRIPTOR
));
83 pfd
.dwFlags
= PixelFlag
.PFD_DRAW_TO_WINDOW
| PixelFlag
.PFD_SUPPORT_OPENGL
| PixelFlag
.PFD_DOUBLEBUFFER
;
84 pfd
.iPixelType
= PixelType
.PFD_TYPE_RGBA
;
87 IntPtr dummyHWnd
= GDI
.CreateWindowEx(0, s_dummyClass
, "", GDI
.WindowStyles
.WS_OVERLAPPEDWINDOW
, 0, 0, 0, 0, IntPtr
.Zero
, IntPtr
.Zero
, IntPtr
.Zero
, IntPtr
.Zero
);
88 IntPtr dummyHDc
= GDI
.GetDC(dummyHWnd
);
90 int dummyPF
= GDI
.ChoosePixelFormat(dummyHDc
, ref pfd
);
91 GDI
.SetPixelFormat(dummyHDc
, dummyPF
, ref pfd
);
93 IntPtr dummyHRc
= WGL
.wglCreateContext(dummyHDc
);
94 WGL
.wglMakeCurrent(dummyHDc
, dummyHRc
);
96 int[] iattrs
= new int[] {
97 WGL
.WGL_DRAW_TO_WINDOW_ARB
, 1,
98 WGL
.WGL_ACCELERATION_ARB
, WGL
.WGL_FULL_ACCELERATION_ARB
,
99 WGL
.WGL_COLOR_BITS_ARB
, 24,
100 WGL
.WGL_ALPHA_BITS_ARB
, 8,
101 WGL
.WGL_DEPTH_BITS_ARB
, 24,
102 WGL
.WGL_STENCIL_BITS_ARB
, 8,
103 WGL
.WGL_DOUBLE_BUFFER_ARB
, 1,
104 WGL
.WGL_SAMPLE_BUFFERS_ARB
, 1,
105 WGL
.WGL_SAMPLES_ARB
, 0,
109 WGL
.wglChoosePixelFormatARBHandler wglChoosePixelFormatARB
= (WGL
.wglChoosePixelFormatARBHandler
)WGL
.GetProc
<WGL
.wglChoosePixelFormatARBHandler
>("wglChoosePixelFormatARB");
112 if (wglChoosePixelFormatARB
!= null)
115 for (reqSamples
= samples
; reqSamples
> 0; reqSamples
/= 2)
117 iattrs
[17] = reqSamples
;
119 int[] pixelFormats
= new int[16];
124 fixed (int* pPixelFormats
= pixelFormats
)
126 fixed (int* pIAttrs
= iattrs
)
128 int success
= wglChoosePixelFormatARB(
130 pIAttrs
, (float*)0, 16,
131 pPixelFormats
, &numFormats
);
133 if (success
!= 0 && numFormats
> 0)
135 pixelFormat
= pixelFormats
[0];
136 samples
= reqSamples
;
147 pixelFormat
= GDI
.ChoosePixelFormat(dummyHDc
, ref pfd
);
150 WGL
.wglMakeCurrent(dummyHDc
, dummyHRc
);
151 WGL
.wglDeleteContext(dummyHRc
);
152 GDI
.ReleaseDC(dummyHWnd
, dummyHDc
);
153 GDI
.DestroyWindow(dummyHWnd
);
158 private IntPtr m_hDC
;
159 private IntPtr m_hRC
;