示例#1
0
        /// <summary>
        /// This function sets the pixel format of the underlying bitmap.
        /// </summary>
        /// <param name="hDC"></param>
        /// <param name="parameters">parameters.</param>
        protected void SetPixelFormat(IntPtr hDC, ContextGenerationParams parameters)
        {
            //	Create the big lame pixel format majoo.
            var pdf = new PixelFormatDescriptor();

            pdf.Init();

            //	Set the values for the pixel format.
            pdf.nVersion     = 1;
            pdf.dwFlags      = (Win32.PFD_DRAW_TO_BITMAP | Win32.PFD_SUPPORT_OPENGL | Win32.PFD_SUPPORT_GDI);
            pdf.iPixelType   = Win32.PFD_TYPE_RGBA;
            pdf.cColorBits   = parameters.ColorBits;
            pdf.cDepthBits   = parameters.DepthBits;
            pdf.cStencilBits = parameters.StencilBits;
            pdf.iLayerType   = Win32.PFD_MAIN_PLANE;

            //	Match an appropriate pixel format
            int iPixelformat = Win32.ChoosePixelFormat(hDC, pdf);

            if (iPixelformat == 0)
            {
                throw new Exception(string.Format("ChoosePixelFormat([{0}], [{1}]) failed!", hDC, pdf));
            }

            //	Sets the pixel format
            if (false == Win32.SetPixelFormat(hDC, iPixelformat, pdf))
            {
                int lastError = Marshal.GetLastWin32Error();
                throw new Exception(string.Format("SetPixelFormat([{0}], [{1}], [{2}]) failed! Win32Error:[{3}].", hDC, iPixelformat, pdf, lastError));
            }
        }
示例#2
0
        /// <summary>
        /// Resizes the section.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="parameters">parameters.</param>
        public void Resize(int width, int height, ContextGenerationParams parameters)
        {
            //	Destroy existing objects.
            this.DestroyBitmap();

            //  Set parameters.
            this.Width  = width;
            this.Height = height;

            //	Create a bitmap info structure.
            var info = new BitmapInfo();

            info.Init();

            //	Set the data.
            info.biBitCount = parameters.ColorBits;
            info.biPlanes   = 1;
            info.biWidth    = width;
            info.biHeight   = height;

            //	Create the bitmap.
            IntPtr mdc = this.MemoryDeviceContext;

            this.HBitmap = Win32.CreateDIBSection(mdc, ref info, Win32.DIB_RGB_COLORS, out this.bits, IntPtr.Zero, 0);

            Win32.SelectObject(mdc, this.HBitmap);
        }
示例#3
0
        /// <summary>
        /// creates render device and render context.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="parameters">parameters.</param>
        /// <returns></returns>
        public SoftGLRenderContext(int width, int height, ContextGenerationParams parameters)
            : base(width, height, parameters)
        {
            GCHandle handle = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection);

            this.Pointer = GCHandle.ToIntPtr(handle);
            handle.Free();
            // Create a new window class, as basic as possible.
            if (!this.CreateBasicRenderContext(width, height, parameters))
            {
                throw new Exception(string.Format("Create basic render context failed!"));
            }

            //  Make the context current.
            this.MakeCurrent();

            if (parameters.UpdateContextVersion)
            {
                //  Update the context if required.
                // if I update context, something in legacy opengl will not work...
                this.UpdateContextVersion(parameters);
            }

            this.dibSection = new DIBSection(this.DeviceContextHandle, width, height, parameters);
        }
示例#4
0
        /// <summary>
        /// Creates the specified width.
        /// </summary>
        /// <param name="deviceContext"></param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="parameters">parameters.</param>
        public DIBSection(IntPtr deviceContext, int width, int height, ContextGenerationParams parameters)
        {
            this.Width  = width;
            this.Height = height;
            //this.MemoryDeviceContext = Win32.CreateCompatibleDC(deviceContext);

            //	Destroy existing objects.
            this.DestroyBitmap();

            ////	Create a bitmap info structure.
            //var info = new BitmapInfo();
            //info.Init();

            ////	Set the data.
            //info.biBitCount = parameters.ColorBits;
            //info.biPlanes = 1;
            //info.biWidth = width;
            //info.biHeight = height;

            ////	Create the bitmap.
            //IntPtr mdc = this.MemoryDeviceContext;
            //this.HBitmap = Win32.CreateDIBSection(mdc, ref info, Win32.DIB_RGB_COLORS, out this.bits, IntPtr.Zero, 0);

            //Win32.SelectObject(mdc, this.HBitmap);

            ////	Set the OpenGL pixel format.
            //this.SetPixelFormat(mdc, parameters);
        }
示例#5
0
        /// <summary>
        /// A render context.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public FBORenderContext(int width, int height, ContextGenerationParams parameters)
            : base(width, height, parameters)
        {
            Framebuffer framebuffer = CreateFramebuffer(width, height, parameters);

            framebuffer.Bind();
            this.framebuffer = framebuffer;
        }
        /// <summary>
        /// Create a new window class, as basic as possible.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private bool CreateBasicRenderContext(int width, int height, ContextGenerationParams parameters)
        {
            var wndClass = new WNDCLASSEX();

            wndClass.Init();
            wndClass.style         = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw | ClassStyles.OwnDC;
            wndClass.lpfnWndProc   = wndProcDelegate;
            wndClass.lpszClassName = "CSharpGLRenderWindow";
            Win32.RegisterClassEx(ref wndClass);

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

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

            //	Setup a pixel format.
            var pfd = new 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      = parameters.ColorBits;
            pfd.cAccumBits      = parameters.AccumBits;
            pfd.cAccumRedBits   = parameters.AccumRedBits;
            pfd.cAccumGreenBits = parameters.AccumGreenBits;
            pfd.cAccumBlueBits  = parameters.AccumBlueBits;
            pfd.cAccumAlphaBits = parameters.AccumAlphaBits;
            pfd.cDepthBits      = parameters.DepthBits;
            pfd.cStencilBits    = parameters.StencilBits;
            pfd.iLayerType      = Win32.PFD_MAIN_PLANE;

            //	Match an appropriate pixel format
            int iPixelformat = Win32.ChoosePixelFormat(this.DeviceContextHandle, pfd);

            if (iPixelformat == 0)
            {
                return(false);
            }

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

            //	Create the render context.
            this.RenderContextHandle = Win32.wglCreateContext(this.DeviceContextHandle);

            return(true);
        }
        void ISupportInitialize.EndInit()
        {
            try
            {
                int width = this.Width, height = this.Height;

                if (this.designMode)
                {
                    //this.assist.Resize(width, height);
                }
                else
                {
                    this.KeyPress   += WinSoftGLCanvas_KeyPress;
                    this.MouseDown  += WinSoftGLCanvas_MouseDown;
                    this.MouseMove  += WinSoftGLCanvas_MouseMove;
                    this.MouseUp    += WinSoftGLCanvas_MouseUp;
                    this.MouseWheel += WinSoftGLCanvas_MouseWheel;
                    this.KeyDown    += WinSoftGLCanvas_KeyDown;
                    this.KeyUp      += WinSoftGLCanvas_KeyUp;
                }

                // Create the render context.
                ContextGenerationParams parameters = null;
                if (this.designMode)
                {
                    parameters = new ContextGenerationParams();
                    parameters.UpdateContextVersion = false;
                }
                else
                {
                    parameters = this.parameters;
                }
                var renderContext = new SoftGLRenderContext(width, height, parameters);
                renderContext.MakeCurrent();
                this.renderContext = renderContext;

                // Set the most basic OpenGL styles.
                GL.Instance.ShadeModel(GL.GL_SMOOTH);
                GL.Instance.ClearDepth(1.0f);
                GL.Instance.Enable(GL.GL_DEPTH_TEST);// depth test is disabled by default.
                GL.Instance.DepthFunc(GL.GL_LEQUAL);
                GL.Instance.Hint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// creates render device and render context.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="parameters">parameters.</param>
        /// <returns></returns>
        public WinGLRenderContext(int width, int height, ContextGenerationParams parameters)
            : base(width, height, parameters)
        {
            // Create a new window class, as basic as possible.
            if (!this.CreateBasicRenderContext(width, height, parameters))
            {
                throw new Exception(string.Format("Create basic render context failed!"));
            }

            //  Make the context current.
            this.MakeCurrent();

            if (parameters.UpdateContextVersion)
            {
                //  Update the context if required.
                // if I update context, something in legacy opengl will not work...
                this.UpdateContextVersion(parameters);
            }

            //this.dibSection = new DIBSection(this.DeviceContextHandle, width, height, parameters);
        }
示例#9
0
        private static Framebuffer CreateFramebuffer(int width, int height, ContextGenerationParams parameters)
        {
            var framebuffer = new Framebuffer(width, height);

            framebuffer.Bind();
            {
                var  renderbuffer            = new Renderbuffer(width, height, GL.GL_RGBA);
                uint colorAttachmentLocation = 0;
                framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, colorAttachmentLocation);// 0
            }
            {
                var renderbuffer = new Renderbuffer(width, height, GL.GL_DEPTH_COMPONENT24);
                framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Depth);// special
            }
            if (parameters.StencilBits > 0)
            {
                var renderbuffer = new Renderbuffer(width, height, GL.GL_STENCIL_INDEX8);
                framebuffer.Attach(FramebufferTarget.Framebuffer, renderbuffer, AttachmentLocation.Stencil);
            }
            framebuffer.CheckCompleteness();
            framebuffer.Unbind();

            return(framebuffer);
        }
示例#10
0
 /// <summary>
 ///  Set the width, height and parameters.
 /// </summary>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="parameters">parameters.</param>
 /// <returns></returns>
 public GLRenderContext(int width, int height, ContextGenerationParams parameters)
 {
     this.Width      = width;
     this.Height     = height;
     this.Parameters = parameters;
 }
        /// <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="parameters"></param>
        protected bool UpdateContextVersion(ContextGenerationParams parameters)
        {
            //  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).

            //  Now the none-trivial case. We must use the WGL_create_context extension to
            //  attempt to create a 3.0+ context.
            try
            {
                var wglChoosePixelFormatARB = GL.Instance.GetDelegateFor("wglChoosePixelFormatARB", GLDelegates.typeof_bool_IntPtr_intN_floatN_uint_intN_uintN) as GLDelegates.bool_IntPtr_intN_floatN_uint_intN_uintN;
                if (wglChoosePixelFormatARB == null)
                {
                    return(false);
                }
                var wglCreateContextAttribs = GL.Instance.GetDelegateFor("wglCreateContextAttribsARB", GLDelegates.typeof_IntPtr_IntPtr_IntPtr_intN) as GLDelegates.IntPtr_IntPtr_IntPtr_intN;
                if (wglCreateContextAttribs == null)
                {
                    return(false);
                }

                int major, minor;
                GetHighestVersion(out major, out minor);
                if ((major > 2) || (major == 2 && minor > 1))
                {
                    int[] attribList = new int[]
                    {
                        WinGL.WGL_SUPPORT_OPENGL_ARB, (int)GL.GL_TRUE,
                        WinGL.WGL_DRAW_TO_WINDOW_ARB, (int)GL.GL_TRUE,
                        WinGL.WGL_DOUBLE_BUFFER_ARB, (int)GL.GL_TRUE,
                        WinGL.WGL_ACCELERATION_ARB, WinGL.WGL_FULL_ACCELERATION_ARB,
                        WinGL.WGL_PIXEL_TYPE_ARB, WinGL.WGL_TYPE_RGBA_ARB,
                        WinGL.WGL_COLOR_BITS_ARB, parameters.ColorBits,
                        WinGL.WGL_ACCUM_BITS_ARB, parameters.AccumBits,
                        WinGL.WGL_ACCUM_RED_BITS_ARB, parameters.AccumRedBits,
                        WinGL.WGL_ACCUM_GREEN_BITS_ARB, parameters.AccumGreenBits,
                        WinGL.WGL_ACCUM_BLUE_BITS_ARB, parameters.AccumBlueBits,
                        WinGL.WGL_ACCUM_ALPHA_BITS_ARB, parameters.AccumAlphaBits,
                        WinGL.WGL_DEPTH_BITS_ARB, parameters.DepthBits,
                        WinGL.WGL_STENCIL_BITS_ARB, parameters.StencilBits,
                        0,        //End
                    };

                    IntPtr dc = this.DeviceContextHandle;
                    //	Match an appropriate pixel format
                    int[]  pixelFormat = new int[1];
                    uint[] numFormats  = new uint[1];
                    if (false == wglChoosePixelFormatARB(dc, attribList, null, 1, pixelFormat, numFormats))
                    {
                        return(false);
                    }
                    //	Sets the pixel format
                    if (false == Win32.SetPixelFormat(dc, pixelFormat[0], new PixelFormatDescriptor()))
                    {
                        return(false);
                    }

                    int[] attributes =
                    {
                        WinGL.WGL_CONTEXT_MAJOR_VERSION_ARB,
                        major,
                        WinGL.WGL_CONTEXT_MINOR_VERSION_ARB,
                        minor,
                        WinGL.WGL_CONTEXT_PROFILE_MASK_ARB,
                        WinGL.WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
//#if DEBUG
//                        GL.WGL_CONTEXT_FLAGS, GL.WGL_CONTEXT_DEBUG_BIT,// this is a debug context
//#endif
                        0
                    };
                    IntPtr hrc = wglCreateContextAttribs(dc, IntPtr.Zero, attributes);
                    Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                    Win32.wglDeleteContext(this.RenderContextHandle);
                    Win32.wglMakeCurrent(dc, hrc);
                    this.RenderContextHandle = hrc;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Log.Write(ex);
            }

            return(true);
        }