コード例 #1
0
ファイル: ContextBuilder.cs プロジェクト: GeirGrusom/ModGL
        public IntPtr BuildModernContext(ContextCreationParameters contextParameters, ILibrary library, IContext sharedContext, IntPtr legacyContextHandle)
        {
            if (!_wgl.wglMakeCurrent((IntPtr)contextParameters.Device, legacyContextHandle))
                throw new ContextCreationException("Unable to make temporary context current.", contextParameters);


            var choosePixelFormat = library.GetProcedure<wglChoosePixelFormatARB>("wglChoosePixelFormatARB");
            var createContext = library.GetProcedure<wglCreateContextAttribsARB>("wglCreateContextAttribsARB");

            if (choosePixelFormat == null)
                throw new MissingEntryPointException("wglChoosePixelFormatARB", library);

            if (createContext == null)
                throw new MissingEntryPointException("wglCreateContextAttribsARB", library);

            var formats = new int[1];
            var numFormats = new uint[1];

            var pixelFormatParameters = new[]
                    {
                        WindowsContext.WGLPixelFormatConstants.WGL_DRAW_TO_WINDOW_ARB, (int) GLboolean.True,
                        WindowsContext.WGLPixelFormatConstants.WGL_SUPPORT_OPENGL_ARB, (int) GLboolean.True,
                        WindowsContext.WGLPixelFormatConstants.WGL_DOUBLE_BUFFER_ARB, (int) GLboolean.True,
                        WindowsContext.WGLPixelFormatConstants.WGL_PIXEL_TYPE_ARB, WindowsContext.WGLPixelFormatConstants.WGL_TYPE_RGBA_ARB,
                        WindowsContext.WGLPixelFormatConstants.WGL_COLOR_BITS_ARB, contextParameters.ColorBits.HasValue ? contextParameters.ColorBits.Value : 32,
                        WindowsContext.WGLPixelFormatConstants.WGL_DEPTH_BITS_ARB, contextParameters.DepthBits.HasValue ? contextParameters.DepthBits.Value : 24,
                        WindowsContext.WGLPixelFormatConstants.WGL_STENCIL_BITS_ARB, contextParameters.StencilBits.HasValue ? contextParameters.StencilBits.Value : 8,
                        WindowsContext.WGLPixelFormatConstants.WGL_SAMPLE_BUFFERS_ARB, 1,
                        WindowsContext.WGLPixelFormatConstants.WGL_SAMPLES_ARB, 4,
                        0 //End 
                    };

            if (!choosePixelFormat((IntPtr)contextParameters.Device, pixelFormatParameters, null, 1, formats, numFormats))
            {
                throw new ContextCreationException("Unable to choose pixel format.", contextParameters);
            }

            var finalContext = createContext((IntPtr)contextParameters.Device, sharedContext != null ? sharedContext.Handle : IntPtr.Zero, new[]
            {
                (int)WGLContextAttributes.MajorVersion, contextParameters.MajorVersion.HasValue ? contextParameters.MajorVersion.Value : 3,
                (int)WGLContextAttributes.MinorVersion, contextParameters.MinorVersion.HasValue ? contextParameters.MinorVersion.Value : 2,
                (int)WGLContextAttributes.Flags, 0,
                (int)WGLContextAttributes.ProfileMask, (int)WGLContextProfileMask.CoreProfileBit,
                0
            }
            );

            if(finalContext == IntPtr.Zero)
                throw new ContextCreationException("Unable to create OpenGL 3.0 context.", contextParameters);



            return finalContext;
        }