Пример #1
0
            public int CompareTo(object obj)
            {
                PixelFormatValue other = (PixelFormatValue)obj;

                //Sort by color depth, with greatest color depth first.
                if (other.pfd.cColorBits > pfd.cColorBits)
                {
                    return(1);
                }
                else if (other.pfd.cColorBits < pfd.cColorBits)
                {
                    return(-1);
                }
                //Next, sort by z-buffer depth, with greatest z-depth first.
                if (other.pfd.cDepthBits > pfd.cDepthBits)
                {
                    return(1);
                }
                else if (other.pfd.cDepthBits < pfd.cDepthBits)
                {
                    return(-1);
                }
                //Now by format number.
                if (other.formatNumber < formatNumber)
                {
                    return(1);
                }
                else if (other.formatNumber > formatNumber)
                {
                    return(-1);
                }
                //At this point the formats are considered equivalent.
                return(0);
            }
Пример #2
0
        public static PixelFormatValue[] PrioritizePixelFormats(Gdi.PIXELFORMATDESCRIPTOR[] unsortedFormats, bool requireDoubleBuffering, bool requireHardwareAccerleration)
        {
            ArrayList sortedFormats = new ArrayList();

            for (int i = 0; i < unsortedFormats.Length; i++)
            {
                Gdi.PIXELFORMATDESCRIPTOR pfd = unsortedFormats[i];
                long bpp      = pfd.cColorBits;
                long depth    = pfd.cDepthBits;
                bool pal      = FormatUsesPalette(pfd);
                bool hardware = FormatSupportsAcceleration(pfd);
                bool opengl   = FormatSupportsOpenGL(pfd);
                bool window   = FormatSupportsWindow(pfd);
                bool bitmap   = FormatSupportsBitmap(pfd);
                bool dbuff    = FormatSupportsDoubleBuffering(pfd);
                //Recognize formats which do not meet minimum requirements first and foremost.
                if (!opengl || !window || bpp < 8 || depth < 8 || pal || requireDoubleBuffering != dbuff || requireHardwareAccerleration != hardware)
                {
                    continue;
                }
                PixelFormatValue pfv = new PixelFormatValue();
                pfv.pfd          = pfd;
                pfv.formatNumber = i + 1;
                sortedFormats.Add(pfv);
            }
            sortedFormats.Sort();
            return((PixelFormatValue[])sortedFormats.ToArray(typeof(PixelFormatValue)));
        }
Пример #3
0
		public static PixelFormatValue[] PrioritizePixelFormats(Gdi.PIXELFORMATDESCRIPTOR[] unsortedFormats,bool requireDoubleBuffering,bool requireHardwareAccerleration) {
			ArrayList sortedFormats=new ArrayList();
			for(int i=0;i<unsortedFormats.Length;i++) {
				Gdi.PIXELFORMATDESCRIPTOR pfd=unsortedFormats[i];
				long bpp=pfd.cColorBits;
				long depth=pfd.cDepthBits;
				bool pal=FormatUsesPalette(pfd);
				bool hardware=FormatSupportsAcceleration(pfd);
				bool opengl=FormatSupportsOpenGL(pfd);
				bool window=FormatSupportsWindow(pfd);
				bool bitmap=FormatSupportsBitmap(pfd);
				bool dbuff=FormatSupportsDoubleBuffering(pfd);
				//Recognize formats which do not meet minimum requirements first and foremost.
				if(!opengl||!window||bpp<8||depth<8||pal||requireDoubleBuffering!=dbuff||requireHardwareAccerleration!=hardware) {
					continue;
				}
				PixelFormatValue pfv=new PixelFormatValue();
				pfv.pfd=pfd;
				pfv.formatNumber=i+1;
				sortedFormats.Add(pfv);
			}
			sortedFormats.Sort();
			return (PixelFormatValue[])sortedFormats.ToArray(typeof(PixelFormatValue));
		}
Пример #4
0
        /// <summary>
        /// Creates the device and rendering contexts using the supplied settings
        /// in accumBits, colorBits, depthBits, and stencilBits. Returns the selected
        /// pixel format number.
        /// </summary>
        protected virtual int CreateContexts(IntPtr pDeviceContext, int preferredPixelFormatNum)
        {
            deviceContext = pDeviceContext;
            if (deviceContext == IntPtr.Zero)
            {
                throw new Exception("CreateContexts: Unable to create an OpenGL device context");
            }
            int selectedFormat = 0;

            Gdi.PIXELFORMATDESCRIPTOR pixelFormat = new Gdi.PIXELFORMATDESCRIPTOR();
            pixelFormat.nSize    = (short)Marshal.SizeOf(pixelFormat);
            pixelFormat.nVersion = 1;
            //Here we care most about finding a format that will allow the proper creation of the control first and foremost, because even if the format is so bad that output to the display cannot be understood by the user, then the user is able to change the graphical options from within the program. We care second most about finding a format which is quick to load and which will be most time efficient during graphics display. Again, if a wrong choice is made, the user can make a choice of format manually.
            try {            //Simply try the preferred pixel format number. If it works, then that is all we care about. Remember, 0 is an invalid format number.
                if (_DescribePixelFormat(deviceContext, preferredPixelFormatNum, (uint)pixelFormat.nSize, ref pixelFormat) == 0 ||
                    !Gdi.SetPixelFormat(deviceContext, preferredPixelFormatNum, ref pixelFormat))
                {
                    throw new Exception(string.Format("Unable to set the requested pixel format ({0})", selectedFormat));
                }
                selectedFormat = preferredPixelFormatNum;
            }catch {           //Could not set the preferred pixel format for some reason. Possibly initial startup or the graphics card or driver changed since the program was last started.
                //Now try to auto-select the best pixel-format for speed efficientcy and graphical quality.
                try{
                    PixelFormatValue pfv = ChoosePixelFormatEx(deviceContext);
                    if (!Gdi.SetPixelFormat(deviceContext, pfv.formatNumber, ref pfv.pfd))
                    {
                        throw new Exception("");
                    }
                    pixelFormat    = pfv.pfd;
                    selectedFormat = pfv.formatNumber;
                }catch {
                    pixelFormat          = new Gdi.PIXELFORMATDESCRIPTOR();         //Zero out the old pixel format.
                    pixelFormat.nSize    = (short)Marshal.SizeOf(pixelFormat);
                    pixelFormat.nVersion = 1;
                    //Unable to select a good pixel format. Now we are desperate. Try all formats starting from 1 until we get some format which at least works. That way the user can change the pixel format from the graphical options from inside the program after this point.
                    selectedFormat = 0;
                    do
                    {
                        selectedFormat++;
                        if (selectedFormat > _DescribePixelFormat(deviceContext, selectedFormat, (uint)pixelFormat.nSize, ref pixelFormat))
                        {
                            throw new Exception("There are no acceptable pixel formats for OpenGL graphics.");
                        }
                    }while(!Gdi.SetPixelFormat(deviceContext, selectedFormat, ref pixelFormat));
                }
            }
            colorBits       = pixelFormat.cColorBits;
            depthBits       = pixelFormat.cDepthBits;
            autoSwapBuffers = FormatSupportsDoubleBuffering(pixelFormat);
            usehardware     = FormatSupportsAcceleration(pixelFormat);

            //Create rendering context
            renderContext = Wgl.wglCreateContext(deviceContext);

            if (renderContext == IntPtr.Zero)
            {
                throw new Exception("CreateContexts: Unable to create an OpenGL rendering context");
            }

            //Make this the current context
            MakeCurrentContext();
            return(selectedFormat);
        }