public static void ZeroPixelDescriptor(ref PIXELFORMATDESCRIPTOR pfd) { pfd.nSize = 40; // sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 0; pfd.dwFlags = 0; pfd.iPixelType = 0; pfd.cColorBits = 0; 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 = 0; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = 0; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; }
/// <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); }
public static void ClearPixelDescriptor(ref PIXELFORMATDESCRIPTOR pfd) { pfd.nSize = (ushort)Marshal.SizeOf(pfd); // 40 bytes total pfd.nVersion = 0; pfd.dwFlags = 0; pfd.iPixelType = 0; pfd.cColorBits = 0; 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 = 0; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = 0; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; }
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()}"); } } }
public static void ZeroPixelDescriptor(ref PIXELFORMATDESCRIPTOR pfd) { pfd.nSize = 40; // sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 0; pfd.dwFlags = 0; pfd.iPixelType = 0; pfd.cColorBits = 0; 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 = 0; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = 0; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; }
public OpenGLContext(IntPtr hwnd, Size size) { p_Handle = hwnd; //do not allow hwnd of zero (it get's the screen) if (hwnd == IntPtr.Zero) { throw new Exception("OpenGL: Invalid window handle!"); } p_Width = size.Width; p_Height = size.Height; //get the device context p_DeviceContext = GetDC(p_Handle); //create the pixel format for the rendering context PIXELFORMATDESCRIPTOR format = new PIXELFORMATDESCRIPTOR() { nSize = 40, /*size of struct in memory*/ nVersion = 1, dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, iPixelType = PFD_TYPE_RGBA, cColorBits = 24, /*24 bits per pixel*/ cDepthBits = 32, iLayerType = PFD_MAIN_PLANE }; //attempt to register the pixel format int match = ChoosePixelFormat(p_DeviceContext, format); if (match == 0) { throw new Exception("OpenGL: Unable to discover a compatable pixel format. [" + GetLastError() + "]"); } if (SetPixelFormat(p_DeviceContext, match, format) == 0) { throw new Exception("OpenGL: Unable to set pixel format. [" + GetLastError() + "]"); } //create the opengl rendering context p_RenderContext = wglCreateContext(p_DeviceContext); //discover opengl version wglMakeCurrent(p_DeviceContext, p_RenderContext); int major, minor; glGetIntegerv(MAJOR_VERSION, out major); glGetIntegerv(MINOR_VERSION, out minor); p_Version = new Version(major, minor); //enable transparency glEnable(BLEND); glBlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA); //switch out of the opengl render so the //rendering thread can own it. wglMakeCurrent(p_DeviceContext, IntPtr.Zero); }
/// <summary> /// /// </summary> /// <param name="deviceContext"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static int ChoosePixelFormat(IntPtr deviceContext, [In] ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { int retValue = UnsafeNativeMethods.ChoosePixelFormat(deviceContext, ref pixelFormatDescriptor); LogFunction("ChoosePixelFormat(0x{0}, {1}) = {2}", deviceContext.ToString("X8"), pixelFormatDescriptor, retValue); DebugCheckErrors(null); return(retValue); }
/// <summary> /// /// </summary> /// <param name="deviceContext"></param> /// <param name="pixelFormat"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { bool retValue = UnsafeNativeMethods.SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor); LogFunction("SetPixelFormat(0x{0}, {1}, {2}) = {3}", deviceContext.ToString("X8"), pixelFormat, pixelFormatDescriptor, retValue); DebugCheckErrors(null); return(retValue); }
/// <summary> /// /// </summary> /// <param name="deviceContext"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static int ChoosePixelFormat(IntPtr deviceContext, [In] ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { int retValue = UnsafeNativeMethods.ChoosePixelFormat(deviceContext, ref pixelFormatDescriptor); LogCommand("ChoosePixelFormat", retValue, deviceContext, pixelFormatDescriptor); DebugCheckErrors(null); return(retValue); }
/// <summary> /// /// </summary> /// <param name="deviceContext"></param> /// <param name="pixelFormat"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { bool retValue = UnsafeNativeMethods.SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor); LogCommand("SetPixelFormat", retValue, deviceContext, pixelFormat, pixelFormatDescriptor); DebugCheckErrors(null); return(retValue); }
/// <summary> /// Create an OpenGL context from the procided Windows Drawing Context /// </summary> /// <param name="HDC">Handle to the windows Drawing Context</param> static public Context CreateContext(IntPtr HDC, PIXELFORMATDESCRIPTOR PixelFormatDescriptor) { PIXELFORMATDESCRIPTOR selectedPixelFormatDescriptor; try { // Always pick the format with HW acceleration first PixelFormatDescriptor.dwFlags |= PIXELFORMATDESCRIPTOR.PFD_GENERIC_ACCELERATED; SetPixelFormat(HDC, PixelFormatDescriptor); int format = GetPixelFormat(HDC); if (format == 0) { throw new Exception("Invalid pixel format"); } if (0 == DescribePixelFormat(HDC, format, (uint)sizeof(PIXELFORMATDESCRIPTOR), &selectedPixelFormatDescriptor)) { throw new Exception("Invalid pixel format"); } if ((selectedPixelFormatDescriptor.dwFlags & PIXELFORMATDESCRIPTOR.PFD_SUPPORT_OPENGL) == 0) { throw new Exception("The pixel format specified has no OpenGL support"); } } catch { PixelFormatDescriptor.dwFlags &= (~PIXELFORMATDESCRIPTOR.PFD_GENERIC_ACCELERATED); SetPixelFormat(HDC, PixelFormatDescriptor); int format = GetPixelFormat(HDC); if (format == 0) { throw new Exception("Invalid pixel format"); } if (0 == DescribePixelFormat(HDC, format, (uint)sizeof(PIXELFORMATDESCRIPTOR), &selectedPixelFormatDescriptor)) { throw new Exception("Invalid pixel format"); } if ((selectedPixelFormatDescriptor.dwFlags & PIXELFORMATDESCRIPTOR.PFD_SUPPORT_OPENGL) == 0) { throw new Exception("The pixel format specified has no OpenGL support"); } } IntPtr HWGLC = wglCreateContext(HDC); if (HWGLC.ToInt64() == 0 || HWGLC.ToInt64() < 0) { int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); switch (error) { case 0x7D0: throw new Exception("Invalid pixel format"); } throw new Exception("Context creation failed with error 0x" + error.ToString("X")); } return(new Context(HDC, HWGLC)); }
static void Main() { bool exit = false; MSG msg; WNDCLASSEX win = new WNDCLASSEX(); win.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)); win.style = (int)(1 | 2); win.hbrBackground = (IntPtr)1 + 1; win.cbClsExtra = 0; win.cbWndExtra = 0; win.hInstance = System.Diagnostics.Process.GetCurrentProcess().Handle; win.hIcon = IntPtr.Zero; win.hCursor = LoadCursor(IntPtr.Zero, (int)32515); win.lpszMenuName = null; win.lpszClassName = "Demo"; win.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(WindowProcPointer); win.hIconSm = IntPtr.Zero; IntPtr hwnd = CreateWindowEx(0, RegisterClassEx(ref win), "Demo", 0xcf0000 | 0x10000000, 0, 0, 800, 480, IntPtr.Zero, IntPtr.Zero, win.hInstance, IntPtr.Zero); IntPtr hdc = GetDC(hwnd); PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.dwFlags = 0x00000001u; SetPixelFormat(hdc, ChoosePixelFormat(hdc, ref pfd), ref pfd); wglMakeCurrent(hdc, wglCreateContext(hdc)); Console.WriteLine("Graphics Processing Unit: " + Marshal.PtrToStringAnsi(glGetString(0x1F01))); glInit(); wglSwapIntervalEXT(0); uint p = glCreateProgram(); uint s = glCreateShader(0x8B30); glShaderSource(s, 1, new[] { FragmentShader }, new[] { FragmentShader.Length }); glCompileShader(s); glAttachShader(p, s); glLinkProgram(p); glUseProgram(p); int time = glGetUniformLocation(p, "iTime"); float start = Environment.TickCount * 0.001f; while (!exit) { while (PeekMessage(out msg, 0, 0, 0, 0x0001)) { if (msg.message == 0x0012) { exit = true; } TranslateMessage(ref msg); DispatchMessage(ref msg); } glUniform1f(time, (Environment.TickCount * 0.001f) - start); glRects(-1, -1, 1, 1); wglSwapLayerBuffers(hdc, 0x00000001); } DestroyWindow(hwnd); }
/////////////////////////////////////////////////////////////////////// // OPENGL WGL WRAPPERS /////////////////////////////////////////////////////////////////////// #region OPENGL WGL WRAPPERS... // int WINAPI wglChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR *ppfd); public static int ChoosePixelFormat(IntPtr hDC, ref PIXELFORMATDESCRIPTOR ppfd) { if (wglChoosePixelFormat != null) { return(wglChoosePixelFormat(hDC, ref ppfd)); } else { return(0); } }
/// <summary> /// Create a WGL Context for the specified Window. It is up to the caller to /// have configured the window correctly for this (see: Windows.Form.CreateParams) /// and to pass a valid handle to it (see: Form.Handle) /// </summary> /// <param name="HWND">The Window handle that will be used to create the context</param> /// <param name="PixelFormatDescriptor"></param> /// <returns></returns> static public Context CreateContextFromWindowHandle(IntPtr HWND, PIXELFORMATDESCRIPTOR PixelFormatDescriptor) { PixelFormatDescriptor.dwFlags |= PIXELFORMATDESCRIPTOR.PFD_DRAW_TO_WINDOW; if (HWND.ToInt64() == 0) { throw new Exception("Invalid from handle"); } IntPtr HDC = GetDC(HWND); return(CreateContext(HDC, PixelFormatDescriptor)); }
// BOOL WINAPI wglSetPixelFormat( HDC hdc, int iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd); public static bool SetPixelFormat(IntPtr hdc, int iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd) { if (wglSetPixelFormat != null) { return(wglSetPixelFormat(hdc, iPixelFormat, ref ppfd)); } else { return(false); } }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="bitCount">The bitcount.</param> protected virtual bool SetPixelFormat(uint bitCount) { // Create the big lame pixel format majoo. PIXELFORMATDESCRIPTOR pixelFormat = new PIXELFORMATDESCRIPTOR(); // Set the values for the pixel format. pixelFormat.nSize = 40; pixelFormat.nVersion = 1; pixelFormat.dwFlags = (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI); pixelFormat.iPixelType = PFD_TYPE_RGBA; pixelFormat.cColorBits = (byte)bitCount; pixelFormat.cRedBits = 0; pixelFormat.cRedShift = 0; pixelFormat.cGreenBits = 0; pixelFormat.cGreenShift = 0; pixelFormat.cBlueBits = 0; pixelFormat.cBlueShift = 0; pixelFormat.cAlphaBits = 0; pixelFormat.cAlphaShift = 0; pixelFormat.cAccumBits = 0; pixelFormat.cAccumRedBits = 0; pixelFormat.cAccumGreenBits = 0; pixelFormat.cAccumBlueBits = 0; pixelFormat.cAccumAlphaBits = 0; pixelFormat.cDepthBits = 32; pixelFormat.cStencilBits = 0; pixelFormat.cAuxBuffers = 0; pixelFormat.iLayerType = PFD_MAIN_PLANE; pixelFormat.bReserved = 0; pixelFormat.dwLayerMask = 0; pixelFormat.dwVisibleMask = 0; pixelFormat.dwDamageMask = 0; // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = ChoosePixelFormat(hDC, pixelFormat)) == 0) { return(false); } // Sets the pixel format if (SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return(false); } return(true); }
public GLForm() { var g = CreateGraphics(); var dc = g.GetHdc(); var pfd = new PIXELFORMATDESCRIPTOR { dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER }; var pf = ChoosePixelFormat(dc, ref pfd); SetPixelFormat(dc, pf, ref pfd); var context = wglCreateContext(dc); wglMakeCurrent(dc, context); g.ReleaseHdc(dc); GetGLFuncs(); Show(); }
private static PIXELFORMATDESCRIPTOR GetPixelFormat() { PIXELFORMATDESCRIPTOR result = new PIXELFORMATDESCRIPTOR(); result.nSize = (ushort)Marshal.SizeOf(result); result.nVersion = 1; result.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER; result.iPixelType = WinGDI.PFD_TYPE_RGBA; result.cColorBits = 32; result.cDepthBits = 24; result.cStencilBits = 8; result.cAuxBuffers = 0; result.iLayerType = WinGDI.PFD_MAIN_PLANE; return(result); }
private bool SetupPixelFormat(ref uint hdc) { PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); ushort pfdSize = (ushort)Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR)); // sizeof(PIXELFORMATDESCRIPTOR) pfd.nSize = pfdSize; // size of pfd pfd.nVersion = 1; // version number pfd.dwFlags = (PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER); // flags pfd.iPixelType = (byte)PFD_TYPE_RGBA; // RGBA type pfd.cColorBits = (byte)GetDeviceCaps(hdc, BITSPIXEL); // color depth pfd.cRedBits = 0; // color bits ignored pfd.cRedShift = 0; pfd.cGreenBits = 0; pfd.cGreenShift = 0; pfd.cBlueBits = 0; pfd.cBlueShift = 0; pfd.cAlphaBits = 0; // no alpha buffer pfd.cAlphaShift = 0; // shift bit ignored pfd.cAccumBits = 0; // no accumulation buffer pfd.cAccumRedBits = 0; // accum bits ignored pfd.cAccumGreenBits = 0; pfd.cAccumBlueBits = 0; pfd.cAccumAlphaBits = 0; pfd.cDepthBits = 32; // 32-bit z-buffer pfd.cStencilBits = 0; // no stencil buffer pfd.cAuxBuffers = 0; // no auxiliary buffer pfd.iLayerType = (byte)PFD_MAIN_PLANE; // main layer pfd.bReserved = 0; // reserved pfd.dwLayerMask = 0; // layer masks ignored pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; int pixelformat = ChoosePixelFormat(hdc, ref pfd); if (pixelformat == 0) // Did Windows Find A Matching Pixel Format? { MessageBox.Show("Can't Find A Suitable PixelFormat.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (SetPixelFormat(hdc, pixelformat, ref pfd) == 0) // Are We Able To Set The Pixel Format? { MessageBox.Show("Can't Set The PixelFormat.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
internal static void SetPixelFormat(IntPtr HDC, PIXELFORMATDESCRIPTOR PixelFormatDescriptor) { PixelFormatDescriptor.dwFlags |= PIXELFORMATDESCRIPTOR.PFD_SUPPORT_OPENGL; int format = ChoosePixelFormat(HDC, &PixelFormatDescriptor); int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (error != 0) { throw new Exception("Invalid pixel format"); } SetPixelFormat(HDC, format, &PixelFormatDescriptor); error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (error != 0) { throw new Exception("Invalid pixel format"); } }
private static void setPixelFormat(IntPtr hdc) { PIXELFORMATDESCRIPTOR format = new PIXELFORMATDESCRIPTOR(); format.nSize = (ushort)Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR)); format.nVersion = 1; format.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; format.PixelType = 24; format.cDepthBits = 32; int pixelFormat = ChoosePixelFormat(hdc, ref format); SetPixelFormat(hdc, pixelFormat, ref format); IntPtr context = wglCreateContext(hdc); wglMakeCurrent(hdc, context); }
private void InitializeOpenGL() { if (IsInDesignMode) { Paint += OnPaintDesigner; return; } SetClassLong(hWnd, GCL_HBRBACKGROUND, 0); SetStyle(ControlStyles.Opaque, true); IntPtr hDC = GetDC(hWnd); try { PIXELFORMATDESCRIPTOR pfd = pfdDefault; int pixelFormat = ChoosePixelFormat(hDC, ref pfd); // If the function fails, the return value is zero CheckAction("ChoosePixelFormat failed.", pixelFormat); int result = SetPixelFormat(hDC, pixelFormat, ref pfd); // If the function fails, the return value is FALSE. CheckAction("SetPixelFormat failed.", result); hRC = wglCreateContext(hDC); // If the function fails, the return value is NULL CheckAction("wglCreateContext failed.", hRC.ToInt32()); wglMakeCurrent(hDC, hRC); LoadFont(hDC); OnActivitedOpenGL(); Paint += OnPaint; wglMakeCurrent(hDC, IntPtr.Zero); } catch (Exception x) { errorMessage = x.Message; this.Paint += OnPaintError; } ReleaseDC(hWnd, hDC); }
private void PrintPixelFormat(IntPtr dc) { Debug.WriteLine("== Win32 DC PixelFormat =="); PIXELFORMATDESCRIPTOR pfd = default; int iPixelFormat; // if the device context has a current pixel format ... iPixelFormat = GetPixelFormat(dc); if (iPixelFormat > 0) { // obtain a detailed description of that pixel format DescribePixelFormat( dc, iPixelFormat, (uint)Marshal.SizeOf <PIXELFORMATDESCRIPTOR>(), ref pfd); Debug.WriteLine(pfd.ToString()); } else { Debug.WriteLine("Invalid pixel format"); } }
protected override void OnCreateControl() { base.OnCreateControl(); int samples = 16; int pixelFormat = GetAAPixelFormat(ref samples); PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.nSize = (ushort)Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR)); pfd.nVersion = 1; pfd.dwFlags = PixelFlag.PFD_DRAW_TO_WINDOW | PixelFlag.PFD_SUPPORT_OPENGL | PixelFlag.PFD_DOUBLEBUFFER; pfd.iPixelType = PixelType.PFD_TYPE_RGBA; pfd.cColorBits = 32; m_hDC = GDI.GetDC(Handle); GDI.SetPixelFormat(m_hDC, pixelFormat, ref pfd); m_hRC = WGL.wglCreateContext(m_hDC); WGL.wglMakeCurrent(m_hDC, m_hRC); }
// Код подготовки окна OpenGL (который в данном // случае вызывается из конструктора элемента // управления) protected unsafe bool OpenGLInit() { // "Явная" загрузка OPENGL32.DLL // uint m_hModuleOGL = LoadLibrary (openGL); // Retrieve a handle to a display device context for the client area of the specified window IntPtr hdc = GetDC(Handle); // Create a pixel format PIXELFORMATDESCRIPTOR pfd = SetPFD(); pfd.nSize = (ushort)sizeof(PIXELFORMATDESCRIPTOR); // Match an appropriate pixel format int iPixelformat; if ((iPixelformat = ChoosePixelFormat(hdc, &pfd)) == 0) { return(false); } // Sets the pixel format if (SetPixelFormat(hdc, iPixelformat, &pfd) == 0) { return(false); } // Create a new OpenGL rendering contex m_hRC = wglCreateContext(hdc); m_hDC = hdc; // Make the OpenGL rendering context the current rendering context int rv = wglMakeCurrent(m_hDC, m_hRC); PrepareScene(); 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()); } }
public static unsafe extern int SetPixelFormat( uint hdc, int iPixelFormat, PIXELFORMATDESCRIPTOR * p_pfd );
public unsafe override bool MakeCurrent(IntPtr gdkDrawable) { if (!initialized) { IntPtr hwnd = gdk_win32_drawable_get_handle(gdkDrawable); deviceContext = GetDC(hwnd); // pfd Tells Windows How We Want Things To Be PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); // Size Of This Pixel Format Descriptor pfd.nSize = (ushort)sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; pfd.iPixelType = PFD_TYPE_COLORINDEX; pfd.cColorBits = 32; for (int i = 0; i < attributes.Length; ++i) { switch (attributes[i]) { case GLContextAttributes.AccumAlphaSize: pfd.cAccumAlphaBits = (byte)attributes[++i]; break; case GLContextAttributes.AccumBlueSize: pfd.cAccumBlueBits = (byte)attributes[++i]; break; case GLContextAttributes.AccumGreenSize: pfd.cAccumGreenBits = (byte)attributes[++i]; break; case GLContextAttributes.AccumRedSize: pfd.cAccumRedBits = (byte)attributes[++i]; break; case GLContextAttributes.AlphaSize: pfd.cAlphaBits = (byte)attributes[++i]; break; case GLContextAttributes.AuxBuffers: pfd.cAuxBuffers = (byte)attributes[++i]; break; case GLContextAttributes.BlueSize: pfd.cBlueBits = (byte)attributes[++i]; break; case GLContextAttributes.BufferSize: // ?!? ++i; break; case GLContextAttributes.DepthSize: pfd.cDepthBits = (byte)attributes[++i]; break; case GLContextAttributes.DoubleBuffer: pfd.dwFlags |= PFD_DOUBLEBUFFER; break; case GLContextAttributes.GreenSize: pfd.cGreenBits = (byte)attributes[++i]; break; case GLContextAttributes.Level: // ? ++i; break; case GLContextAttributes.None: // ? break; case GLContextAttributes.RedSize: pfd.cRedBits = (byte)attributes[++i]; break; case GLContextAttributes.Rgba: pfd.iPixelType = PFD_TYPE_RGBA; break; case GLContextAttributes.StencilSize: pfd.cStencilBits = (byte)attributes[++i]; break; case GLContextAttributes.Stereo: pfd.dwFlags |= PFD_STEREO; break; } } // Tell the graphics hardware how to display pixels. int pixelFormat = ChoosePixelFormat(deviceContext, new IntPtr(&pfd)); if (pixelFormat == 0) return false; if (!SetPixelFormat(deviceContext, pixelFormat, new IntPtr(&pfd))) throw new Exception("Couldn't set pixelformat"); renderingContext = wglCreateContext(deviceContext); if (renderingContext == IntPtr.Zero) throw new Exception("Couldn't create rendering context"); if (share != null) { if (share.renderingContext != IntPtr.Zero) { Console.WriteLine("DoSharing"); if (!wglShareLists(share.renderingContext, renderingContext)) throw new Exception("Can't share opengl contexts"); } else throw new Exception("Trying to share with uninitialized context..."); } else { Console.WriteLine("Share zero"); } initialized = true; } return wglMakeCurrent(deviceContext, renderingContext); }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="bitCount">The bitcount.</param> protected virtual bool SetPixelFormat(uint bitCount) { // Create the big lame pixel format majoo. PIXELFORMATDESCRIPTOR pixelFormat = new PIXELFORMATDESCRIPTOR(); // Set the values for the pixel format. pixelFormat.nSize = 40; pixelFormat.nVersion = 1; pixelFormat.dwFlags = (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI); pixelFormat.iPixelType = PFD_TYPE_RGBA; pixelFormat.cColorBits = (byte)bitCount; pixelFormat.cRedBits = 0; pixelFormat.cRedShift = 0; pixelFormat.cGreenBits = 0; pixelFormat.cGreenShift = 0; pixelFormat.cBlueBits = 0; pixelFormat.cBlueShift = 0; pixelFormat.cAlphaBits = 0; pixelFormat.cAlphaShift = 0; pixelFormat.cAccumBits = 0; pixelFormat.cAccumRedBits = 0; pixelFormat.cAccumGreenBits = 0; pixelFormat.cAccumBlueBits = 0; pixelFormat.cAccumAlphaBits = 0; pixelFormat.cDepthBits = 32; pixelFormat.cStencilBits = 0; pixelFormat.cAuxBuffers = 0; pixelFormat.iLayerType = PFD_MAIN_PLANE; pixelFormat.bReserved = 0; pixelFormat.dwLayerMask = 0; pixelFormat.dwVisibleMask = 0; pixelFormat.dwDamageMask = 0; // Match an appropriate pixel format int iPixelformat; if((iPixelformat = ChoosePixelFormat(hDC, pixelFormat)) == 0 ) return false; // Sets the pixel format if(SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return false; } return true; }
// DWORD GetLastError(VOID); // ############################################################################ // ############################################################################ // ############################################################################ // ############################################################################ // ============================================================================ // The following function, DemoCreateRenderingContext(ref_uint_DC,ref_uint_RC), // can be used as a simple way to create an OpenGL "Rendering Context" (RC). // **** DO NOT CALL DemoCreateRenderingContext() DIRECTLY IF YOU CHOOSE TO // CALL DemoInitOpenGL() (below) TO ESTABLISH OPENGL. **** // ============================================================================ public static unsafe void DemoCreateRenderingContext ( ref uint ref_uint_DC, ref uint ref_uint_RC ) { ref_uint_RC = 0; PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); // -------------------------------------------------------------------------- pfd.nSize = 40; // sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER); pfd.iPixelType = (byte)(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 = 32; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = (byte)(PFD_MAIN_PLANE); pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Choose Pixel Format // -------------------------------------------------------------------------- int iPixelFormat = 0; PIXELFORMATDESCRIPTOR* _pfd = &pfd; iPixelFormat = WGL.ChoosePixelFormat(ref_uint_DC, _pfd); if (0 == iPixelFormat) { uint uint_LastError = WGL.GetLastError(); string string_Message = "ChoosePixelFormat() FAILED: Error: " + uint_LastError; WGL.MessageBox( 0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK ); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Set Pixel Format // -------------------------------------------------------------------------- int int_Result_SPF = 0; int_Result_SPF = WGL.SetPixelFormat(ref_uint_DC, iPixelFormat, _pfd); if (0 == int_Result_SPF) { uint uint_LastError = WGL.GetLastError(); string string_Message = "SetPixelFormat() FAILED. Error: " + uint_LastError; WGL.MessageBox( 0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK ); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Create Rendering Context (RC) // NOTE: You will get the following error: // 126 : ERROR_MOD_NOT_FOUND // if you attempt to create a render context too soon after creating a // window and getting its Device Context (DC). // See the comments for WGL.DemoInitOpenGL() on how to use a call to // WGL.wglSwapBuffers( ref_uint_DC ) before attempting to create the RC. // -------------------------------------------------------------------------- ref_uint_RC = WGL.wglCreateContext( ref_uint_DC ); if (0 == ref_uint_RC) { uint uint_LastError = WGL.GetLastError(); string string_Message = "wglCreateContext() FAILED. Error: " + uint_LastError; WGL.MessageBox( 0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK ); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Make the new Render Context (RC) the current Render Context (RC) // -------------------------------------------------------------------------- int int_Result_MC = 0; int_Result_MC = WGL.wglMakeCurrent( ref_uint_DC, ref_uint_RC ); if (0 == int_Result_MC) { uint uint_LastError = WGL.GetLastError(); string string_Message = "wglMakeCurrent() FAILED. Error: " + uint_LastError; WGL.MessageBox( 0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK ); // *************************** WGL.wglDeleteContext( ref_uint_RC ); ref_uint_RC = 0; // *************************** return; } // -------------------------------------------------------------------------- }
int SetPixelFormat( uint hdc, int iPixelFormat, PIXELFORMATDESCRIPTOR * p_pfd );
/// <summary> /// Creates the rendering context. /// </summary> /// <param name="windowHandle">The window handle.</param> /// <returns>True if the rendering context was created successfully, otherwise false.</returns> public static bool CreateContext(IntPtr windowHandle) { var opengl32 = LoadLibrary("opengl32.dll"); var pfd = new PIXELFORMATDESCRIPTOR { Size = (ushort)Marshal.SizeOf<PIXELFORMATDESCRIPTOR>(), Version = 1, Flags = PFD.DOUBLEBUFFER | PFD.DRAW_TO_WINDOW | PFD.SUPPORT_OPENGL, ColorBits = 32, DepthBits = 24, StencilBits = 8, }; hwnd = windowHandle; dc = GetDC(hwnd); SetPixelFormat(dc, ChoosePixelFormat(dc, ref pfd), ref pfd); if (dc == IntPtr.Zero) { Trace.TraceError("Failed to get the device context"); return false; } rc = wglCreateContext(dc); wglMakeCurrent(dc, rc); if (rc == IntPtr.Zero) { Trace.TraceError("Failed to create the rendering context"); return false; } var found = true; foreach (var entryPoint in typeof(Native).GetFields(BindingFlags.Public | BindingFlags.Static)) { var address = GetProcAddress(opengl32, entryPoint.Name); if (address == IntPtr.Zero) { address = wglGetProcAddress(entryPoint.Name); } if (address != IntPtr.Zero && address != new IntPtr(1) && address != new IntPtr(2)) { entryPoint.SetValue(null, Marshal.GetDelegateForFunctionPointer(address, entryPoint.FieldType)); } else { Trace.TraceError("Missing OpenGL entry point: {0}", entryPoint.Name); found = false; } } return found; }
public static extern int ChoosePixelFormat(uint hdc, ref PIXELFORMATDESCRIPTOR p_pfd);
/// <summary> /// The <b>SetPixelFormat</b> function sets the pixel format of the specified device context to the format /// specified by the <i>iPixelFormat</i> index. /// </summary> /// <param name="deviceContext"> /// Specifies the device context whose pixel format the function attempts to set. /// </param> /// <param name="pixelFormat"> /// Index that identifies the pixel format to set. The various pixel formats supported by a device /// context are identified by one-based indexes. /// </param> /// <param name="pixelFormatDescriptor"> /// Pointer to a <see cref="Gdi.PIXELFORMATDESCRIPTOR" /> structure that contains the logical pixel /// format specification. The system's metafile component uses this structure to record the logical /// pixel format specification. The structure has no other effect upon the behavior of the /// <b>SetPixelFormat</b> function. /// </param> /// <returns> /// If the function succeeds, the return value is true.<br /><br /> /// If the function fails, the return value is false. To get extended error information, call /// see cref="Kernel.GetLastError" />. /// </returns> /// <remarks> /// If <i>hdc</i> references a window, calling the <b>SetPixelFormat</b> function also changes the pixel format /// of the window. Setting the pixel format of a window more than once can lead to significant complications /// for the Window Manager and for multithread applications, so it is not allowed. An application can only set /// the pixel format of a window one time. Once a window's pixel format is set, it cannot be changed.<br /><br /> /// /// You should select a pixel format in the device context before calling the <see cref="Wgl.wglCreateContext" /> /// function. The <b>wglCreateContext</b> function creates a rendering context for drawing on the device in the /// selected pixel format of the device context.<br /><br /> /// /// An OpenGL window has its own pixel format. Because of this, only device contexts retrieved for the client /// area of an OpenGL window are allowed to draw into the window. As a result, an OpenGL window should be created /// with the WS_CLIPCHILDREN and WS_CLIPSIBLINGS styles. Additionally, the window class attribute should not /// include the CS_PARENTDC style.<br /><br /> /// /// The following code example shows <b>SetPixelFormat</b> usage:<br /><br /> /// /// <code> /// HDC hdc; /// int pixelFormat; /// Gdi.PIXELFORMATDESCRIPTOR pfd; /// /// // size of this pfd /// pfd.nSize = (ushort) sizeof(Gdi.PIXELFORMATDESCRIPTOR); /// /// // version number /// pfd.nVersion = 1; /// /// // support window, support OpenGL, double buffered /// pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER; /// /// // RGBA type /// pfd.iPixelType = Gdi.PFD_TYPE_RGBA; /// /// // 24-bit color depth /// pfd.cColorBits = 24; /// /// // color bits and shift bits ignored /// pfd.cRedBits = 0; /// pfd.cRedShift = 0; /// pfd.cGreenBits = 0; /// pfd.cGreenShift = 0; /// pfd.cBlueBits = 0; /// pfd.cBlueShift = 0; /// pfd.cAlphaBits = 0; /// pfd.cAlphaShift = 0; /// /// // no accumulation buffer, accum bits ignored /// pfd.cAccumBits = 0; /// pfd.cAccumRedBits = 0; /// pfd.cAccumGreenBits = 0; /// pfd.cAccumBlueBits = 0; /// pfd.cAccumAlphaBits = 0; /// /// // no stencil buffer /// pfd.cStencilBits = 0; /// /// // no auxiliary buffer /// pfd.cAuxBuffers = 0; /// /// // main layer /// pfd.iLayerType = Gdi.PFD_MAIN_PLANE; /// /// // reserved /// pfd.bReserved = 0; /// /// // layer masks ignored /// pfd.dwLayerMask = 0; /// pfd.dwVisibleMask = 0; /// pfd.dwDamageMask = 0; /// /// pixelFormat = Gdi.ChoosePixelFormat(hdc, &pfd); /// /// // make that the pixel format of the device context /// Gdi.SetPixelFormat(hdc, pixelFormat, &pfd); /// </code> /// </remarks> /// <seealso cref="ChoosePixelFormat" /> /// seealso cref="DescribePixelFormat" /> /// seealso cref="GetPixelFormat" /> public static bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { Kernel.LoadLibrary("opengl32.dll"); return _SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor); }
public static extern bool _SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
public static unsafe extern int ChoosePixelFormat(IntPtr hdc, PIXELFORMATDESCRIPTOR* ppfd);
public static unsafe extern System.Int32 ChoosePixelFormat(IntPtr hdc, ref PIXELFORMATDESCRIPTOR ppfd);
public static extern bool DescribePixelFormat(IntPtr hdc, int iPixelFormat, UInt32 nBytes, [In, Out] ref PIXELFORMATDESCRIPTOR ppfd);
public static extern bool SetPixelFormat(IntPtr hDC, int iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd);
/// <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> /// 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> /// /// </summary> /// <param name="deviceContext"></param> /// <param name="pixelFormat"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { bool retValue = UnsafeNativeMethods.SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor); LogFunction("SetPixelFormat(0x{0}, {1}, {2}) = {3}", deviceContext.ToString("X8"), pixelFormat, pixelFormatDescriptor, retValue); DebugCheckErrors(null); return (retValue); }
public static extern int ChoosePixelFormat(IntPtr deviceContext, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
public static unsafe extern int ChoosePixelFormat( uint hdc, PIXELFORMATDESCRIPTOR * p_pfd );
private void InitializeOpenGL() { uint hDC = GetDC(hWnd); PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); ZeroPixelDescriptor(ref pfd); pfd.nVersion = 1; pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER); pfd.iPixelType = (byte)(PFD_TYPE_RGBA); pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = (byte)(PFD_MAIN_PLANE); int pixelFormat = 0; pixelFormat = ChoosePixelFormat(hDC, ref pfd); SetPixelFormat(hDC, pixelFormat, ref pfd); hRC = wglCreateContext(hDC); LoadFont(hDC); ReleaseDC(hWnd, hDC); }
/// <summary> /// Creates the OpenGL rendering context. /// </summary> protected virtual void CreateOpenGLContext() { try { // Fill in the pixel format descriptor. PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); pfd.Initialize(); // Allow the user to correct the pixel format descriptor. OnPreparePFD(ref pfd); _hDC = GetDC(Handle); if (_hDC == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); // Choose appropriate pixel format supported by a _hDC context int iPixelformat = ChoosePixelFormat(_hDC, ref pfd); if (iPixelformat == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); // Set the pixel format if (SetPixelFormat(_hDC, iPixelformat, ref pfd) == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); // Create a new OpenGL rendering context _hRC = wglCreateContext(_hDC); if (_hRC == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); // Make _hRC rendering context as a current context if (wglMakeCurrent(_hDC, _hRC) == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); // Allow the user to tune the scene OnInitScene(); // Release the OpenGL rendering context wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); } catch (Exception) { if (_hRC != IntPtr.Zero) wglDeleteContext(_hRC); if (_hDC != IntPtr.Zero) ReleaseDC(Handle, _hDC); throw; } }
public static extern uint SetPixelFormat(uint hdc, int iPixelFormat, ref PIXELFORMATDESCRIPTOR p_pfd);
public static extern uint SetPixelFormat( uint hdc, int iPixelFormat, ref PIXELFORMATDESCRIPTOR p_pfd );
[DllImport("gdi32.dll", SetLastError=true)] public unsafe static extern System.Int32 SetPixelFormat(IntPtr hdc, System.Int32 iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd);
int ChoosePixelFormat( uint hdc, PIXELFORMATDESCRIPTOR * p_pfd );
/// <summary> /// Called before creation OpenGL window. By overriding this function, /// user can modify fields of the PIXELFORMATDESCRIPTOR structure. /// </summary> /// <param name="pfd">A reference to the PIXELFORMATDESCRIPTOR structure.</param> protected virtual void OnPreparePFD(ref PIXELFORMATDESCRIPTOR pfd) { }
public static extern int ChoosePixelFormat( uint hdc, ref PIXELFORMATDESCRIPTOR p_pfd );
/// <summary> /// /// </summary> /// <param name="hdc"></param> /// <param name="iPixelFormat"></param> /// <param name="nBytes"></param> /// <param name="pixelFormatDescriptor"></param> /// <returns></returns> public static bool DescribePixelFormat(IntPtr hdc, int iPixelFormat, UInt32 nBytes, [In, Out] ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) { bool retValue = UnsafeNativeMethods.DescribePixelFormat(hdc, iPixelFormat, nBytes, ref pixelFormatDescriptor); LogFunction("DescribePixelFormat(0x{0}, {1}, {2}, {3}) = {4}", hdc.ToString("X8"), iPixelFormat, nBytes, pixelFormatDescriptor, retValue); DebugCheckErrors(null); return(retValue); }
void HandleRealized(object sender, EventArgs eventArgs) { if (renderingContextHandle.Handle != IntPtr.Zero) return; switch (platformID) { case PlatformID.Win32NT: case PlatformID.Win32S: case PlatformID.Win32Windows: case PlatformID.WinCE: LoadLibrary("opengl32.dll"); IntPtr windowHandle = gdk_win32_drawable_get_handle(GdkWindow.Handle); IntPtr deviceContext = GetDC(windowHandle); PIXELFORMATDESCRIPTOR pixelFormatDescriptor = new PIXELFORMATDESCRIPTOR(); pixelFormatDescriptor.nSize = (short)System.Runtime.InteropServices.Marshal.SizeOf(pixelFormatDescriptor); pixelFormatDescriptor.nVersion = 1; pixelFormatDescriptor.iPixelType = PFD_TYPE_RGBA; pixelFormatDescriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; if (doubleBuffer) pixelFormatDescriptor.dwFlags |= PFD_DOUBLEBUFFER; pixelFormatDescriptor.cColorBits = colorBits; pixelFormatDescriptor.cAlphaBits = alphaBits; pixelFormatDescriptor.cDepthBits = depthBits; pixelFormatDescriptor.cStencilBits = stencilBits; int pixelFormat = ChoosePixelFormat(deviceContext, ref pixelFormatDescriptor); if (!SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor)) throw new Exception("Cannot SetPixelFormat!"); IntPtr renderingContext = wglCreateContext(deviceContext); renderingContextHandle = new HandleRef(this, renderingContext); ReleaseDC(windowHandle, deviceContext); if (sharedContextWidget != null) { GLWidget primaryWidget = sharedContextWidget; while (primaryWidget.sharedContextWidget != null) primaryWidget = primaryWidget.sharedContextWidget; if (primaryWidget.RenderingContextHandle.Handle != IntPtr.Zero) { wglShareLists(primaryWidget.RenderingContextHandle.Handle, RenderingContextHandle.Handle); } else { this.sharedContextWidget = null; primaryWidget.sharedContextWidget = this; } } break; case PlatformID.Unix: default: int[] attributeList = new int[24]; int attributeIndex = 0; attributeList[attributeIndex++] = GLX_RGBA; if (doubleBuffer) attributeList[attributeIndex++] = GLX_DOUBLEBUFFER; attributeList[attributeIndex++] = GLX_RED_SIZE; attributeList[attributeIndex++] = 1; attributeList[attributeIndex++] = GLX_GREEN_SIZE; attributeList[attributeIndex++] = 1; attributeList[attributeIndex++] = GLX_BLUE_SIZE; attributeList[attributeIndex++] = 1; if (alphaBits != 0) { attributeList[attributeIndex++] = GLX_ALPHA_SIZE; attributeList[attributeIndex++] = 1; } if (depthBits != 0) { attributeList[attributeIndex++] = GLX_DEPTH_SIZE; attributeList[attributeIndex++] = 1; } if (stencilBits != 0) { attributeList[attributeIndex++] = GLX_STENCIL_SIZE; attributeList[attributeIndex++] = 1; } attributeList[attributeIndex++] = GLX_NONE; IntPtr xDisplay = gdk_x11_display_get_xdisplay(Screen.Display.Handle); IntPtr visualIntPtr = IntPtr.Zero; try { visualIntPtr = glXChooseVisual(xDisplay, Screen.Number, attributeList); } catch (DllNotFoundException e) { throw new Exception("OpenGL dll not found!", e); } catch (EntryPointNotFoundException enf) { throw new Exception("Glx entry point not found!", enf); } if (visualIntPtr == IntPtr.Zero) { throw new Exception("Visual"); } XVisualInfo xVisualInfo = (XVisualInfo)Marshal.PtrToStructure(visualIntPtr, typeof(XVisualInfo)); IntPtr xRenderingContext = IntPtr.Zero; if (sharedContextWidget != null) { GLWidget primaryWidget = sharedContextWidget; while (primaryWidget.sharedContextWidget != null) primaryWidget = primaryWidget.sharedContextWidget; if (primaryWidget.RenderingContextHandle.Handle != IntPtr.Zero) { xRenderingContext = glXCreateContext(xDisplay, visualIntPtr, primaryWidget.RenderingContextHandle, true); } else { xRenderingContext = glXCreateContext(xDisplay, visualIntPtr, new HandleRef(null, IntPtr.Zero), true); this.sharedContextWidget = null; primaryWidget.sharedContextWidget = this; } } else { xRenderingContext = glXCreateContext(xDisplay, visualIntPtr, new HandleRef(null, IntPtr.Zero), true); } if (xRenderingContext == IntPtr.Zero) { throw new Exception("Unable to create rendering context"); } renderingContextHandle = new HandleRef(this, xRenderingContext); visual = (Gdk.Visual)GLib.Object.GetObject(gdk_x11_screen_lookup_visual(Screen.Handle, xVisualInfo.visualid)); if (visualIntPtr != IntPtr.Zero) { XFree(visualIntPtr); } break; } }
public static extern int ChoosePixelFormat(IntPtr deviceContext, [In] ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
private static extern bool SetPixelFormat(IntPtr hdc, int iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd);
public static extern bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
public static unsafe extern System.Int32 SetPixelFormat(IntPtr hdc, System.Int32 iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd);