Пример #1
0
        public void SetRenderingWindow(IWindow window)
        {
            if (hglrc == IntPtr.Zero)
            {
                throw new InvalidOperationException("OpenGL context hasn't been created.");
            }

            var dc = GetDC(window.Pointer);

            hDC = dc;

            //For OpenGL, we need to set pixel format for each win32 window before make it current.

            if (GetPixelFormat(dc) == 0)//Set a valid pixel format if haven't.
            {
                var pixelformatdescriptor = new PIXELFORMATDESCRIPTOR();
                pixelformatdescriptor.Init();

                if (!Application.EnableMSAA)
                {
                    int pixelFormat = ChoosePixelFormat(dc, ref pixelformatdescriptor);
                    SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor);
                }
                else
                {
                    int[] iPixAttribs =
                    {
                        (int)WGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE,
                        (int)WGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE,
                        (int)WGL.WGL_DOUBLE_BUFFER_ARB,  (int)GL.GL_TRUE,
                        (int)WGL.WGL_PIXEL_TYPE_ARB,     (int)WGL.WGL_TYPE_RGBA_ARB,
                        (int)WGL.WGL_ACCELERATION_ARB,   (int)WGL.WGL_FULL_ACCELERATION_ARB,
                        (int)WGL.WGL_COLOR_BITS_ARB,                                     24,
                        (int)WGL.WGL_ALPHA_BITS_ARB,                                      8,
                        (int)WGL.WGL_DEPTH_BITS_ARB,                                     24,
                        (int)WGL.WGL_STENCIL_BITS_ARB,                                    8,
                        (int)WGL.WGL_SWAP_METHOD_ARB,    (int)WGL.WGL_SWAP_EXCHANGE_ARB,
                        (int)WGL.WGL_SAMPLE_BUFFERS_ARB, (int)GL.GL_TRUE,   //Enable MSAA
                        (int)WGL.WGL_SAMPLES_ARB,                                        16,
                        0
                    };

                    int  pixelFormat;
                    uint numFormats;
                    var  result1 = Wgl.ChoosePixelFormatARB(dc, iPixAttribs, null, 1, out pixelFormat,
                                                            out numFormats);
                    if (result1 == false || numFormats == 0)
                    {
                        throw new Exception(
                                  $"wglChoosePixelFormatARB failed: error {Marshal.GetLastWin32Error()}");
                    }

                    if (!DescribePixelFormat(dc, pixelFormat, (uint)Marshal.SizeOf <PIXELFORMATDESCRIPTOR>(),
                                             ref pixelformatdescriptor))
                    {
                        throw new Exception(
                                  $"DescribePixelFormat failed: error {Marshal.GetLastWin32Error()}");
                    }

                    if (!SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor))
                    {
                        throw new Exception(
                                  $"SetPixelFormat failed: error {Marshal.GetLastWin32Error()}");
                    }
                }
            }

            var result = Wgl.MakeCurrent(dc, hglrc);

            if (!result)
            {
                var lastError = Native.GetLastErrorString();
                PrintPixelFormat(dc);
                throw new InvalidOperationException($"Wgl.MakeCurrent failed, error: {lastError}");
            }

            var viewportSize = window.ClientSize;

            GL.Viewport(0, 0, (int)viewportSize.Width, (int)viewportSize.Height);
            GL.Scissor(0, 0, (int)viewportSize.Width, (int)viewportSize.Height);
        }