Пример #1
0
        /// <summary>
        /// Set pixel format by letting the driver choose the best pixel format following the criteria.
        /// </summary>
        /// <param name="pixelFormat">
        ///
        /// </param>
        private void SetDisplayablePixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }

            List <int>   attribIList = new List <int>();
            List <float> attribFList = new List <float>();

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
            uint[] countFormatAttribsValues       = new uint[1];
            int[]  choosenFormats = new int[4];

            // Let choose pixel formats
            if (!Wgl.ChoosePixelFormatARB(_DeviceContext, attribIList.ToArray(), attribFList.ToArray(), (uint)choosenFormats.Length, choosenFormats, countFormatAttribsValues))
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to choose pixel format: {0}", innerException.Message), innerException);
            }

            // Set choosen pixel format
            if (Wgl.SetPixelFormat(_DeviceContext, choosenFormats[0], ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to set pixel format {0}: {1}", pixelFormat.FormatIndex, innerException.Message), innerException);
            }
        }
Пример #2
0
        /// <summary>
        /// Create a simple context.
        /// </summary>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        internal override IntPtr CreateSimpleContext()
        {
            IntPtr rContext;

            // Define most compatible pixel format
            Wgl.PIXELFORMATDESCRIPTOR pfd = new Wgl.PIXELFORMATDESCRIPTOR(24);
            int  pFormat;
            bool res;

            // Find pixel format match
            pfd.dwFlags |= Wgl.PixelFormatDescriptorFlags.DepthDontCare | Wgl.PixelFormatDescriptorFlags.DoublebufferDontCare | Wgl.PixelFormatDescriptorFlags.StereoDontCare;

            pFormat = Wgl.ChoosePixelFormat(_DeviceContext, ref pfd);
            Debug.Assert(pFormat != 0);

            // Get exact description of the pixel format
            res = Wgl.DescribePixelFormat(_DeviceContext, pFormat, (uint)pfd.nSize, ref pfd);
            Debug.Assert(res);

            // Set pixel format before creating OpenGL context
            res = Wgl.SetPixelFormat(_DeviceContext, pFormat, ref pfd);
            Debug.Assert(res);

            // Create a dummy OpenGL context to retrieve initial informations.
            rContext = CreateContext(IntPtr.Zero);
            Debug.Assert(rContext != IntPtr.Zero);

            return(rContext);
        }
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }
            if (_PixelFormatSet == true)
            {
                throw new InvalidOperationException("pixel format already set");
            }

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

            // Note (from MSDN): 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.

            if (Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to describe pixel format, {0}", innerException.Message), innerException);
            }

            // Set choosen pixel format
            if (Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor) == false)
            {
                Win32Exception innerException = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(String.Format("unable to set pixel format, {0}", innerException.Message), innerException);
            }

            // Unable to set pixel format again
            _PixelFormatSet = true;
        }
Пример #4
0
        /// <summary>
        /// Set the device pixel format.
        /// </summary>
        /// <param name="pixelFormat">
        /// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="pixelFormat"/> is null.
        /// </exception>
        public override void SetPixelFormat(DevicePixelFormat pixelFormat)
        {
            if (pixelFormat == null)
            {
                throw new ArgumentNullException("pixelFormat");
            }
            if (_PixelFormatSet == true)
            {
                throw new InvalidOperationException("pixel format already set");
            }

            Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
            bool res;

            // Note (from MSDN): 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.

            res = Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor);
            Debug.Assert(res);

            // Set choosen pixel format
            res = Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor);
            Debug.Assert(res);

            // Unable to set pixel format again
            _PixelFormatSet = true;
        }
Пример #5
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");
			if (IsPixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

#if CHOOSE_PIXEL_FORMAT_FALLBACK
			try {
#endif
				Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

				// Note (from MSDN): 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.

				if (Wgl.DescribePixelFormat(_DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor) == 0)
					throw new InvalidOperationException(String.Format("unable to describe pixel format {0}", pixelFormat.FormatIndex), GetPlatformException());

				// Set choosen pixel format
				if (!Wgl.SetPixelFormat(_DeviceContext, pixelFormat.FormatIndex, ref pDescriptor))
					throw new InvalidOperationException(String.Format("unable to set pixel format {0}: {1}", pixelFormat.FormatIndex), GetPlatformException());

#if CHOOSE_PIXEL_FORMAT_FALLBACK
			} catch (InvalidOperationException) {
				// Try using default ChoosePixelFormat*
				SetDisplayablePixelFormat(pixelFormat);
			}
#endif

			IsPixelFormatSet = true;
		}
Пример #6
0
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		public override void ChoosePixelFormat(DevicePixelFormat pixelFormat)
		{
			if (IsPixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

			// Choose pixel format
			int pixelFormatIndex = ChoosePixelFormat(_DeviceContext, pixelFormat);
			
			// Set choosen pixel format
			Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();

			if (Wgl.SetPixelFormat(_DeviceContext, pixelFormatIndex, ref pDescriptor) == false)
				throw new InvalidOperationException("unable to set pixel format", GetPlatformException());

			IsPixelFormatSet = true;
		}
Пример #7
0
		private DevicePixelFormatCollection GetPixelFormats_Win32()
		{
			DevicePixelFormatCollection pixelFormats = new DevicePixelFormatCollection();
			Wgl.PIXELFORMATDESCRIPTOR pixelDescr = new Wgl.PIXELFORMATDESCRIPTOR();
			int pixelFormatsCount = Wgl.DescribePixelFormat(_DeviceContext, 0, 0, ref pixelDescr);

			for (int i = 1; i <= pixelFormatsCount; i++) {
				Wgl.DescribePixelFormat(_DeviceContext, i, (uint)Marshal.SizeOf(typeof(Wgl.PIXELFORMATDESCRIPTOR)), ref pixelDescr);

				if ((pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.SupportOpenGL) == 0)
					continue;

				DevicePixelFormat pixelFormat = new DevicePixelFormat();

				pixelFormat.FormatIndex = i;

				pixelFormat.RgbaUnsigned = true;
				pixelFormat.RgbaFloat = false;

				pixelFormat.RenderWindow = true;
				pixelFormat.RenderBuffer = false;

				pixelFormat.DoubleBuffer = (pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.Doublebuffer) != 0;
				pixelFormat.SwapMethod = 0;
				pixelFormat.StereoBuffer = (pixelDescr.dwFlags & Wgl.PixelFormatDescriptorFlags.Stereo) != 0;

				pixelFormat.ColorBits = pixelDescr.cColorBits;
				pixelFormat.DepthBits = pixelDescr.cDepthBits;
				pixelFormat.StencilBits = pixelDescr.cStencilBits;

				pixelFormat.MultisampleBits = 0;

				pixelFormat.RenderPBuffer = false;

				pixelFormat.SRGBCapable = false;

				pixelFormats.Add(pixelFormat);
			}

			return (pixelFormats);
		}
Пример #8
0
		/// <summary>
		/// Creates an OpenGL context from a Windows platform.
		/// </summary>
		/// <returns>
		/// A <see cref="IntPtr"/>
		/// </returns>
		private static IntPtr CreateWinSimpleContext(IDeviceContext rDevice)
		{
			WindowsDeviceContext winDeviceContext = (WindowsDeviceContext)rDevice;
			IntPtr rContext;

			// Define most compatible pixel format
			Wgl.PIXELFORMATDESCRIPTOR pfd = new Wgl.PIXELFORMATDESCRIPTOR(24);
			int pFormat;
			bool res;

			// Find pixel format match
			pfd.dwFlags |= Wgl.PixelFormatDescriptorFlags.DepthDontCare | Wgl.PixelFormatDescriptorFlags.DoublebufferDontCare | Wgl.PixelFormatDescriptorFlags.StereoDontCare;

			pFormat = Wgl.ChoosePixelFormat(winDeviceContext.DeviceContext, ref pfd);
			Debug.Assert(pFormat != 0);

			// Get exact description of the pixel format
			res = Wgl.DescribePixelFormat(winDeviceContext.DeviceContext, pFormat, (uint)pfd.nSize, ref pfd);
			Debug.Assert(res);

			// Set pixel format before creating OpenGL context
			res = Wgl.SetPixelFormat(winDeviceContext.DeviceContext, pFormat, ref pfd);
			Debug.Assert(res);

			// Create a dummy OpenGL context to retrieve initial informations.
			rContext = winDeviceContext.CreateContext(IntPtr.Zero);
			Debug.Assert(rContext != IntPtr.Zero);

			return (rContext);
		}
		/// <summary>
		/// Set the device pixel format.
		/// </summary>
		/// <param name="pixelFormat">
		/// A <see cref="DevicePixelFormat"/> that specifies the pixel format to set.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// Exception thrown if <paramref name="pixelFormat"/> is null.
		/// </exception>
		public override void SetPixelFormat(DevicePixelFormat pixelFormat)
		{
			if (pixelFormat == null)
				throw new ArgumentNullException("pixelFormat");
			if (_PixelFormatSet == true)
				throw new InvalidOperationException("pixel format already set");

			Wgl.PIXELFORMATDESCRIPTOR pDescriptor = new Wgl.PIXELFORMATDESCRIPTOR();
			bool res;

			// Note (from MSDN): 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.

			res = Wgl.DescribePixelFormat(DeviceContext, pixelFormat.FormatIndex, (uint)pDescriptor.nSize, ref pDescriptor);
			Debug.Assert(res);

			// Set choosen pixel format
			res = Wgl.SetPixelFormat(DeviceContext, pixelFormat.FormatIndex, ref pDescriptor);
			Debug.Assert(res);

			// Unable to set pixel format again
			_PixelFormatSet = true;
		}