Ejemplo n.º 1
0
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);

            gdi.PIXELFORMATDESCRIPTOR pfd = new gdi.PIXELFORMATDESCRIPTOR();
            pfd.nSize           = (ushort)Marshal.SizeOf(pfd);
            pfd.nVersion        = 1;
            pfd.dwFlags         = gdi.PFD_DRAW_TO_WINDOW | gdi.PFD_SUPPORT_OPENGL | gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType      = gdi.PFD_TYPE_RGBA;
            pfd.cColorBits      = 32;
            pfd.cRedBits        = 0;
            pfd.cRedShift       = 0;
            pfd.cGreenBits      = 0;
            pfd.cGreenShift     = 0;
            pfd.cBlueBits       = 0;
            pfd.cBlueShift      = 0;
            pfd.cAlphaBits      = 0;
            pfd.cAlphaShift     = 0;
            pfd.cAccumBits      = 0;
            pfd.cAccumRedBits   = 0;
            pfd.cAccumGreenBits = 0;
            pfd.cAccumBlueBits  = 0;
            pfd.cAccumAlphaBits = 0;
            pfd.cDepthBits      = 24;
            pfd.cStencilBits    = 8;
            pfd.cAuxBuffers     = 0;
            pfd.iLayerType      = 0;
            pfd.bReserved       = 0;
            pfd.dwLayerMask     = 0;
            pfd.dwVisibleMask   = 0;
            pfd.dwDamageMask    = 0;

            HDC = gdi.GetDC(Handle);
            if (HDC == IntPtr.Zero)
            {
                throw new Exception("OpenGLSurface error: Failed to get device context.");
            }
            int iPixelFormat = gdi.ChoosePixelFormat(HDC, out pfd);

            if (!gdi.SetPixelFormat(HDC, iPixelFormat, out pfd))
            {
                throw new Exception("OpenGLSurface error: Failed to set pixel format.");
            }
            HGLRC = wgl.CreateContext(HDC);
            if (HGLRC == IntPtr.Zero)
            {
                throw new Exception("OpenGLSurface error: Failed to create OpenGL device context.");
            }
            if (!MakeCurrent())
            {
                throw new Exception("OpenGLSurface error: Failed to make device context current.");
            }

            gl.Enable(GL.BLEND);
            gl.BlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
            gl.DepthMask(GL.FALSE);
            gl.MatrixMode(GL.PROJECTION);

            gl.LoadIdentity();
            gl.Ortho(-SurfaceSize.Width / 2f, SurfaceSize.Width / 2f,
                     -SurfaceSize.Height / 2f, SurfaceSize.Height / 2f, 0f, 1f);

            GLStart?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 2
0
        private void Worker(object args)
        {
            var Args  = (ThreadArgs)args;
            var HWND  = Args.HWND;
            var HDC   = IntPtr.Zero;
            var HGLRC = IntPtr.Zero;

            try
            {
                GLInit(HWND, out HDC, out HGLRC);

                gl.Enable(GL.BLEND);
                gl.BlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
                gl.DepthMask(GL.FALSE);
                gl.MatrixMode(GL.PROJECTION);

                gl.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
                gl.LoadIdentity();
                gl.Ortho(-SurfaceSize.Width / 2f, SurfaceSize.Width / 2f,
                         -SurfaceSize.Height / 2f, SurfaceSize.Height / 2f, 0f, 1f);

                GLStart?.Invoke(this, EventArgs.Empty);

                var FrameTimer = new Stopwatch();
                while (RendererRunning)
                {
                    if (FPS <= 0)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        var delay = 1000 / FPS - (int)FrameTimer.ElapsedMilliseconds;
                        if (delay > 0)
                        {
                            Thread.Sleep(delay);
                        }
                        FrameTimer.Restart();

                        lock (ActionRequests)
                            while (ActionRequests.Count > 0)
                            {
                                ActionRequests.Dequeue().Invoke();
                            }

                        if (!NoClear)
                        {
                            gl.ClearColor(BackColor.R / 256f, BackColor.G / 256f,
                                          BackColor.B / 256f, BackColor.A / 256f);
                            gl.Clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
                        }
                        GLPaint?.Invoke(this, EventArgs.Empty);

                        gdi.SwapBuffers(HDC);
                    }
                }
            }
            finally
            {
                GLStop?.Invoke(this, EventArgs.Empty);

                GLDispose(HWND, HDC, HGLRC);
            }
        }