/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="hDC"></param> /// <param name="bitCount">The bitcount.</param> protected virtual bool SetPixelFormat(IntPtr hDC, int bitCount) { // Create the big lame pixel format majoo. var 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 = Win32.ChoosePixelFormat(hDC, pixelFormat); if (iPixelformat == 0) { return(false); } // Sets the pixel format if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); return(false); } return(true); }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="hDC"></param> /// <param name="parameters">parameters.</param> protected void SetPixelFormat(IntPtr hDC, ContextGenerationParams parameters) { // Create the big lame pixel format majoo. var pdf = new PixelFormatDescriptor(); pdf.Init(); // Set the values for the pixel format. pdf.nVersion = 1; pdf.dwFlags = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI); pdf.iPixelType = Win32.PFD_TYPE_RGBA; pdf.cColorBits = parameters.ColorBits; pdf.cDepthBits = parameters.DepthBits; pdf.cStencilBits = parameters.StencilBits; pdf.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat = Win32.ChoosePixelFormat(hDC, pdf); if (iPixelformat == 0) { throw new Exception(string.Format("ChoosePixelFormat([{0}], [{1}]) failed!", hDC, pdf)); } // Sets the pixel format if (false == Win32.SetPixelFormat(hDC, iPixelformat, pdf)) { int lastError = Marshal.GetLastWin32Error(); throw new Exception(string.Format("SetPixelFormat([{0}], [{1}], [{2}]) failed! Win32Error:[{3}].", hDC, iPixelformat, pdf, lastError)); } }
/// <summary> /// This function sets the pixel format of the underlying bitmap. /// </summary> /// <param name="hDC"></param> /// <param name="bitCount">The bitcount.</param> protected void SetPixelFormat(IntPtr hDC, int bitCount) { // Create the big lame pixel format majoo. var 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 = Win32.ChoosePixelFormat(hDC, pixelFormat); if (iPixelformat == 0) { throw new Exception(string.Format("ChoosePixelFormat([{0}], [{1}]) failed!", hDC, pixelFormat)); } // Sets the pixel format if (Win32.SetPixelFormat(hDC, iPixelformat, pixelFormat) == 0) { int lastError = Marshal.GetLastWin32Error(); throw new Exception(string.Format("SetPixelFormat([{0}], [{1}], [{2}]) failed! Win32Error:[{3}].", hDC, iPixelformat, pixelFormat, lastError)); } }
/// <summary> /// Create a new window class, as basic as possible. /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="bitDepth"></param> /// <returns></returns> private bool CreateBasicRenderContext(int width, int height, int bitDepth) { var 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 = "CSharpGLRenderWindow"; wndClass.hIconSm = IntPtr.Zero; Win32.RegisterClassEx(ref wndClass); // Create the window. Position and size it. windowHandle = Win32.CreateWindowEx(0, "CSharpGLRenderWindow", "", 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. this.DeviceContextHandle = Win32.GetDC(windowHandle); // Setup a pixel format. var 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 = Win32.ChoosePixelFormat(this.DeviceContextHandle, pfd); if (iPixelformat == 0) { return(false); } // Sets the pixel format if (Win32.SetPixelFormat(this.DeviceContextHandle, iPixelformat, pfd) == 0) { return(false); } return(true); }
/// <summary> /// Create a new window class, as basic as possible. /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="parameters"></param> /// <returns></returns> private bool CreateBasicRenderContext(int width, int height, ContextGenerationParams parameters) { var wndClass = new WNDCLASSEX(); wndClass.Init(); wndClass.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw | ClassStyles.OwnDC; wndClass.lpfnWndProc = wndProcDelegate; wndClass.lpszClassName = "CSharpGLRenderWindow"; Win32.RegisterClassEx(ref wndClass); // Create the window. Position and size it. windowHandle = Win32.CreateWindowEx(0, "CSharpGLRenderWindow", "", 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. this.DeviceContextHandle = Win32.GetDC(windowHandle); // Setup a pixel format. var 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 = parameters.ColorBits; pfd.cAccumBits = parameters.AccumBits; pfd.cAccumRedBits = parameters.AccumRedBits; pfd.cAccumGreenBits = parameters.AccumGreenBits; pfd.cAccumBlueBits = parameters.AccumBlueBits; pfd.cAccumAlphaBits = parameters.AccumAlphaBits; pfd.cDepthBits = parameters.DepthBits; pfd.cStencilBits = parameters.StencilBits; pfd.iLayerType = Win32.PFD_MAIN_PLANE; // Match an appropriate pixel format int iPixelformat = Win32.ChoosePixelFormat(this.DeviceContextHandle, pfd); if (iPixelformat == 0) { return(false); } // Sets the pixel format if (false == Win32.SetPixelFormat(this.DeviceContextHandle, iPixelformat, pfd)) { return(false); } // Create the render context. this.RenderContextHandle = Win32.wglCreateContext(this.DeviceContextHandle); return(true); }
/// <summary> /// Only valid to be called after the render context is created, this function attempts to /// move the render context to the OpenGL version originally requested. If this is > 2.1, this /// means building a new context. If this fails, we'll have to make do with 2.1. /// </summary> /// <param name="parameters"></param> protected bool UpdateContextVersion(ContextGenerationParams parameters) { // If the request version number is anything up to and including 2.1, standard render contexts // will provide what we need (as long as the graphics card drivers are up to date). // Now the none-trivial case. We must use the WGL_create_context extension to // attempt to create a 3.0+ context. try { var wglChoosePixelFormatARB = GL.Instance.GetDelegateFor("wglChoosePixelFormatARB", GLDelegates.typeof_bool_IntPtr_intN_floatN_uint_intN_uintN) as GLDelegates.bool_IntPtr_intN_floatN_uint_intN_uintN; if (wglChoosePixelFormatARB == null) { return(false); } var wglCreateContextAttribs = GL.Instance.GetDelegateFor("wglCreateContextAttribsARB", GLDelegates.typeof_IntPtr_IntPtr_IntPtr_intN) as GLDelegates.IntPtr_IntPtr_IntPtr_intN; if (wglCreateContextAttribs == null) { return(false); } int major, minor; GetHighestVersion(out major, out minor); if ((major > 2) || (major == 2 && minor > 1)) { int[] attribList = new int[] { WinGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE, WinGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE, WinGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE, WinGL.WGL_ACCELERATION_ARB, WinGL.WGL_FULL_ACCELERATION_ARB, WinGL.WGL_PIXEL_TYPE_ARB, WinGL.WGL_TYPE_RGBA_ARB, WinGL.WGL_COLOR_BITS_ARB, parameters.ColorBits, WinGL.WGL_ACCUM_BITS_ARB, parameters.AccumBits, WinGL.WGL_ACCUM_RED_BITS_ARB, parameters.AccumRedBits, WinGL.WGL_ACCUM_GREEN_BITS_ARB, parameters.AccumGreenBits, WinGL.WGL_ACCUM_BLUE_BITS_ARB, parameters.AccumBlueBits, WinGL.WGL_ACCUM_ALPHA_BITS_ARB, parameters.AccumAlphaBits, WinGL.WGL_DEPTH_BITS_ARB, parameters.DepthBits, WinGL.WGL_STENCIL_BITS_ARB, parameters.StencilBits, 0, //End }; IntPtr dc = this.DeviceContextHandle; // Match an appropriate pixel format int[] pixelFormat = new int[1]; uint[] numFormats = new uint[1]; if (false == wglChoosePixelFormatARB(dc, attribList, null, 1, pixelFormat, numFormats)) { return(false); } // Sets the pixel format if (false == Win32.SetPixelFormat(dc, pixelFormat[0], new PixelFormatDescriptor())) { return(false); } int[] attributes = { WinGL.WGL_CONTEXT_MAJOR_VERSION_ARB, major, WinGL.WGL_CONTEXT_MINOR_VERSION_ARB, minor, WinGL.WGL_CONTEXT_PROFILE_MASK_ARB, WinGL.WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //#if DEBUG // GL.WGL_CONTEXT_FLAGS, GL.WGL_CONTEXT_DEBUG_BIT,// this is a debug context //#endif 0 }; IntPtr hrc = wglCreateContextAttribs(dc, IntPtr.Zero, attributes); Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); Win32.wglDeleteContext(this.RenderContextHandle); Win32.wglMakeCurrent(dc, hrc); this.RenderContextHandle = hrc; } } catch (Exception ex) { Debug.WriteLine(ex); Log.Write(ex); } 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 = "CSharpGLRenderWindow"; wndClass.hIconSm = IntPtr.Zero; Win32.RegisterClassEx(ref wndClass); // Create the window. Position and size it. windowHandle = Win32.CreateWindowEx(0, "CSharpGLRenderWindow", "", 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); }