Exemple #1
0
        private void _initializeWgl()
        {
            // wglGetProcAddress does not work without an active OpenGL context,
            // but we need wglChoosePixelFormatARB's address before we can
            // create our main window.  Thank you very much, Microsoft!
            //
            // The solution is to create a dummy OpenGL window first, and then
            // test for WGL_ARB_pixel_format support.  If it is not supported,
            // we make sure to never call the ARB pixel format functions.
            //
            // If is is supported, we call the pixel format functions at least once
            // to initialize them (pointers are stored by glprocs.h).  We can also
            // take this opportunity to enumerate the valid FSAA modes.

            SWF.Form frm = new SWF.Form();
            IntPtr hwnd = frm.Handle;

            // if a simple CreateWindow fails, then boy are we in trouble...
            if (hwnd == IntPtr.Zero)
                throw new Exception("Window creation failed");

            // no chance of failure and no need to release thanks to CS_OWNDC
            IntPtr hdc = User.GetDC(hwnd);

            // assign a simple OpenGL pixel format that everyone supports
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
            ;
            pfd.nSize = (short)Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.cColorBits = 16;
            pfd.cDepthBits = 15;
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType = Gdi.PFD_TYPE_RGBA;

            // if these fail, wglCreateContext will also quietly fail
            int format;
            format = Gdi.ChoosePixelFormat(hdc, ref pfd);
            if (format != 0)
                Gdi.SetPixelFormat(hdc, format, ref pfd);

            IntPtr hrc = Wgl.wglCreateContext(hdc);
            if (hrc != IntPtr.Zero)
            {
                // if wglMakeCurrent fails, wglGetProcAddress will return null
                Wgl.wglMakeCurrent(hdc, hrc);
                Wgl.ReloadFunctions(); // Tao 2.0

                // check for pixel format and multisampling support

                //IntPtr wglGetExtensionsStringARB = Wgl.wglGetProcAddress( "wglGetExtensionsStringARB" );
                //if ( wglGetExtensionsStringARB != IntPtr.Zero )
                //{
                //    string exts = Wgl.wglGetExtensionsStringARB( wglGetExtensionsStringARB, hdc );
                //    _hasPixelFormatARB = exts.Contains( "WGL_ARB_pixel_format" );
                //    _hasMultisample = exts.Contains( "WGL_ARB_multisample" );
                //}

                _hasPixelFormatARB = Wgl.IsExtensionSupported("WGL_ARB_pixel_format");
                _hasMultisample = Wgl.IsExtensionSupported("WGL_ARB_multisample");

                if (_hasPixelFormatARB && _hasMultisample)
                {
                    // enumerate all formats w/ multisampling
                    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,
				        // We are no matter about the colour, depth and stencil buffers here
				        //WGL_COLOR_BITS_ARB, 24,
				        //WGL_ALPHA_BITS_ARB, 8,
				        //WGL_DEPTH_BITS_ARB, 24,
				        //WGL_STENCIL_BITS_ARB, 8,
				        //
				        Wgl.WGL_SAMPLES_ARB, 2,
				        0
				    };
                    int[] formats = new int[256];
                    int[] count = new int[256]; // Tao 2.0
                    //int count;
                    // cheating here.  wglChoosePixelFormatARB proc address needed later on
                    // when a valid GL context does not exist and glew is not initialized yet.
                    _wglChoosePixelFormatARB = Wgl.wglGetProcAddress("wglChoosePixelFormatARB");
                    if (Wgl.wglChoosePixelFormatARB(hdc, iattr, null, 256, formats, count)) // Tao 2.0
                    //if ( Wgl.wglChoosePixelFormatARB( _wglChoosePixelFormatARB, hdc, iattr, null, 256, formats, out count ) != 0 )
                    {
                        // determine what multisampling levels are offered
                        int query = Wgl.WGL_SAMPLES_ARB, samples;
                        for (int i = 0; i < count[0]; ++i) // Tao 2.0
                        //for ( int i = 0; i < count[ 0 ]; ++i )
                        {
                            IntPtr wglGetPixelFormatAttribivARB = Wgl.wglGetProcAddress("wglGetPixelFormatAttribivARB");
                            if (Wgl.wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, ref query, out samples)) // Tao 2.0
                            //if ( Wgl.wglGetPixelFormatAttribivARB( wglGetPixelFormatAttribivARB, hdc, formats[ i ], 0, 1, ref query, out samples ) != 0 )
                            {
                                if (!_fsaaLevels.Contains(samples))
                                    _fsaaLevels.Add(samples);
                            }
                        }
                    }
                }

                Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                Wgl.wglDeleteContext(hrc);
            }

            // clean up our dummy window and class
            frm.Dispose();
            frm = null;
        }
Exemple #2
0
        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));
        }