Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 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();

            // 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;
        }
Ejemplo n.º 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;
		}
Ejemplo n.º 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;
		}