!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Tools / Statoscope / Statoscope / AAOpenGLControl.cs
blobd639f6a9dfa58e2096548388d74c37938dd1b9bd
1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using CsGL.OpenGL;
8 using System.Runtime.InteropServices;
9 using System.Windows.Forms;
11 namespace Statoscope
13 public class AAOpenGLControl : Control
15 protected void MakeCurrent()
17 WGL.wglMakeCurrent(m_hDC, m_hRC);
20 protected override void OnCreateControl()
22 base.OnCreateControl();
24 int samples = 16;
25 int pixelFormat = GetAAPixelFormat(ref samples);
27 PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR();
28 pfd.nSize = (ushort)Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR));
29 pfd.nVersion = 1;
30 pfd.dwFlags = PixelFlag.PFD_DRAW_TO_WINDOW | PixelFlag.PFD_SUPPORT_OPENGL | PixelFlag.PFD_DOUBLEBUFFER;
31 pfd.iPixelType = PixelType.PFD_TYPE_RGBA;
32 pfd.cColorBits = 32;
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)
44 MakeCurrent();
46 glDraw();
47 OpenGL.glFinish();
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();
61 if (wc.cbSize == 0)
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)
70 return null;
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));
82 pfd.nVersion = 1;
83 pfd.dwFlags = PixelFlag.PFD_DRAW_TO_WINDOW | PixelFlag.PFD_SUPPORT_OPENGL | PixelFlag.PFD_DOUBLEBUFFER;
84 pfd.iPixelType = PixelType.PFD_TYPE_RGBA;
85 pfd.cColorBits = 32;
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,
106 0, 0
109 WGL.wglChoosePixelFormatARBHandler wglChoosePixelFormatARB = (WGL.wglChoosePixelFormatARBHandler)WGL.GetProc<WGL.wglChoosePixelFormatARBHandler>("wglChoosePixelFormatARB");
111 int pixelFormat = 0;
112 if (wglChoosePixelFormatARB != null)
114 int reqSamples;
115 for (reqSamples = samples; reqSamples > 0; reqSamples /= 2)
117 iattrs[17] = reqSamples;
119 int[] pixelFormats = new int[16];
120 int numFormats;
122 unsafe
124 fixed (int* pPixelFormats = pixelFormats)
126 fixed (int* pIAttrs = iattrs)
128 int success = wglChoosePixelFormatARB(
129 dummyHDc,
130 pIAttrs, (float*)0, 16,
131 pPixelFormats, &numFormats);
133 if (success != 0 && numFormats > 0)
135 pixelFormat = pixelFormats[0];
136 samples = reqSamples;
137 break;
144 else
146 samples = 1;
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);
155 return pixelFormat;
158 private IntPtr m_hDC;
159 private IntPtr m_hRC;