Exemple #1
0
        /// <summary>
        /// Create Context using user setting.
        /// </summary>
        /// <param name="c">Control to host the rendering context</param>
        /// <param name="setting">Context setting parameters</param>
        /// <returns>Rendering context</returns>
        public static new Context CreateContext(IntPtr hWnd, ContextSettings setting)
        {
            ContextWin rc = new ContextWin(setting, hWnd);

            rc.CreateOldContext();

            // set highest context major version
            if (setting.MajorVersion == 0xff)
            {
                setting.MajorVersion = (byte)GetInteger(GL.MAJOR_VERSION);
            }

            // set highest context minor version
            if (setting.MinorVersion == 0xff)
            {
                setting.MinorVersion = (byte)GetInteger(GL.MINOR_VERSION);
            }

            // create new context using ARB_create_context
            if (setting.MajorVersion >= 3)
            {
                rc.CreateNewContext();
            }
            else
            {
                setting.Profile = ContextProfile.None;
                setting.Flags   = ContextFlags.None;
            }

            setting.MajorVersion = (byte)GetInteger(GL.MAJOR_VERSION);
            setting.MinorVersion = (byte)GetInteger(GL.MINOR_VERSION);

            return(rc);
        }
Exemple #2
0
        /// <summary>
        /// Creates a context using custom settings.
        /// </summary>
        /// <param name="hWnd">The window handle to create the context on.</param>
        /// <param name="settings">The custom context settings.</param>
        /// <returns>Context</returns>
        public static Context CreateContext(IntPtr hWnd, ContextSettings settings)
        {
            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
            case PlatformID.Win32Windows:
                return(ContextWin.CreateContext(hWnd, settings));

            default:
                throw new PlatformNotSupportedException();
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a pre OpenGL 3.0 context.
        /// </summary>
        private void CreateOldContext()
        {
            if (Settings.Multisample == 0)
            {
                // create non-multisample context
                CreateBaseContext();
                MakeCurrent();

                return;
            }

            // Replace Windows.Form with custom window library implementation.
            Form       tmpform = new Form();
            ContextWin tmprc   = new ContextWin(Settings, tmpform.Handle);

            tmprc.CreateBaseContext();

            // test multisampling and pixel format possibilities
            if (!(WGLExtension.hasARB_pixel_format || WGLExtension.hasEXT_pixel_format))
            {
                // if the maximum multisample should be used on non-multisample device, create a simple context without multisample.
                // if the number of samples is specified strictly on non-multisample device throw an exception.
                if (Settings.Multisample != 0xFF)
                {
                    throw new GLException(string.Format("ARB_pixel_format nor EXT_pixel_format is not supported."));
                }

                CreateBaseContext();
                MakeCurrent();

                return;
            }

            tmprc.Dispose();
            tmpform.Close();

            int[]   pixelFormat = new int[100];
            uint[]  numFormats  = new uint[1];
            float[] fAttributes = new float[] { 0, 0 };

            hDC = User32.GetDC(hWnd);

            if (Settings.Multisample == 0xFF)
            {
                Settings.Multisample = (byte)GetInteger(GL.MAX_SAMPLES_EXT);
            }

            if (Settings.Multisample > 0)             // Number of samples was specified or the HW supports MAX_SAMPLES_EXT for multisample = 0xFF
            {
                int[] attributes = GetAttributes(true);
                bool  valid      = wgl.ChoosePixelFormat(hDC, attributes, fAttributes, 1, pixelFormat, numFormats);

                if (valid && (numFormats[0] > 0))
                {
                    if (!Gdi32.SetPixelFormat(hDC, pixelFormat[0], ref pfd))
                    {
                        throw GetException("SetPixelFormat failed");
                    }
                }
            }
            else             // Does not support MAX_SAMPLES_EXT
            {
                int[] attributes = GetAttributes(false);
                bool  valid      = wgl.ChoosePixelFormat(hDC, attributes, fAttributes, 100, pixelFormat, numFormats);

                if (valid && (numFormats[0] > 0))
                {
                    int   maxSamples       = 0;
                    int   maxSamplesFormat = 0;
                    int[] piAttributes     = { WGL.SAMPLES_ARB };
                    int[] piValues         = new int[1];

                    for (uint i = 0; i < Math.Min(100, numFormats[0]); i++)
                    {
                        wgl.GetPixelFormatAttribiv(hDC, pixelFormat[i], 0, 1, piAttributes, piValues);

                        if (piValues[0] > maxSamples)
                        {
                            maxSamples       = piValues[0];
                            maxSamplesFormat = (int)i;
                        }
                    }

                    Settings.Multisample = (byte)maxSamples;

                    if (!Gdi32.SetPixelFormat(hDC, pixelFormat[maxSamplesFormat], ref pfd))
                    {
                        throw GetException("SetPixelFormat failed");
                    }
                }
                else
                {
                    CreateBaseContext();
                    MakeCurrent();

                    return;
                }
            }

            hRC = wgl.CreateContext(hDC);
            MakeCurrent();
        }