Axiom.RenderSystems.OpenGL.GLSupport.SelectPixelFormat C# (CSharp) Метод

SelectPixelFormat() публичный Метод

public SelectPixelFormat ( IntPtr hdc, int colorDepth, int multisample ) : bool
hdc System.IntPtr
colorDepth int
multisample int
Результат bool
        public bool SelectPixelFormat(IntPtr hdc, int colorDepth, int multisample)
        {
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
            pfd.nSize = (short)Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
            pfd.cColorBits = (byte)((colorDepth > 16) ? 24 : colorDepth);
            pfd.cAlphaBits = (byte)((colorDepth > 16) ? 8 : 0);
            pfd.cDepthBits = 24;
            pfd.cStencilBits = 8;

            int[] format = new int[1];

            if (multisample != 0)
            {
                // only available with driver support
                if (!_hasMultisample || !_hasPixelFormatARB)
                    return false;

                int[] iattr = {
					Wgl.WGL_DRAW_TO_WINDOW_ARB, 1,
					Wgl.WGL_SUPPORT_OPENGL_ARB, 1,
					Wgl.WGL_DOUBLE_BUFFER_ARB, 1,
					Wgl.WGL_SAMPLE_BUFFERS_ARB, 1,
					Wgl.WGL_ACCELERATION_ARB, Wgl.WGL_FULL_ACCELERATION_ARB,
					Wgl.WGL_COLOR_BITS_ARB, pfd.cColorBits,
					Wgl.WGL_ALPHA_BITS_ARB, pfd.cAlphaBits,
					Wgl.WGL_DEPTH_BITS_ARB, pfd.cDepthBits,
					Wgl.WGL_STENCIL_BITS_ARB, pfd.cStencilBits,
					Wgl.WGL_SAMPLES_ARB, multisample,
					0
				};

                int[] nformats = new int[1];
                //int nformats;
                Debug.Assert(_wglChoosePixelFormatARB != null, "failed to get proc address for ChoosePixelFormatARB");
                // ChoosePixelFormatARB proc address was obtained when setting up a dummy GL context in initialiseWGL()
                // since glew hasn't been initialized yet, we have to cheat and use the previously obtained address
                bool result = Wgl.wglChoosePixelFormatARB(hdc, iattr, null, 1, format, nformats); // Tao 2.0
                //int result = Wgl.wglChoosePixelFormatARB( _wglChoosePixelFormatARB, hdc, iattr, null, 1, format, out nformats );
                if (!result || nformats[0] <= 0) // Tao 2.0
                    //if ( result == 0 || nformats <= 0 )
                    return false;
            }
            else
            {
                format[0] = Gdi.ChoosePixelFormat(hdc, ref pfd);
            }

            return (format[0] != 0 && Gdi.SetPixelFormat(hdc, format[0], ref pfd));
        }

Usage Example

Пример #1
0
        public override void Create(string name, int width, int height, bool isFullScreen, NamedParameterList miscParams)
        {
            if (_hWindow != IntPtr.Zero)
            {
                dispose(true);
            }

            _hWindow          = IntPtr.Zero;
            this.name         = name;
            this.IsFullScreen = isFullScreen;
            this._isClosed    = false;

            // load window defaults
            this.left              = this.top = -1; // centered
            this.width             = width;
            this.height            = height;
            this._displayFrequency = 0;
            this.isDepthBuffered   = true;
            this.colorDepth        = IsFullScreen ? 32 : 16;

            IntPtr parentHwnd = IntPtr.Zero;
            string title      = name;
            bool   vsync      = false;
            int    fsaa       = 0;
            string border     = "";
            bool   outerSize  = false;

            #region Parameter Handling
            if (miscParams != null)
            {
                foreach (KeyValuePair <string, object> entry in miscParams)
                {
                    switch (entry.Key)
                    {
                    case "title":
                        title = entry.Value.ToString();
                        break;

                    case "left":
                        left = Int32.Parse(entry.Value.ToString());
                        break;

                    case "top":
                        top = Int32.Parse(entry.Value.ToString());
                        break;

                    case "depthBuffer":
                        isDepthBuffered = bool.Parse(entry.Value.ToString());
                        break;

                    case "vsync":
                        vsync = entry.Value.ToString() == "Yes" ? true : false;
                        break;

                    case "fsaa":
                        fsaa = Int32.Parse(entry.Value.ToString());
                        break;

                    case "externalWindowHandle":
                        _hWindow = (IntPtr)entry.Value;
                        if (_hWindow != IntPtr.Zero)
                        {
                            _isExternal  = true;
                            IsFullScreen = false;
                        }
                        break;

                    case "externalGLControl":
                        break;

                    case "border":
                        border = ((string)miscParams["border"]).ToLower();
                        break;

                    case "outerDimensions":
                        break;

                    case "displayFrequency":
                        if (IsFullScreen)
                        {
                            _displayFrequency = Int32.Parse(entry.Value.ToString());
                        }
                        break;

                    case "colorDepth":
                        if (IsFullScreen)
                        {
                            colorDepth = Int32.Parse(entry.Value.ToString());
                        }
                        break;

                    case "parentWindowHandle":
                        if (!IsFullScreen)
                        {
                            parentHwnd = (IntPtr)entry.Value;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            #endregion Parameter Handling

            if (!_isExternal)
            {
                DefaultForm form = new DefaultForm();

                form.ClientSize    = new System.Drawing.Size(width, height);
                form.MaximizeBox   = false;
                form.MinimizeBox   = false;
                form.StartPosition = SWF.FormStartPosition.CenterScreen;

                if (IsFullScreen)
                {
                    // Set the display to the desired resolution
                    Gdi.DEVMODE screenSettings = new Gdi.DEVMODE();
                    screenSettings.dmSize       = (short)Marshal.SizeOf(screenSettings);
                    screenSettings.dmPelsWidth  = width;                                            // Selected Screen Width
                    screenSettings.dmPelsHeight = height;                                           // Selected Screen Height
                    screenSettings.dmBitsPerPel = ColorDepth;                                       // Selected Bits Per Pixel
                    screenSettings.dmFields     = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                    // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
                    int result = User.ChangeDisplaySettings(ref screenSettings, User.CDS_FULLSCREEN);

                    if (result != User.DISP_CHANGE_SUCCESSFUL)
                    {
                        throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to change user display settings.");
                    }

                    // Adjust form to size the screen
                    form.Top             = 0;
                    form.Left            = 0;
                    form.FormBorderStyle = SWF.FormBorderStyle.None;
                    form.WindowState     = SWF.FormWindowState.Maximized;
#if !DEBUG
                    form.TopMost  = true;
                    form.TopLevel = true;
#endif
                }
                else
                {
                    if (parentHwnd != IntPtr.Zero)
                    {
                        form.Owner = (SWF.Form)SWF.Control.FromHandle(parentHwnd);
                    }
                    else
                    {
                        if (border == "none")
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.None;
                        }
                        else if (border == "fixed")
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
                            form.MaximizeBox     = false;
                        }
                    }

                    form.Top  = top;
                    form.Left = left;
                    //form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
                    form.WindowState = SWF.FormWindowState.Normal;
                    form.Text        = title;
                }

                WindowEventMonitor.Instance.RegisterWindow(this);

                form.RenderWindow = this;
                _hWindow          = form.Handle;
                form.Show();
            }

            IntPtr old_hdc     = Wgl.wglGetCurrentDC();
            IntPtr old_context = Wgl.wglGetCurrentContext();

            SWF.Control ctrl = SWF.Form.FromHandle(_hWindow);
            //Form frm = (Form)ctrl.TopLevelControl;
            this.top    = ctrl.Top;
            this.left   = ctrl.Left;
            this.width  = ctrl.ClientRectangle.Width;
            this.height = ctrl.ClientRectangle.Height;

            _hDeviceContext = User.GetDC(_hWindow);

            // Do not change vsync if the external window has the OpenGL control
            if (!_isExternalGLControl)
            {
                if (!_glSupport.SelectPixelFormat(_hDeviceContext, ColorDepth, fsaa))
                {
                    if (fsaa == 0)
                    {
                        throw new Exception("selectPixelFormat failed");
                    }

                    LogManager.Instance.Write("FSAA level not supported, falling back");
                    if (!_glSupport.SelectPixelFormat(_hDeviceContext, ColorDepth, 0))
                    {
                        throw new Exception("selectPixelFormat failed");
                    }
                }
            }

            // attempt to get the rendering context
            _hRenderingContext = Wgl.wglCreateContext(_hDeviceContext);

            if (_hRenderingContext == IntPtr.Zero)
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to create a GL rendering context.");
            }

            if (!Wgl.wglMakeCurrent(_hDeviceContext, _hRenderingContext))
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to activate the GL rendering context.");
            }

            // Do not change vsync if the external window has the OpenGL control
            if (!_isExternalGLControl)
            {
                // Don't use wglew as if this is the first window, we won't have initialised yet
                //IntPtr wglSwapIntervalEXT = Wgl.wglGetProcAddress( "wglSwapIntervalEXT" );
                //if ( wglSwapIntervalEXT != IntPtr.Zero )
                //Wgl.wglSwapIntervalEXT( wglSwapIntervalEXT, vsync );
                if (Wgl.IsExtensionSupported("wglSwapIntervalEXT"))
                {
                    Wgl.wglSwapIntervalEXT(vsync ? 1 : 0);                       // Tao 2.0
                }
            }

            if (old_context != IntPtr.Zero)
            {
                // Restore old context
                if (!Wgl.wglMakeCurrent(old_hdc, old_context))
                {
                    throw new Exception("wglMakeCurrent() failed");
                }

                // Share lists with old context
                if (!Wgl.wglShareLists(old_context, _hRenderingContext))
                {
                    throw new Exception("wglShareLists() failed");
                }
            }

            // Create RenderSystem context
            _glContext = new Win32Context(_hDeviceContext, _hRenderingContext);

            // make this window active
            this.IsActive = true;
        }