/// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The extra parameter.</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //  Get the desktop DC.
            IntPtr desktopDC = Win32.GetDC(IntPtr.Zero);

            //  Create our DC as a compatible DC for the desktop.
            deviceContextHandle = Win32.CreateCompatibleDC(desktopDC);

            //  Release the desktop DC.
            Win32.ReleaseDC(IntPtr.Zero, desktopDC);

            //  Create our dib section.
            dibSection.Create(deviceContextHandle, width, height, bitDepth);

            //  Create the render context.
            Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);

            //  Make current.
            MakeCurrent();

            //  Update the context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return(true);
        }
Exemplo n.º 2
0
        public OGLDevice()
        {
            InitializeComponent();
            _glVersion           = OpenGLVersion.OpenGL2_1;
            _glRenderContextType = RenderContextType.NativeWindow;

            ResizeRedraw = false;
            MinimumSize  = new Size(1, 1);
            SetStyle(ControlStyles.DoubleBuffer, false);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, false);    // а надо ли
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.ResizeRedraw, false);

            _Thread          = new Thread(RenderLoop);
            glContext        = new QueueSynchronizationContext(_Thread);
            uiContext        = SynchronizationContext.Current; // NOTE: Сделанно допущение, что объект всегда создается из потока UI
            HandleDestroyed += (sender, args) => {
                if (Closed != null)
                {
                    lock (LockObj) DeviceUpdate = null; // Нет смысла вызывать обновление устройства после обратки события
                    glContext.Send(d => Closed(sender, new OpenGLEventArgs(OpenGL)), null);
                }
                _Thread.Abort();
                lock (LockObj);
            };
            _Thread.Start();
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The extra parameter.</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //  Get the desktop DC.
            IntPtr desktopDC = Win32.GetDC(IntPtr.Zero);

            //  Create our DC as a compatible DC for the desktop.
            deviceContextHandle = Win32.CreateCompatibleDC(desktopDC);

            //  Release the desktop DC.
            Win32.ReleaseDC(IntPtr.Zero, desktopDC);

            //  Create our dib section.
            dibSection.Create(deviceContextHandle, width, height, bitDepth);

            //  Create the render context.
            Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);

            //  Make current.
            MakeCurrent();

            //  Update the context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return true;
        }
Exemplo n.º 4
0
        private void CreateDBOs(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            uint[] ids = new uint[1];

            //  First, create the frame buffer and bind it.
            ids = new uint[1];
            gl.GenFramebuffersEXT(1, ids);
            frameBufferID = ids[0];
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);

            //	Create the colour render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            colourRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height);

            //	Create the depth render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            depthRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, width, height);

            //  Set the render buffer for colour and depth.
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);

            dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle);

            //  Create the DIB section.
            dibSection.Create(dibSectionDeviceContext, width, height, bitDepth);
        }
Exemplo n.º 5
0
 public ClydeDebugInfo(OpenGLVersion openGLVersion, string renderer, string vendor, string versionString, bool overriding)
 {
     OpenGLVersion = openGLVersion;
     Renderer      = renderer;
     Vendor        = vendor;
     VersionString = versionString;
     Overriding    = overriding;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">A valid Window Handle must be provided for the NativeWindowRenderContextProvider</exception>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //  Cast the parameter to the device context.
            try
            {
                windowHandle = (IntPtr)parameter;
            }
            catch
            {
                throw new Exception("A valid Window Handle must be provided for the NativeWindowRenderContextProvider");
            }

            //	Get the window device context.
            deviceContextHandle = Win32.GetDC(windowHandle);

            //	Setup a pixel format.
            Win32.PIXELFORMATDESCRIPTOR pfd = new Win32.PIXELFORMATDESCRIPTOR();
            pfd.Init();
            pfd.nVersion = 1;
            pfd.dwFlags  = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER
                           | Win32.PFD_GENERIC_ACCELERATED | Win32.PFD_SWAP_EXCHANGE;
            pfd.iPixelType   = Win32.PFD_TYPE_RGBA;
            pfd.cColorBits   = (byte)bitDepth;
            pfd.cDepthBits   = 8;//16;
            pfd.cStencilBits = 8;
            pfd.iLayerType   = Win32.PFD_MAIN_PLANE;
            //pfd.cAccumBits = 32;

            //	Match an appropriate pixel format
            int iPixelformat;

            if ((iPixelformat = Win32.ChoosePixelFormat(deviceContextHandle, pfd)) == 0)
            {
                return(false);
            }

            //	Sets the pixel format
            if (Win32.SetPixelFormat(deviceContextHandle, iPixelformat, pfd) == 0)
            {
                return(false);
            }

            //	Create the render context.
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);

            //  Make the context current.
            MakeCurrent();

            //  Update the render context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            CreateDBOs(openGLVersion, gl, width, height, bitDepth, parameter);

            return(true);
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, int width, int height, int bitDepth, object parameter)
        {
//            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, width, height, bitDepth, parameter);

            uint[] ids = new uint[1];

            //  First, create the frame buffer and bind it.
            ids = new uint[1];

////            gl.GenFramebuffersEXT(1, ids);
            GL.GenFramebuffers(1, ids);
            frameBufferID = ids[0];
////            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);
            GL.BindFramebuffer(FramebufferTarget.FramebufferExt, frameBufferID);
            //	Create the colour render buffer and bind it, then allocate storage for it.

////		    gl.GenRenderbuffersEXT(1, ids);
            GL.GenRenderbuffers(1, ids);
            colourRenderBufferID = ids[0];
////		    gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, colourRenderBufferID);

////            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height);
            GL.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.Rgba8, width, height);

            //	Create the depth render buffer and bind it, then allocate storage for it.
////            gl.GenRenderbuffersEXT(1, ids);
            GL.GenRenderbuffers(1, ids);

            depthRenderBufferID = ids[0];
////            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);
            GL.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, depthRenderBufferID);

////            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, width, height);
            GL.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent24, width, height);

            //  Set the render buffer for colour and depth.
////            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT,OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
////            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT,OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);

            GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, RenderbufferTarget.RenderbufferExt, colourRenderBufferID);
            GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, RenderbufferTarget.RenderbufferExt, depthRenderBufferID);

            dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle);

            //  Create the DIB section.
            dibSection.Create(dibSectionDeviceContext, width, height, bitDepth);

            return(true);
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">A valid Window Handle must be provided for the NativeWindowRenderContextProvider</exception>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //  Cast the parameter to the device context.
            try
            {
                windowHandle = (IntPtr)parameter;
            }
            catch
            {
                throw new Exception("A valid Window Handle must be provided for the NativeWindowRenderContextProvider");
            }

            //	Get the window device context.
            deviceContextHandle = Win32.GetDC(windowHandle);

            //	Setup a pixel format.
            Win32.PIXELFORMATDESCRIPTOR pfd = new Win32.PIXELFORMATDESCRIPTOR();
            pfd.Init();
            pfd.nVersion = 1;
            pfd.dwFlags = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER;
            pfd.iPixelType = Win32.PFD_TYPE_RGBA;
            pfd.cColorBits = (byte)bitDepth;
            pfd.cDepthBits = 16;
            pfd.cStencilBits = 8;
            pfd.iLayerType = Win32.PFD_MAIN_PLANE;

            //	Match an appropriate pixel format
            int iPixelformat;
            if((iPixelformat = Win32.ChoosePixelFormat(deviceContextHandle, pfd)) == 0 )
                return false;

            //	Sets the pixel format
            if (Win32.SetPixelFormat(deviceContextHandle, iPixelformat, pfd) == 0)
            {
                return false;
            }

            //	Create the render context.
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);

            //  Make the context current.
            MakeCurrent();

            //  Update the render context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return true;
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            uint[] ids = new uint[1];

            //  First, create the frame buffer and bind it.
            gl.GenFramebuffersEXT(1, ids);
            gl.ThrowIfErrors();
            frameBufferID = ids[0];
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);
            gl.ThrowIfErrors();

            //    Create the colour render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            gl.ThrowIfErrors();
            colourRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.ThrowIfErrors();
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height);
            gl.ThrowIfErrors();

            //    Create the depth stencil render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            gl.ThrowIfErrors();
            depthStencilRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID);
            gl.ThrowIfErrors();
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH24_STENCIL8, width, height);
            gl.ThrowIfErrors();

            //  Set the render buffer for colour, depth and stencil.
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.ThrowIfErrors();
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID);
            gl.ThrowIfErrors();
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_STENCIL_ATTACHMENT_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID);
            gl.ThrowIfErrors();
            ValidateFramebufferStatus(gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT));

            dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle);

            //  Create the DIB section.
            dibSection.Create(dibSectionDeviceContext, width, height, bitDepth);
            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The extra parameter.</param>
        /// <returns></returns>
        public virtual bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
	        //  Set the width, height and bit depth.
            Width = width;
            Height = height;
            BitDepth = bitDepth;

            //  For now, assume we're going to be able to create the requested OpenGL version.
            requestedOpenGLVersion = openGLVersion;
            createdOpenGLVersion = openGLVersion;

            return true;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The extra parameter.</param>
        /// <returns></returns>
        public virtual bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Set the width, height and bit depth.
            Width    = width;
            Height   = height;
            BitDepth = bitDepth;

            //  For now, assume we're going to be able to create the requested OpenGL version.
            requestedOpenGLVersion = openGLVersion;
            createdOpenGLVersion   = openGLVersion;

            return(true);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Creates the render context provider with sharing lists of another render context.
        /// Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <param name="hrc">Render context handle to share lists with</param>
        /// <returns></returns>
        public bool CreateWithShareLists(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter, IntPtr hrc)
        {
            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            bool shareStatus = Win32.wglShareLists(hrc, this.RenderContextHandle);

            CreateDBOs(openGLVersion, gl, width, height, bitDepth, parameter);

            return(shareStatus);
        }
        public OGLViewport(OpenGLVersion version = OpenGLVersion.OpenGL2_1, RenderContextType contextType = SharpGL.RenderContextType.FBO)
        {
            OglVersion = version;
            RenderContextType = contextType;

            //  Lock on OpenGL.
            lock (Gl)
            {
                //  Create OpenGL.
                Gl.Create(OglVersion, RenderContextType, 1, 1, 32, null);
            }

            Gl.Enable(OpenGL.GL_DEPTH_TEST);
        }
        public OGLViewport(OpenGLVersion version = OpenGLVersion.OpenGL2_1, RenderContextType contextType = SharpGL.RenderContextType.FBO)
        {
            OglVersion        = version;
            RenderContextType = contextType;

            //  Lock on OpenGL.
            lock (Gl)
            {
                //  Create OpenGL.
                Gl.Create(OglVersion, RenderContextType, 1, 1, 32, null);
            }

            Gl.Enable(OpenGL.GL_DEPTH_TEST);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            uint[] ids = new uint[1];
            // Multi sampled fbo
            gl.GenFramebuffersEXT(1, ids);
            gl.ThrowIfErrors();
            msFrameBufferID = ids[0];
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, msFrameBufferID);
            gl.ThrowIfErrors();

            // Create the colour render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            gl.ThrowIfErrors();
            msColourRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, msColourRenderBufferID);
            gl.ThrowIfErrors();
            gl.RenderbufferStorageMultisampleEXT(OpenGL.GL_RENDERBUFFER_EXT, MSAA, OpenGL.GL_RGBA, width, height);
            gl.ThrowIfErrors();
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_RENDERBUFFER_EXT, msColourRenderBufferID);
            gl.ThrowIfErrors();

            //    Create the depth stencil render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            gl.ThrowIfErrors();
            msDepthStencilRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID);
            gl.ThrowIfErrors();
            gl.RenderbufferStorageMultisampleEXT(OpenGL.GL_RENDERBUFFER_EXT, MSAA, OpenGL.GL_DEPTH24_STENCIL8, width, height);
            gl.ThrowIfErrors();

            //  Set the render buffer for depth and stencil.
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID);
            gl.ThrowIfErrors();
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_STENCIL_ATTACHMENT_EXT,
                                          OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID);
            gl.ThrowIfErrors();

            ValidateFramebufferStatus(gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT));
            return(true);
        }
        public GLWindowsContext(IHost viewHost,
                                OpenGLVersion glVersion,
                                GLWindowBuilder windowBuilder,
                                Task <IntPtr> deviceContextPromise,
                                Byte bitDepth = 32)
        {
            _dibSection    = new DIBSection();
            _delegateCache = new DelegateCache();
            _currentSize   = new Size(0, 0);
            _windowBuilder = windowBuilder;

            _viewHost = viewHost;
            _viewHost.AvailableSizeChanged += OnHostSizeChanged;

            _bitDepth = bitDepth;
            _requestedOpenGLVersion = glVersion;

            deviceContextPromise.ContinueWith(OnDeviceContextAvailable);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            // Request an id for our pixel buffer.
            uint[] ids = new uint[1];

            ids = new uint[1];
            gl.GenBuffers(1, ids);

            pixelBufferID = ids[0];

            // The writeable bitmap needs to be initialized, so trigger setDimensions for this.
            SetDimensions(width, height);


            return(true);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            this.gl = gl;

            //  Call the base class. 	        
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            uint[] ids = new uint[1];

            //  First, create the frame buffer and bind it.
            ids = new uint[1];
            gl.GenFramebuffersEXT(1, ids);
            frameBufferID = ids[0];
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID);
                        
		    //	Create the colour render buffer and bind it, then allocate storage for it.
		    gl.GenRenderbuffersEXT(1, ids);
            colourRenderBufferID = ids[0];
		    gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height);

            //	Create the depth render buffer and bind it, then allocate storage for it.
            gl.GenRenderbuffersEXT(1, ids);
            depthRenderBufferID = ids[0];
            gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);
            gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, width, height);

            //  Set the render buffer for colour and depth.
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT,
                OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID);
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT,
                OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID);

            dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle);
		
            //  Create the DIB section.
            dibSection.Create(dibSectionDeviceContext, width, height, bitDepth);
            
            return true;
	    }
Exemplo n.º 20
0
        private void InitOpenGL()
        {
            var vendor   = GL.GetString(StringName.Vendor);
            var renderer = GL.GetString(StringName.Renderer);
            var version  = GL.GetString(StringName.Version);
            var major    = GL.GetInteger(GetPName.MajorVersion);
            var minor    = GL.GetInteger(GetPName.MinorVersion);

            _sawmillOgl.Debug("OpenGL Vendor: {0}", vendor);
            _sawmillOgl.Debug("OpenGL Renderer: {0}", renderer);
            _sawmillOgl.Debug("OpenGL Version: {0}", version);

            var overrideVersion = ParseGLOverrideVersion();

            if (overrideVersion != null)
            {
                (major, minor) = overrideVersion.Value;
                _sawmillOgl.Debug("OVERRIDING detected GL version to: {0}.{1}", major, minor);
            }

            DetectOpenGLFeatures(major, minor);
            SetupDebugCallback();

            LoadVendorSettings(vendor, renderer, version);

            var glVersion = new OpenGLVersion((byte)major, (byte)minor, _isGLES, _isCore);

            DebugInfo = new ClydeDebugInfo(glVersion, renderer, vendor, version, overrideVersion != null);

            GL.Enable(EnableCap.Blend);
            if (_hasGLSrgb)
            {
                GL.Enable(EnableCap.FramebufferSrgb);
                CheckGlError();
            }
            if (_hasGLPrimitiveRestart)
            {
                GL.Enable(EnableCap.PrimitiveRestart);
                CheckGlError();
                GL.PrimitiveRestartIndex(PrimitiveRestartIndex);
                CheckGlError();
            }
            if (!HasGLAnyVertexArrayObjects)
            {
                _sawmillOgl.Warning("NO VERTEX ARRAY OBJECTS! Things will probably go terribly, terribly wrong (no fallback path yet)");
            }

            ResetBlendFunc();

            CheckGlError();

            // Primitive Restart's presence or lack thereof changes the amount of required memory.
            InitRenderingBatchBuffers();

            _sawmillOgl.Debug("Loading stock textures...");

            LoadStockTextures();

            _sawmillOgl.Debug("Loading stock shaders...");

            LoadStockShaders();

            _sawmillOgl.Debug("Creating various GL objects...");

            CreateMiscGLObjects();

            _sawmillOgl.Debug("Setting up RenderHandle...");

            _renderHandle = new RenderHandle(this);

            _sawmillOgl.Debug("Setting viewport and rendering splash...");

            GL.Viewport(0, 0, ScreenSize.X, ScreenSize.Y);
            CheckGlError();

            // Quickly do a render with _drawingSplash = true so the screen isn't blank.
            Render();
        }
Exemplo n.º 21
0
        /// <summary>
        /// Only valid to be called after the render context is created, this function attempts to
        /// move the render context to the OpenGL version originally requested. If this is &gt; 2.1, this
        /// means building a new context. If this fails, we'll have to make do with 2.1.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        protected void UpdateContextVersion(OpenGL gl)
        {
            //  If the request version number is anything up to and including 2.1, standard render contexts
            //  will provide what we need (as long as the graphics card drivers are up to date).
            var requestedVersionNumber = VersionAttribute.GetVersionAttribute(requestedOpenGLVersion);
            if (requestedVersionNumber.IsAtLeastVersion(3, 0) == false)
            {
                createdOpenGLVersion = requestedOpenGLVersion;
                return;
            }

            //  Now the none-trivial case. We must use the WGL_ARB_create_context extension to
            //  attempt to create a 3.0+ context.
            try
            {
                int[] attributes =
                {
                    OpenGL.WGL_CONTEXT_MAJOR_VERSION_ARB, requestedVersionNumber.Major,
                    OpenGL.WGL_CONTEXT_MINOR_VERSION_ARB, requestedVersionNumber.Minor,
                    OpenGL.WGL_CONTEXT_FLAGS_ARB, OpenGL.WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
                    0
                };
                var hrc = gl.CreateContextAttribsARB(IntPtr.Zero, attributes);
                Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                Win32.wglDeleteContext(renderContextHandle);
                Win32.wglMakeCurrent(deviceContextHandle, hrc);
                renderContextHandle = hrc;
            }
            catch(Exception)
            {
                //  TODO: can we actually get the real version?
                createdOpenGLVersion = OpenGLVersion.OpenGL2_1;
            }
        }
Exemplo n.º 22
0
        /// <summary>
        /// Creates the OpenGL instance using an external, existing render context.
        /// </summary>
        /// <param name="openGLVersion">The OpenGL version requested.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="windowHandle">The window handle.</param>
        /// <param name="renderContextHandle">The render context handle.</param>
        /// <param name="deviceContextHandle">The device context handle.</param>
        /// <returns>
        /// True on success
        /// </returns>
        public virtual bool CreateFromExternalContext(OpenGLVersion openGLVersion, int width, int height, int bitDepth, IntPtr windowHandle, IntPtr renderContextHandle, IntPtr deviceContextHandle)
        {
            // Return if we don't have a sensible width or height.
            if (width == 0 || height == 0 || bitDepth == 0)
            {
                return false;
            }

            renderContextProvider = new ExternalRenderContextProvider(windowHandle, renderContextHandle, deviceContextHandle);
            renderContextProvider.Create(openGLVersion, this, width, height, bitDepth, null);

            return true;
        }
Exemplo n.º 23
0
        /// <summary>
        /// Creates the OpenGL instance.
        /// </summary>
        /// <param name="openGLVersion">The OpenGL version requested.</param>
        /// <param name="renderContextType">Type of the render context.</param>
        /// <param name="width">The drawing context width.</param>
        /// <param name="height">The drawing context height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        public virtual bool Create(OpenGLVersion openGLVersion, RenderContextType renderContextType, int width, int height, int bitDepth, object parameter)
        {
            //  Return if we don't have a sensible width or height.
            if(width == 0 || height == 0 || bitDepth == 0)
                return false;

            // TODO: [RS] I suggest to replace the switch-case statement with a RenderContextProviderFactory.

            //	Create an instance of the render context provider.
            switch (renderContextType)
            {
                case RenderContextType.DIBSection:
                    renderContextProvider = new DIBSectionRenderContextProvider();
                    break;
                case RenderContextType.NativeWindow:
                    renderContextProvider = new NativeWindowRenderContextProvider();
                    break;
                case RenderContextType.HiddenWindow:
                    renderContextProvider = new HiddenWindowRenderContextProvider();
                    break;
                case RenderContextType.FBO:
                    renderContextProvider = new FBORenderContextProvider();
                    break;
            }

            //	Create the render context.
            renderContextProvider.Create(openGLVersion, this, width, height, bitDepth, parameter);

            return true;
        }
Exemplo n.º 24
0
        private void RenderLoop()
        {
            var queuecontext = glContext as QueueSynchronizationContext;

            SynchronizationContext.SetSynchronizationContext(glContext);

            int fps_count = 0;
            int fps_ticks = 0;
            int pre_ticks = Environment.TickCount & Int32.MaxValue;
            var updatearg = new OGLDeviceUpdateArgs(gl);

            int width = -1, height = -1; IntPtr handle = IntPtr.Zero;

            uiContext.Send(d => {   // WindowsFormsSynchronizationContext обрабатывает синхронные сообщения в
                width  = Width;     // общем обработчике сообщений окна (WndProc). Это означает, что главный
                height = Height;    // поток обратает синхронное сообщение лишь после того как завершится
                handle = Handle;    // выполнение текущего кода. Таким образом в неявном виде тут происходит
            }, null);               // синхронизация потоков - поток ожидаяет выполнения OnMainWindowLoad()

            // Создание и работа с устройством OpenGL должны осуществляться в одном потоке
            gl.Create(_glVersion, _glRenderContextType, width, height, 32, handle);
            _glVersion = ((RenderContextProvider)gl.RenderContextProvider).CreatedOpenGLVersion;

            // Установка наиболее распространенных стилей
            gl.ShadeModel(OpenGL.GL_SMOOTH);

            gl.ClearColor(0.5f, 0.5f, 0.5f, 0.0f);
            gl.ClearDepth(1.0f);
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.DepthFunc(OpenGL.GL_LEQUAL);

            gl.Enable(OpenGL.GL_BLEND);                                       // Правило наложения (смешивания) цветов
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); // result_color = src * src.a + dst * (1 - src.a)

            Initialize(Width, Height, false);                                 // задаем проецирование
            OpenGL.MakeCurrent();

            if (Inited != null)
            {
                Inited(this, new OpenGLEventArgs(gl));
            }

            while (true)
            {
                queuecontext.Run();
                if (ogl_resize_request_param != -1)
                {
                    Initialize(ogl_resize_request_param & 0xFFFF, ogl_resize_request_param >> 16, false);
                    ogl_resize_request_param = -1;
                }

                var cur_ticks = Environment.TickCount & Int32.MaxValue;
                if (cur_ticks - fps_ticks > 1000)
                {
                    uiContext.Post(s => {
                        if (ParentForm != null)
                        {
                            ParentForm.Text = String.Format("FPS: {0}", (int)s);
                        }
                    }, fps_count);
                    fps_count = 1;
                    fps_ticks = cur_ticks;
                }
                else
                {
                    ++fps_count;
                }

                lock (LockObj) {
                    if (DeviceUpdate != null)
                    {
                        DeviceUpdate(this, updatearg.Update(cur_ticks - pre_ticks));

                        gl.Flush();
                        OpenGL.Blit(handle);    // Для ContextType.NativeWindow тут выполняется переключение буфферов,
                        pre_ticks = cur_ticks;  // сам же параметр handle при этом вообще никак и ни где не используется.
                    }
                }

                Thread.Sleep(1);
            }
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //	Create a new window class, as basic as possible.
            Win32.WNDCLASSEX wndClass = new Win32.WNDCLASSEX();
            wndClass.Init();
            wndClass.style         = Win32.ClassStyles.HorizontalRedraw | Win32.ClassStyles.VerticalRedraw | Win32.ClassStyles.OwnDC;
            wndClass.lpfnWndProc   = wndProcDelegate;
            wndClass.cbClsExtra    = 0;
            wndClass.cbWndExtra    = 0;
            wndClass.hInstance     = IntPtr.Zero;
            wndClass.hIcon         = IntPtr.Zero;
            wndClass.hCursor       = IntPtr.Zero;
            wndClass.hbrBackground = IntPtr.Zero;
            wndClass.lpszMenuName  = null;
            wndClass.lpszClassName = "SharpGLRenderWindow";
            wndClass.hIconSm       = IntPtr.Zero;
            Win32.RegisterClassEx(ref wndClass);

            //	Create the window. Position and size it.
            windowHandle = Win32.CreateWindowEx(0,
                                                "SharpGLRenderWindow",
                                                "",
                                                Win32.WindowStyles.WS_CLIPCHILDREN | Win32.WindowStyles.WS_CLIPSIBLINGS | Win32.WindowStyles.WS_POPUP,
                                                0, 0, width, height,
                                                IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            //	Get the window device context.
            deviceContextHandle = Win32.GetDC(windowHandle);

            //	Setup a pixel format.
            Win32.PIXELFORMATDESCRIPTOR pfd = new Win32.PIXELFORMATDESCRIPTOR();
            pfd.Init();
            pfd.nVersion     = 1;
            pfd.dwFlags      = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER;
            pfd.iPixelType   = Win32.PFD_TYPE_RGBA;
            pfd.cColorBits   = (byte)bitDepth;
            pfd.cDepthBits   = 16;
            pfd.cStencilBits = 8;
            pfd.iLayerType   = Win32.PFD_MAIN_PLANE;

            //	Match an appropriate pixel format
            int iPixelformat;

            if ((iPixelformat = Win32.ChoosePixelFormat(deviceContextHandle, pfd)) == 0)
            {
                return(false);
            }

            //	Sets the pixel format
            if (Win32.SetPixelFormat(deviceContextHandle, iPixelformat, pfd) == 0)
            {
                return(false);
            }

            //	Create the render context.
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);

            //  Make the context current.
            MakeCurrent();

            //  Update the context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return(true);
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            //  Call the base.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            //	Create a new window class, as basic as possible.                
            Win32.WNDCLASSEX wndClass = new Win32.WNDCLASSEX();
            wndClass.Init();
		    wndClass.style			= Win32.ClassStyles.HorizontalRedraw | Win32.ClassStyles.VerticalRedraw | Win32.ClassStyles.OwnDC;
            wndClass.lpfnWndProc    = wndProcDelegate;
		    wndClass.cbClsExtra		= 0;
		    wndClass.cbWndExtra		= 0;
		    wndClass.hInstance		= IntPtr.Zero;
		    wndClass.hIcon			= IntPtr.Zero;
		    wndClass.hCursor		= IntPtr.Zero;
		    wndClass.hbrBackground	= IntPtr.Zero;
		    wndClass.lpszMenuName	= null;
		    wndClass.lpszClassName	= "SharpGLRenderWindow";
		    wndClass.hIconSm		= IntPtr.Zero;
		    Win32.RegisterClassEx(ref wndClass);
            	
		    //	Create the window. Position and size it.
		    windowHandle = Win32.CreateWindowEx(0,
					      "SharpGLRenderWindow",
					      "",
					      Win32.WindowStyles.WS_CLIPCHILDREN | Win32.WindowStyles.WS_CLIPSIBLINGS | Win32.WindowStyles.WS_POPUP,
					      0, 0, width, height,
					      IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

		    //	Get the window device context.
		    deviceContextHandle = Win32.GetDC(windowHandle);

		    //	Setup a pixel format.
		    Win32.PIXELFORMATDESCRIPTOR pfd = new Win32.PIXELFORMATDESCRIPTOR();
            pfd.Init();
		    pfd.nVersion = 1;
		    pfd.dwFlags = Win32.PFD_DRAW_TO_WINDOW | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_DOUBLEBUFFER;
		    pfd.iPixelType = Win32.PFD_TYPE_RGBA;
		    pfd.cColorBits = (byte)bitDepth;
		    pfd.cDepthBits = 16;
		    pfd.cStencilBits = 8;
		    pfd.iLayerType = Win32.PFD_MAIN_PLANE;
		
		    //	Match an appropriate pixel format 
		    int iPixelformat;
		    if((iPixelformat = Win32.ChoosePixelFormat(deviceContextHandle, pfd)) == 0 )
			    return false;

		    //	Sets the pixel format
            if (Win32.SetPixelFormat(deviceContextHandle, iPixelformat, pfd) == 0)
		    {
			    return false;
		    }

		    //	Create the render context.
            renderContextHandle = Win32.wglCreateContext(deviceContextHandle);
            
            //  Make the context current.
            MakeCurrent();

            //  Update the context if required.
            UpdateContextVersion(gl);

            //  Return success.
            return true;
        }
        /// <summary>
        /// Creates the render context provider. Must also create the OpenGL extensions.
        /// </summary>
        /// <param name="openGLVersion">The desired OpenGL version.</param>
        /// <param name="gl">The OpenGL context.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="bitDepth">The bit depth.</param>
        /// <param name="parameter">The parameter</param>
        /// <returns></returns>
        public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter)
        {
            this.gl = gl;

            //  Call the base class.
            base.Create(openGLVersion, gl, width, height, bitDepth, parameter);

            // Request an id for our pixel buffer.
            uint[] ids = new uint[1];

            ids = new uint[1];
            gl.GenBuffers(1, ids);

            pixelBufferID = ids[0];

            // The writeable bitmap needs to be initialized, so trigger setDimensions for this.
            SetDimensions(width, height);

            return true;
        }