Example #1
0
        public static void EnableOpenGL(IntPtr ghDC)
        {
            int PixFormat;   // pixed format

            // Pixed descriptor 
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();

            // Set memory 
            //ZeroMemory(pfd, Microsoft.VisualBasic.Strings.Len(pfd));

            // Pixel descriptor configuration
            pfd.nSize = (short)Microsoft.VisualBasic.Strings.Len(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 = 24;
            pfd.cDepthBits = 32;
            pfd.cAlphaBits = 24;
            pfd.iLayerType = Gdi.PFD_MAIN_PLANE;

            // Chose pixel format
            PixFormat = Gdi.ChoosePixelFormat((IntPtr)ghDC, ref pfd);

            // If the operation failed let user know
            if (PixFormat == 0)
            {
                //MessageBox.Show("Can't create OpenGL context!", "Saturn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable = false;
                return;
            }
            if (!Gdi.SetPixelFormat((IntPtr)ghDC, PixFormat, ref pfd))
            {
                //MessageBox.Show("Unable to set pixel format", "Saturn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable = false;
                return;
            }

            // Set graphic context
            hRC = Wgl.wglCreateContext(ghDC);

            // If the operation failed let user know
            if (hRC.ToInt32() == 0)
            {
                //MessageBox.Show("Unable to get rendering context", "Saturn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable = false;
                return;
            }
            if (!(Wgl.wglMakeCurrent(ghDC, hRC)))
            {
                //MessageBox.Show("Unable to make rendering context current", "Saturn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                enable = false;
                return;
            }
            enable = true;   // GL has been enabled

        }
Example #2
0
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, false);
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();

            pfd.dwFlags = Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_DOUBLEBUFFER;   // OpenGLをサポート | ウィンドウに描画 | ダブルバッファ
            pfd.iPixelType = Gdi.PFD_TYPE_RGBA; //RGBAフォーマット
            pfd.cColorBits = 32; // 32bit/pixel
            pfd.cAlphaBits = 8; // アルファチャンネル8bit (0にするとアルファチャンネル無しになる)
            pfd.cDepthBits = 16; // デプスバッファ16bit

            this.hDC = User.GetDC(this.Handle);     // デバイスコンテキストのハンドルを取得

            int pixFormat = Gdi.ChoosePixelFormat(this.hDC, ref pfd);   // ピクセルフォーマットを選択
            if (pixFormat <= 0)
            {
                throw new Exception("ChoosePixelFormat failed.");
            }

            Wgl.wglDescribePixelFormat(this.hDC, pixFormat, 40, ref pfd);   //ピクセルフォーマットの正確な設定を取得

            bool valid = Gdi.SetPixelFormat(this.hDC, pixFormat, ref pfd);  //デバイスコンテキストにピクセルフォーマットを設定
            if (!valid)
            {
                throw new Exception("SetPixelFormat failed");
            }

            this.hRC = Wgl.wglCreateContext(this.hDC);  //OpenGLのレンダリングコンテキストを作成
            if (this.hRC == IntPtr.Zero)
            {
                throw new Exception("wglCreateContext failed.");
            }

            Wgl.wglMakeCurrent(this.hDC, this.hRC);  //作成したコンテキストをカレントに設定

            //レンダリングコンテキストを作成、カレントに設定したら、1度だけこれを呼び出しておく
            //Tao.OpenGl.GL、Tao.Platform.Windows.WGLの仕様。
            Gl.ReloadFunctions();
            Wgl.ReloadFunctions();

            int err = Gl.glGetError();
            if (err != Gl.GL_NO_ERROR)
            {
                throw new Exception("Error code = " + err.ToString());
            }

            this.SetupGL();
        }
Example #3
0
        /// <summary>
        /// Initializes an OpenGL context with the associated Win32 device context</summary>
        /// <param name="hdc">HDC from the window to which the new OpenGL context is bound</param>
        /// <param name="hglrc">Handle to the new OpenGL context</param>
        public static void InitOpenGl(IntPtr hdc, out IntPtr hglrc)
        {
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
            PopulatePixelFormatDescriptor(ref pfd);

            // Attempt To Find An Appropriate Pixel Format
            int pixelFormat = Gdi.ChoosePixelFormat(hdc, ref pfd);
            if (pixelFormat == 0)
                throw new InvalidOperationException("Can't find a suitable PixelFormat");

            // Attempt To Set The Pixel Format?
            if (!Gdi.SetPixelFormat(hdc, pixelFormat, ref pfd))
                throw new InvalidOperationException("Can't set the PixelFormat");

            // Attempt To Get The Rendering Context
            hglrc = Wgl.wglCreateContext(hdc);
            if (hglrc == IntPtr.Zero)
                throw new InvalidOperationException("Can't create GL rendering context");

            if (s_sharedHglrc == IntPtr.Zero)
                s_sharedHglrc = hglrc;
            else
            {
                // We don't throw an exception on failure because the caller may be on another thread or is
                //  using a different pixel format.
                Wgl.wglShareLists(s_sharedHglrc, hglrc);
            }

            if (!Wgl.wglMakeCurrent(hdc, hglrc))
                throw new InvalidOperationException("Can't make the OpenGL rendering context to be the current context.");

            // Load all extensions for mainline rendering
            if (!s_initialized)
            {
                LoadAllExtensions();
                s_initialized = true;
            }

            // Init fonts
            using (Font defaultFont = SystemFonts.DefaultFont)
            {
                Gdi.SelectObject(hdc, defaultFont.ToHfont());
            }
            foreach (IntSet.Range range in s_fontMap.Ranges)
            {
                int baseDisplayListId = range.PreviousItemsCount + TEXT_DISPLAY_LIST_BASE;

                if (!wglUseFontBitmaps(hdc, range.Min, range.Count, (uint)baseDisplayListId))
                    throw new InvalidOperationException("Font bitmaps were unable to be created.");
            }
            s_fontMap.Lock();

            Util3D.ReportErrors();
        }
Example #4
0
		private void SetupGL()
		{
			_hDC = User.GetDC( _controlHandle );
			Debug.Assert( _hDC != IntPtr.Zero );
			if( _hDC == IntPtr.Zero )
				return;

			Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
			//ZeroMemory( &pfd, sizeof( pfd ) );
			pfd.nSize = ( short )sizeof( Gdi.PIXELFORMATDESCRIPTOR );
			pfd.nVersion = 1;
			pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_TYPE_RGBA;
#if VSYNC
			//pfd.dwFlags |= Gdi.PFD_DOUBLEBUFFER;
#endif
			pfd.iPixelType = Gdi.PFD_TYPE_RGBA;
			pfd.cColorBits = 24;
			pfd.cDepthBits = 32;
			pfd.iLayerType = Gdi.PFD_MAIN_PLANE;
			int iFormat = Gdi.ChoosePixelFormat( _hDC, ref pfd );
			bool spf = Gdi.SetPixelFormat( _hDC, iFormat, ref pfd );

			Wgl.wglMakeCurrent( IntPtr.Zero, IntPtr.Zero );
			_hRC = Wgl.wglCreateContext( _hDC );
			Debug.Assert( _hRC != IntPtr.Zero );
			if( _hRC == IntPtr.Zero )
			{
				User.ReleaseDC( _controlHandle, _hDC );
				_hDC = IntPtr.Zero;
				return;
			}
			bool mc = Wgl.wglMakeCurrent( _hDC, _hRC );

			Gl.glShadeModel( Gl.GL_SMOOTH );
			Gl.glClearColor( 0.0f, 0.5f, 0.5f, 1.0f );
			Gl.glClearDepth( 0.0f );
			//Gl.glEnable( Gl.GL_DEPTH_TEST );
			Gl.glDepthFunc( Gl.GL_LEQUAL );
			Gl.glDepthRange( 1.0f, 0.0f );
			Gl.glAlphaFunc( Gl.GL_GREATER, 0.03f );
			Gl.glHint( Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST );
			Gl.glFogi( Gl.GL_FOG_MODE, Gl.GL_LINEAR );
			Gl.glFogf( Gl.GL_FOG_DENSITY, 0.1f );
			Gl.glHint( Gl.GL_FOG_HINT, Gl.GL_DONT_CARE );

#if !VSYNC
			Wgl.wglSwapIntervalEXT( 0 );
#endif

			this.Resize( _screenWidth, _screenHeight );

			_defaultProgram = new DefaultProgram();
			_defaultProgram.Prepare();
		}
Example #5
0
        /// <summary>
        ///     Creates our OpenGL Window.
        /// </summary>
        /// <param name="title">
        ///     The title to appear at the top of the window.
        /// </param>
        /// <param name="width">
        ///     The width of the GL window or fullscreen mode.
        /// </param>
        /// <param name="height">
        ///     The height of the GL window or fullscreen mode.
        /// </param>
        /// <param name="bits">
        ///     The number of bits to use for color (8/16/24/32).
        /// </param>
        /// <param name="fullscreenflag">
        ///     Use fullscreen mode (<c>true</c>) or windowed mode (<c>false</c>).
        /// </param>
        /// <returns>
        ///     <c>true</c> on successful window creation, otherwise <c>false</c>.
        /// </returns>
        private static bool CreateGLWindow(string title, int width, int height, int bits, bool fullscreenflag)
        {
            int pixelFormat;                                                    // Holds The Results After Searching For A Match
            fullscreen = fullscreenflag;                                        // Set The Global Fullscreen Flag
            form = null;                                                        // Null The Form

            GC.Collect();                                                       // Request A Collection
            // This Forces A Swap
            Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);

            if(fullscreen) {                                                    // Attempt Fullscreen Mode?
                Gdi.DEVMODE dmScreenSettings = new Gdi.DEVMODE();               // Device Mode
                // Size Of The Devmode Structure
                dmScreenSettings.dmSize = (short) Marshal.SizeOf(dmScreenSettings);
                dmScreenSettings.dmPelsWidth = width;                           // Selected Screen Width
                dmScreenSettings.dmPelsHeight = height;                         // Selected Screen Height
                dmScreenSettings.dmBitsPerPel = bits;                           // Selected Bits Per Pixel
                dmScreenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
                if(User.ChangeDisplaySettings(ref dmScreenSettings, User.CDS_FULLSCREEN) != User.DISP_CHANGE_SUCCESSFUL) {
                    // If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
                    if(MessageBox.Show("The Requested Fullscreen Mode Is Not Supported By\nYour Video Card.  Use Windowed Mode Instead?", "NeHe GL",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) {
                        fullscreen = false;                                     // Windowed Mode Selected.  Fullscreen = false
                    }
                    else {
                        // Pop up A Message Box Lessing User Know The Program Is Closing.
                        MessageBox.Show("Program Will Now Close.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return false;                                           // Return false
                    }
                }
            }

            form = new Lesson13();                                              // Create The Window

            if(fullscreen) {                                                    // Are We Still In Fullscreen Mode?
                form.FormBorderStyle = FormBorderStyle.None;                    // No Border
                Cursor.Hide();                                                  // Hide Mouse Pointer
            }
            else {                                                              // If Windowed
                form.FormBorderStyle = FormBorderStyle.Sizable;                 // Sizable
                Cursor.Show();                                                  // Show Mouse Pointer
            }

            form.Width = width;                                                 // Set Window Width
            form.Height = height;                                               // Set Window Height
            form.Text = title;                                                  // Set Window Title

            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();    // pfd Tells Windows How We Want Things To Be
            pfd.nSize = (short) Marshal.SizeOf(pfd);                            // Size Of This Pixel Format Descriptor
            pfd.nVersion = 1;                                                   // Version Number
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |                              // Format Must Support Window
                Gdi.PFD_SUPPORT_OPENGL |                                        // Format Must Support OpenGL
                Gdi.PFD_DOUBLEBUFFER;                                           // Format Must Support Double Buffering
            pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA;                          // Request An RGBA Format
            pfd.cColorBits = (byte) bits;                                       // Select Our Color Depth
            pfd.cRedBits = 0;                                                   // Color Bits Ignored
            pfd.cRedShift = 0;
            pfd.cGreenBits = 0;
            pfd.cGreenShift = 0;
            pfd.cBlueBits = 0;
            pfd.cBlueShift = 0;
            pfd.cAlphaBits = 0;                                                 // No Alpha Buffer
            pfd.cAlphaShift = 0;                                                // Shift Bit Ignored
            pfd.cAccumBits = 0;                                                 // No Accumulation Buffer
            pfd.cAccumRedBits = 0;                                              // Accumulation Bits Ignored
            pfd.cAccumGreenBits = 0;
            pfd.cAccumBlueBits = 0;
            pfd.cAccumAlphaBits = 0;
            pfd.cDepthBits = 16;                                                // 16Bit Z-Buffer (Depth Buffer)
            pfd.cStencilBits = 0;                                               // No Stencil Buffer
            pfd.cAuxBuffers = 0;                                                // No Auxiliary Buffer
            pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE;                         // Main Drawing Layer
            pfd.bReserved = 0;                                                  // Reserved
            pfd.dwLayerMask = 0;                                                // Layer Masks Ignored
            pfd.dwVisibleMask = 0;
            pfd.dwDamageMask = 0;

            hDC = User.GetDC(form.Handle);                                      // Attempt To Get A Device Context
            if(hDC == IntPtr.Zero) {                                            // Did We Get A Device Context?
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Can't Create A GL Device Context.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);                  // Attempt To Find An Appropriate Pixel Format
            if(pixelFormat == 0) {                                              // Did Windows Find A Matching Pixel Format?
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Can't Find A Suitable PixelFormat.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if(!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd)) {                // Are We Able To Set The Pixel Format?
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Can't Set The PixelFormat.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            hRC = Wgl.wglCreateContext(hDC);                                    // Attempt To Get The Rendering Context
            if(hRC == IntPtr.Zero) {                                            // Are We Able To Get A Rendering Context?
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Can't Create A GL Rendering Context.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if(!Wgl.wglMakeCurrent(hDC, hRC)) {                                 // Try To Activate The Rendering Context
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Can't Activate The GL Rendering Context.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            form.Show();                                                        // Show The Window
            form.TopMost = true;                                                // Topmost Window
            form.Focus();                                                       // Focus The Window

            if(fullscreen) {                                                    // This Shouldn't Be Necessary, But Is
                Cursor.Hide();
            }
            ReSizeGLScene(width, height);                                       // Set Up Our Perspective GL Screen

            if(!InitGL()) {                                                     // Initialize Our Newly Created GL Window
                KillGLWindow();                                                 // Reset The Display
                MessageBox.Show("Initialization Failed.", "ERROR",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;                                                        // Success
        }
Example #6
0
    /// <summary>
    ///     Creates our OpenGl Window.
    /// </summary>
    /// <param name="title">
    ///     The title to appear at the top of the window.
    /// </param>
    /// <param name="width">
    ///     The width of the Gl window or fullscreen mode.
    /// </param>
    /// <param name="height">
    ///     The height of the Gl window or fullscreen mode.
    /// </param>
    /// <param name="bits">
    ///     The number of bits to use for color (8/16/24/32).
    /// </param>
    /// <param name="fullscreenflag">
    ///     Use fullscreen mode (<c>true</c>) or windowed mode (<c>false</c>).
    /// </param>
    /// <returns>
    ///     <c>true</c> on successful window creation, otherwise <c>false</c>.
    /// </returns>
    private static bool CreateGlWindow(string title, int width, int height, int bits, bool fullscreenflag)
    {
        int pixelFormat;                                                    // Holds The Results After Searching For A Match
        fullscreen = fullscreenflag;                                        // Set The Global Fullscreen Flag
        form = null;                                                        // Null The Form

        GC.Collect();                                                       // Request A Collection
        // This Forces A Swap
        Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);

        if (fullscreen)
        {                                                    // Attempt Fullscreen Mode?
            Gdi.DEVMODE dmScreenSettings = new Gdi.DEVMODE();               // Device Mode
            // Size Of The Devmode Structure
            dmScreenSettings.dmSize = (short)Marshal.SizeOf(dmScreenSettings);
            dmScreenSettings.dmPelsWidth = width;                           // Selected Screen Width
            dmScreenSettings.dmPelsHeight = height;                         // Selected Screen Height
            dmScreenSettings.dmBitsPerPel = bits;                           // Selected Bits Per Pixel
            dmScreenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

            // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
            fullscreen = true;
        }

        form = new QuadricDemo();                                              // Create The Window

        form.Width = width;                                                 // Set Window Width
        form.Height = height;                                               // Set Window Height
        form.Text = title;

        #region WINDOW CREATION STUFF
        Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();    // pfd Tells Windows How We Want Things To Be
        pfd.nSize = (short)Marshal.SizeOf(pfd);                            // Size Of This Pixel Format Descriptor
        pfd.nVersion = 1;                                                   // Version Number
        pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |                              // Format Must Support Window
            Gdi.PFD_SUPPORT_OPENGL |                                        // Format Must Support OpenGl
            Gdi.PFD_DOUBLEBUFFER;                                           // Format Must Support Double Buffering
        pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;                          // Request An RGBA Format
        pfd.cColorBits = (byte)bits;                                       // Select Our Color Depth
        pfd.cRedBits = 0;                                                   // Color Bits Ignored
        pfd.cRedShift = 0;
        pfd.cGreenBits = 0;
        pfd.cGreenShift = 0;
        pfd.cBlueBits = 0;
        pfd.cBlueShift = 0;
        pfd.cAlphaBits = 0;                                                 // No Alpha Buffer
        pfd.cAlphaShift = 0;                                                // Shift Bit Ignored
        pfd.cAccumBits = 0;                                                 // No Accumulation Buffer
        pfd.cAccumRedBits = 0;                                              // Accumulation Bits Ignored
        pfd.cAccumGreenBits = 0;
        pfd.cAccumBlueBits = 0;
        pfd.cAccumAlphaBits = 0;
        pfd.cDepthBits = 16;                                                // 16Bit Z-Buffer (Depth Buffer)
        pfd.cStencilBits = 0;                                               // No Stencil Buffer
        pfd.cAuxBuffers = 0;                                                // No Auxiliary Buffer
        pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;                         // Main Drawing Layer
        pfd.bReserved = 0;                                                  // Reserved
        pfd.dwLayerMask = 0;                                                // Layer Masks Ignored
        pfd.dwVisibleMask = 0;
        pfd.dwDamageMask = 0;

        hDC = User.GetDC(form.Handle);                                      // Attempt To Get A Device Context
        if (hDC == IntPtr.Zero)
        {                                            // Did We Get A Device Context?
            KillGlWindow();                                                 // Reset The Display
            MessageBox.Show("Can't Create A Gl Device Context.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);                  // Attempt To Find An Appropriate Pixel Format
        if (pixelFormat == 0)
        {                                              // Did Windows Find A Matching Pixel Format?
            KillGlWindow();                                                 // Reset The Display
            MessageBox.Show("Can't Find A Suitable PixelFormat.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        if (!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd))
        {                // Are We Able To Set The Pixel Format?
            KillGlWindow();                                                 // Reset The Display
            MessageBox.Show("Can't Set The PixelFormat.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        hRC = Wgl.wglCreateContext(hDC);                                    // Attempt To Get The Rendering Context
        if (hRC == IntPtr.Zero)
        {                                            // Are We Able To Get A Rendering Context?
            KillGlWindow();                                                 // Reset The Display
            MessageBox.Show("Can't Create A Gl Rendering Context.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        if (!Wgl.wglMakeCurrent(hDC, hRC))
        {                                 // Try To Activate The Rendering Context
            KillGlWindow();                                                 // Reset The Display
            MessageBox.Show("Can't Activate The Gl Rendering Context.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
        #endregion

        form.Show();                                                        // Show The Window
        form.Focus();                                                       // Focus The Window

        InitGl();                                                            //Initiate OpenGl

        setViewport(width, height);                                          // Set viewport
        setPerspective(width, height);                                       // Set Up Our Perspective Gl Screen

        return true;                                                         // Success
    }
Example #7
0
		///<summary>Returns the pixel formats from 1 to Max(maximumCount,maximum available pixel formats).</summary>
		public static Gdi.PIXELFORMATDESCRIPTOR[] GetPixelFormats(System.IntPtr hdc) {
			Gdi.PIXELFORMATDESCRIPTOR pfd=new Gdi.PIXELFORMATDESCRIPTOR();
			pfd.nSize=(short)Marshal.SizeOf(pfd);
			pfd.nVersion=1;
			int numFormats=_DescribePixelFormat(hdc,1,(uint)pfd.nSize,ref pfd);
			Gdi.PIXELFORMATDESCRIPTOR[] pixelFormats=new Gdi.PIXELFORMATDESCRIPTOR[numFormats];
			for(int i=0;i<pixelFormats.Length;i++) {
				pixelFormats[i]=new Gdi.PIXELFORMATDESCRIPTOR();
				pixelFormats[i].nSize=(short)Marshal.SizeOf(pixelFormats[i]);
				pixelFormats[i].nVersion=1;
				_DescribePixelFormat(hdc,i+1,(uint)pixelFormats[i].nSize,ref pixelFormats[i]);
			}
			return pixelFormats;
		}
Example #8
0
		/// <summary>
		/// Creates the device and rendering contexts using the supplied settings
		/// in accumBits, colorBits, depthBits, and stencilBits. Returns the selected
		/// pixel format number.
		/// </summary>
		protected virtual int CreateContexts(IntPtr pDeviceContext,int preferredPixelFormatNum) {
			deviceContext = pDeviceContext;
			if(deviceContext == IntPtr.Zero) {
				throw new Exception("CreateContexts: Unable to create an OpenGL device context");
			}
			int selectedFormat=0;
			Gdi.PIXELFORMATDESCRIPTOR pixelFormat=new Gdi.PIXELFORMATDESCRIPTOR();
			pixelFormat.nSize=(short)Marshal.SizeOf(pixelFormat);
			pixelFormat.nVersion=1;
			//Here we care most about finding a format that will allow the proper creation of the control first and foremost, because even if the format is so bad that output to the display cannot be understood by the user, then the user is able to change the graphical options from within the program. We care second most about finding a format which is quick to load and which will be most time efficient during graphics display. Again, if a wrong choice is made, the user can make a choice of format manually.
			try {//Simply try the preferred pixel format number. If it works, then that is all we care about. Remember, 0 is an invalid format number.
				if(_DescribePixelFormat(deviceContext,preferredPixelFormatNum,(uint)pixelFormat.nSize,ref pixelFormat)==0 ||
					!Gdi.SetPixelFormat(deviceContext,preferredPixelFormatNum,ref pixelFormat)) {
					throw new Exception(string.Format("Unable to set the requested pixel format ({0})",selectedFormat));
				}
				selectedFormat=preferredPixelFormatNum;
			}catch{//Could not set the preferred pixel format for some reason. Possibly initial startup or the graphics card or driver changed since the program was last started.
				//Now try to auto-select the best pixel-format for speed efficientcy and graphical quality.
				try{
					PixelFormatValue pfv=ChoosePixelFormatEx(deviceContext);
					if(!Gdi.SetPixelFormat(deviceContext,pfv.formatNumber,ref pfv.pfd)) {
						throw new Exception("");
					}
					pixelFormat=pfv.pfd;
					selectedFormat=pfv.formatNumber;
				}catch{
					pixelFormat=new Gdi.PIXELFORMATDESCRIPTOR();//Zero out the old pixel format.
					pixelFormat.nSize=(short)Marshal.SizeOf(pixelFormat);
					pixelFormat.nVersion=1;
					//Unable to select a good pixel format. Now we are desperate. Try all formats starting from 1 until we get some format which at least works. That way the user can change the pixel format from the graphical options from inside the program after this point.
					selectedFormat=0;
					do{
						selectedFormat++;
						if(selectedFormat>_DescribePixelFormat(deviceContext,selectedFormat,(uint)pixelFormat.nSize,ref pixelFormat)){
 							throw new Exception("There are no acceptable pixel formats for OpenGL graphics.");
						}
					}while(!Gdi.SetPixelFormat(deviceContext,selectedFormat,ref pixelFormat));
				}
			}
			colorBits=pixelFormat.cColorBits;
			depthBits=pixelFormat.cDepthBits;
			autoSwapBuffers=FormatSupportsDoubleBuffering(pixelFormat);
			usehardware=FormatSupportsAcceleration(pixelFormat);

			//Create rendering context
			renderContext = Wgl.wglCreateContext(deviceContext);

			if(renderContext == IntPtr.Zero) {
				throw new Exception("CreateContexts: Unable to create an OpenGL rendering context");
			}

			//Make this the current context
			MakeCurrentContext();
			return selectedFormat;
		}
        public override void Create(string name, int width, int height, int colorDepth, bool isFullScreen, int left, int top, bool depthBuffer, params object[] miscParams)
        {
            // see if a OpenGLContext has been created yet
            if(hDC == IntPtr.Zero) {
                // grab the current display settings
                User.EnumDisplaySettings(null, User.ENUM_CURRENT_SETTINGS, out intialScreenSettings);

                if(isFullScreen) {
                    Gdi.DEVMODE screenSettings = new Gdi.DEVMODE();
                    screenSettings.dmSize = (short)Marshal.SizeOf(screenSettings);
                    screenSettings.dmPelsWidth = width;                         // Selected Screen Width
                    screenSettings.dmPelsHeight = height;                       // Selected Screen Height
                    screenSettings.dmBitsPerPel = colorDepth;                         // Selected Bits Per Pixel
                    screenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                    // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
                    int result = User.ChangeDisplaySettings(ref screenSettings, User.CDS_FULLSCREEN);

                    if(result != User.DISP_CHANGE_SUCCESSFUL) {
                        throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to change user display settings.");
                    }
                }

                // grab the HWND from the supplied target control
                hWnd = (IntPtr)((Control)this.Handle).Handle;

                Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
                pfd.nSize = (short)Marshal.SizeOf(pfd);
                pfd.nVersion = 1;
                pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |
                    Gdi.PFD_SUPPORT_OPENGL |
                    Gdi.PFD_DOUBLEBUFFER;
                pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA;
                pfd.cColorBits = (byte) colorDepth;
                pfd.cDepthBits = 32;
                // TODO: Find the best setting and use that
                pfd.cStencilBits = 8;
                pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE;

                // get the device context
                hDC = User.GetDC(hWnd);

                if(hDC == IntPtr.Zero) {
                    throw new Exception("Cannot create a GL device context.");
                }

                // attempt to find an appropriate pixel format
                int pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);

                if(pixelFormat == 0) {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to find a suitable pixel format.");
                }

                if(!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd)) {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to set the pixel format.");
                }

                // attempt to get the rendering context
                hRC = Wgl.wglCreateContext(hDC);

                if(hRC == IntPtr.Zero) {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to create a GL rendering context.");
                }

                if(!Wgl.wglMakeCurrent(hDC, hRC)) {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to activate the GL rendering context.");
                }

                // init the GL context
                Gl.glShadeModel(Gl.GL_SMOOTH);							// Enable Smooth Shading
                Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
                Gl.glClearDepth(1.0f);									// Depth Buffer Setup
                Gl.glEnable(Gl.GL_DEPTH_TEST);							// Enables Depth Testing
                Gl.glDepthFunc(Gl.GL_LEQUAL);								// The Type Of Depth Testing To Do
                Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);	// Really Nice Perspective Calculations
            }

            // set the params of the window
            // TODO: deal with depth buffer
            this.Name = name;
            this.colorDepth = colorDepth;
            this.width = width;
            this.height = height;
            this.isFullScreen = isFullScreen;
            this.top = top;
            this.left = left;

            // make this window active
            this.isActive = true;
        }
Example #10
0
        private void _initializeWgl()
        {
            // wglGetProcAddress does not work without an active OpenGL context,
            // but we need wglChoosePixelFormatARB's address before we can
            // create our main window.  Thank you very much, Microsoft!
            //
            // The solution is to create a dummy OpenGL window first, and then
            // test for WGL_ARB_pixel_format support.  If it is not supported,
            // we make sure to never call the ARB pixel format functions.
            //
            // If is is supported, we call the pixel format functions at least once
            // to initialize them (pointers are stored by glprocs.h).  We can also
            // take this opportunity to enumerate the valid FSAA modes.

            SWF.Form frm = new SWF.Form();
            IntPtr hwnd = frm.Handle;

            // if a simple CreateWindow fails, then boy are we in trouble...
            if (hwnd == IntPtr.Zero)
                throw new Exception("Window creation failed");

            // no chance of failure and no need to release thanks to CS_OWNDC
            IntPtr hdc = User.GetDC(hwnd);

            // assign a simple OpenGL pixel format that everyone supports
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
            ;
            pfd.nSize = (short)Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.cColorBits = 16;
            pfd.cDepthBits = 15;
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType = Gdi.PFD_TYPE_RGBA;

            // if these fail, wglCreateContext will also quietly fail
            int format;
            format = Gdi.ChoosePixelFormat(hdc, ref pfd);
            if (format != 0)
                Gdi.SetPixelFormat(hdc, format, ref pfd);

            IntPtr hrc = Wgl.wglCreateContext(hdc);
            if (hrc != IntPtr.Zero)
            {
                // if wglMakeCurrent fails, wglGetProcAddress will return null
                Wgl.wglMakeCurrent(hdc, hrc);
                Wgl.ReloadFunctions(); // Tao 2.0

                // check for pixel format and multisampling support

                //IntPtr wglGetExtensionsStringARB = Wgl.wglGetProcAddress( "wglGetExtensionsStringARB" );
                //if ( wglGetExtensionsStringARB != IntPtr.Zero )
                //{
                //    string exts = Wgl.wglGetExtensionsStringARB( wglGetExtensionsStringARB, hdc );
                //    _hasPixelFormatARB = exts.Contains( "WGL_ARB_pixel_format" );
                //    _hasMultisample = exts.Contains( "WGL_ARB_multisample" );
                //}

                _hasPixelFormatARB = Wgl.IsExtensionSupported("WGL_ARB_pixel_format");
                _hasMultisample = Wgl.IsExtensionSupported("WGL_ARB_multisample");

                if (_hasPixelFormatARB && _hasMultisample)
                {
                    // enumerate all formats w/ multisampling
                    int[] iattr = {
				        Wgl.WGL_DRAW_TO_WINDOW_ARB, 1,
				        Wgl.WGL_SUPPORT_OPENGL_ARB, 1,
				        Wgl.WGL_DOUBLE_BUFFER_ARB, 1,
				        Wgl.WGL_SAMPLE_BUFFERS_ARB, 1,
				        Wgl.WGL_ACCELERATION_ARB, Wgl.WGL_FULL_ACCELERATION_ARB,
				        // We are no matter about the colour, depth and stencil buffers here
				        //WGL_COLOR_BITS_ARB, 24,
				        //WGL_ALPHA_BITS_ARB, 8,
				        //WGL_DEPTH_BITS_ARB, 24,
				        //WGL_STENCIL_BITS_ARB, 8,
				        //
				        Wgl.WGL_SAMPLES_ARB, 2,
				        0
				    };
                    int[] formats = new int[256];
                    int[] count = new int[256]; // Tao 2.0
                    //int count;
                    // cheating here.  wglChoosePixelFormatARB proc address needed later on
                    // when a valid GL context does not exist and glew is not initialized yet.
                    _wglChoosePixelFormatARB = Wgl.wglGetProcAddress("wglChoosePixelFormatARB");
                    if (Wgl.wglChoosePixelFormatARB(hdc, iattr, null, 256, formats, count)) // Tao 2.0
                    //if ( Wgl.wglChoosePixelFormatARB( _wglChoosePixelFormatARB, hdc, iattr, null, 256, formats, out count ) != 0 )
                    {
                        // determine what multisampling levels are offered
                        int query = Wgl.WGL_SAMPLES_ARB, samples;
                        for (int i = 0; i < count[0]; ++i) // Tao 2.0
                        //for ( int i = 0; i < count[ 0 ]; ++i )
                        {
                            IntPtr wglGetPixelFormatAttribivARB = Wgl.wglGetProcAddress("wglGetPixelFormatAttribivARB");
                            if (Wgl.wglGetPixelFormatAttribivARB(hdc, formats[i], 0, 1, ref query, out samples)) // Tao 2.0
                            //if ( Wgl.wglGetPixelFormatAttribivARB( wglGetPixelFormatAttribivARB, hdc, formats[ i ], 0, 1, ref query, out samples ) != 0 )
                            {
                                if (!_fsaaLevels.Contains(samples))
                                    _fsaaLevels.Add(samples);
                            }
                        }
                    }
                }

                Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                Wgl.wglDeleteContext(hrc);
            }

            // clean up our dummy window and class
            frm.Dispose();
            frm = null;
        }
Example #11
0
        public bool SelectPixelFormat(IntPtr hdc, int colorDepth, int multisample)
        {
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();
            pfd.nSize = (short)Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
            pfd.cColorBits = (byte)((colorDepth > 16) ? 24 : colorDepth);
            pfd.cAlphaBits = (byte)((colorDepth > 16) ? 8 : 0);
            pfd.cDepthBits = 24;
            pfd.cStencilBits = 8;

            int[] format = new int[1];

            if (multisample != 0)
            {
                // only available with driver support
                if (!_hasMultisample || !_hasPixelFormatARB)
                    return false;

                int[] iattr = {
					Wgl.WGL_DRAW_TO_WINDOW_ARB, 1,
					Wgl.WGL_SUPPORT_OPENGL_ARB, 1,
					Wgl.WGL_DOUBLE_BUFFER_ARB, 1,
					Wgl.WGL_SAMPLE_BUFFERS_ARB, 1,
					Wgl.WGL_ACCELERATION_ARB, Wgl.WGL_FULL_ACCELERATION_ARB,
					Wgl.WGL_COLOR_BITS_ARB, pfd.cColorBits,
					Wgl.WGL_ALPHA_BITS_ARB, pfd.cAlphaBits,
					Wgl.WGL_DEPTH_BITS_ARB, pfd.cDepthBits,
					Wgl.WGL_STENCIL_BITS_ARB, pfd.cStencilBits,
					Wgl.WGL_SAMPLES_ARB, multisample,
					0
				};

                int[] nformats = new int[1];
                //int nformats;
                Debug.Assert(_wglChoosePixelFormatARB != null, "failed to get proc address for ChoosePixelFormatARB");
                // ChoosePixelFormatARB proc address was obtained when setting up a dummy GL context in initialiseWGL()
                // since glew hasn't been initialized yet, we have to cheat and use the previously obtained address
                bool result = Wgl.wglChoosePixelFormatARB(hdc, iattr, null, 1, format, nformats); // Tao 2.0
                //int result = Wgl.wglChoosePixelFormatARB( _wglChoosePixelFormatARB, hdc, iattr, null, 1, format, out nformats );
                if (!result || nformats[0] <= 0) // Tao 2.0
                    //if ( result == 0 || nformats <= 0 )
                    return false;
            }
            else
            {
                format[0] = Gdi.ChoosePixelFormat(hdc, ref pfd);
            }

            return (format[0] != 0 && Gdi.SetPixelFormat(hdc, format[0], ref pfd));
        }
Example #12
0
        /// <summary>
        ///     Creates the OpenGL contexts.
        /// </summary>
        public void InitializeContexts() {
            int pixelFormat;                                                // Holds the selected pixel format

            windowHandle = this.Handle;                                     // Get window handle

            if(windowHandle == IntPtr.Zero) {                               // No window handle means something is wrong
                MessageBox.Show("Window creation error.  No window handle.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();// The pixel format descriptor
            pfd.nSize = (short) Marshal.SizeOf(pfd);                        // Size of the pixel format descriptor
            pfd.nVersion = 1;                                               // Version number (always 1)
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |                          // Format must support windowed mode
                        Gdi.PFD_SUPPORT_OPENGL |                            // Format must support OpenGL
                        Gdi.PFD_DOUBLEBUFFER;                               // Must support double buffering
            pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA;                      // Request an RGBA format
            pfd.cColorBits = (byte) colorBits;                              // Select our color depth
            pfd.cRedBits = 0;                                               // Individual color bits ignored
            pfd.cRedShift = 0;
            pfd.cGreenBits = 0;
            pfd.cGreenShift = 0;
            pfd.cBlueBits = 0;
            pfd.cBlueShift = 0;
            pfd.cAlphaBits = 0;                                             // No alpha buffer
            pfd.cAlphaShift = 0;                                            // Alpha shift bit ignored
            pfd.cAccumBits = accumBits;                                     // Accumulation buffer
            pfd.cAccumRedBits = 0;                                          // Individual accumulation bits ignored
            pfd.cAccumGreenBits = 0;
            pfd.cAccumBlueBits = 0;
            pfd.cAccumAlphaBits = 0;
            pfd.cDepthBits = depthBits;                                     // Z-buffer (depth buffer)
            pfd.cStencilBits = stencilBits;                                 // No stencil buffer
            pfd.cAuxBuffers = 0;                                            // No auxiliary buffer
            pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE;                     // Main drawing layer
            pfd.bReserved = 0;                                              // Reserved
            pfd.dwLayerMask = 0;                                            // Layer masks ignored
            pfd.dwVisibleMask = 0;
            pfd.dwDamageMask = 0;

            deviceContext = User.GetDC(windowHandle);                       // Attempt to get the device context
            if(deviceContext == IntPtr.Zero) {                              // Did we not get a device context?
                MessageBox.Show("Can not create a GL device context.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            pixelFormat = Gdi.ChoosePixelFormat(deviceContext, ref pfd);    // Attempt to find an appropriate pixel format
            if(pixelFormat == 0) {                                          // Did windows not find a matching pixel format?
                MessageBox.Show("Can not find a suitable PixelFormat.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            if(!Gdi.SetPixelFormat(deviceContext, pixelFormat, ref pfd)) {  // Are we not able to set the pixel format?
                MessageBox.Show("Can not set the chosen PixelFormat.  Chosen PixelFormat was " + pixelFormat + ".", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            renderingContext = Wgl.wglCreateContext(deviceContext);         // Attempt to get the rendering context
            if(renderingContext == IntPtr.Zero) {                           // Are we not able to get a rendering context?
                MessageBox.Show("Can not create a GL rendering context.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }

            MakeCurrent();                                                  // Attempt to activate the rendering context

            // Force A Reset On The Working Set Size
            Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
        }
Example #13
0
        protected Gdi.PIXELFORMATDESCRIPTOR GetPixelFormat()
        {
            Gdi.PIXELFORMATDESCRIPTOR lRet = new Gdi.PIXELFORMATDESCRIPTOR();
              lRet.nSize = (short)Marshal.SizeOf(lRet);
              lRet.nVersion = 1;
              lRet.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |
              Gdi.PFD_SUPPORT_OPENGL |
              Gdi.PFD_DOUBLEBUFFER;
              lRet.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
              lRet.cColorBits = 16;
              lRet.cRedBits = 0;
              lRet.cRedShift = 0;
              lRet.cGreenBits = 0;
              lRet.cGreenShift = 0;
              lRet.cBlueBits = 0;
              lRet.cBlueShift = 0;
              lRet.cAlphaBits = 0;
              lRet.cAlphaShift = 0;
              lRet.cAccumBits = 0;
              lRet.cAccumRedBits = 0;
              lRet.cAccumGreenBits = 0;
              lRet.cAccumBlueBits = 0;
              lRet.cAccumAlphaBits = 0;
              lRet.cDepthBits = 16;
              lRet.cStencilBits = 0;
              lRet.cAuxBuffers = 0;
              lRet.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;
              lRet.bReserved = 0;
              lRet.dwLayerMask = 0;
              lRet.dwVisibleMask = 0;
              lRet.dwDamageMask = 0;

              return lRet;
        }
Example #14
0
        void AttachGLToWindow(/*FlurryAnimateChildInfo *child*/)
        {
            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();//HACK
            pfd.nSize = (short)System.Runtime.InteropServices.Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL;
            pfd.iPixelType = Gdi.PFD_TYPE_RGBA;
            pfd.iLayerType = Gdi.PFD_MAIN_PLANE;
            pfd.cDepthBits = 16;
            pfd.cColorBits = 24;
            #if !USEFADEHACK
            if (Types.iSettingBufferMode != Types.BUFFER.BUFFER_MODE_SINGLE)
                pfd.dwFlags |= Gdi.PFD_DOUBLEBUFFER;
            #endif
            hdc = User.GetDC(this.Handle);
            //Win32.GDI.SetBkColor(hdc, 0);
            int iPixelFormat = Gdi.ChoosePixelFormat(hdc, ref pfd);

            Gdi.SetPixelFormat(hdc, iPixelFormat, ref pfd);

            // then use this to create a rendering context
            hglrc = Wgl.wglCreateContext(hdc);
            Wgl.wglMakeCurrent(hdc, hglrc);

            // tell Flurry to use the whole window as viewport
            //GetClientRect(child->hWnd, &rc);
            flgroup.SetSize(this.Width, this.Height);

            // some nice debug output
            //_RPT4(_CRT_WARN, "  child 0x%08x: hWnd 0x%08x, hdc 0x%08x, hglrc 0x%08x\n",
            //    child, child->hWnd, child->hdc, child->hglrc);
            //_RPT1(_CRT_WARN, "  GL vendor:     %s\n", glGetString(GL_VENDOR));
            //_RPT1(_CRT_WARN, "  GL renderer:   %s\n", glGetString(GL_RENDERER));
            //_RPT1(_CRT_WARN, "  GL version:    %s\n", glGetString(GL_VERSION));
            //_RPT1(_CRT_WARN, "  GL extensions: %s\n", glGetString(GL_EXTENSIONS));
            //_RPT0(_CRT_WARN, "\n");
        }
Example #15
0
        public bool Setup(Form mainForm, int width, int height, int bits, bool isfullscreen)
        {
            int pixelFormat;

            this.IsFullscreen = isfullscreen;
            this.MainForm = mainForm;

            GC.Collect();
            Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);

            Gl.glShadeModel(Gl.GL_SMOOTH);

            if (IsFullscreen)
            {
                Gdi.DEVMODE devMode = new Gdi.DEVMODE();
                devMode.dmSize = (short)Marshal.SizeOf(devMode);
                devMode.dmPelsWidth = width;
                devMode.dmPelsHeight = height;
                devMode.dmBitsPerPel = bits;
                devMode.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                if (User.ChangeDisplaySettings(ref devMode, User.CDS_FULLSCREEN) != User.DISP_CHANGE_SUCCESSFUL)
                {
                    IsFullscreen = false;
                }

            }

            if (IsFullscreen)
            {
                MainForm.FormBorderStyle = FormBorderStyle.None;
            }
            else
            {
                MainForm.FormBorderStyle = FormBorderStyle.Sizable;
            }

            MainForm.Width = width;
            MainForm.Height = height;

            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();

            pfd.nSize = (short)Marshal.SizeOf(pfd);
            pfd.nVersion = 1;
            pfd.dwFlags = Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_DOUBLEBUFFER;
            pfd.iPixelType = (byte)Gdi.PFD_TYPE_RGBA;
            pfd.cColorBits = (byte)bits;
            pfd.cAccumAlphaBits = 0;
            pfd.cAccumBits = 0;
            pfd.cAccumBlueBits = 0;
            pfd.cAccumGreenBits = 0;
            pfd.cAccumRedBits = 0;
            pfd.cAlphaBits = 0;
            pfd.cAlphaShift = 0;
            pfd.cAuxBuffers = 0;
            pfd.cBlueBits = 0;
            pfd.cBlueShift = 0;
            pfd.cDepthBits = 16;    // depth
            pfd.cGreenBits = 0;
            pfd.cGreenShift = 0;
            pfd.cRedBits = 0;
            pfd.cRedShift = 0;
            pfd.cStencilBits = 0;   // stencil
            pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE;
            pfd.bReserved = 0;
            pfd.dwDamageMask = 0;
            pfd.dwLayerMask = 0;
            pfd.dwVisibleMask = 0;

            hDC = User.GetDC(MainForm.Handle);
            if (hDC == IntPtr.Zero)
            {
                Deinitialize();
                return false;
            }

            pixelFormat = Gdi.ChoosePixelFormat(hDC, ref pfd);
            if (pixelFormat == 0)
            {
                Deinitialize();
                return false;
            }

            if (!Gdi.SetPixelFormat(hDC, pixelFormat, ref pfd))
            {
                Deinitialize();
                return false;
            }

            hRC = Wgl.wglCreateContext(hDC);    // ako imamo validan hdc onda postavi rendering context
            if (hRC == IntPtr.Zero)
            {
                Deinitialize();
                return false;
            }

            if (!Wgl.wglMakeCurrent(hDC, hRC))
            {
                Deinitialize();
                return false;
            }

            MainForm.TopMost = true;
            MainForm.Focus();

            if (IsFullscreen)
            {
                Cursor.Hide();
            }

            ResizeScene(width, height);

            if (!Initialize())
            {
                Deinitialize();
                return false;
            }

            return true;
        }
Example #16
0
        private void CreateDevice(int width, int height, int bits)
        {
            int pixelFormat;  // Holds The Results After Searching For A Match
            //this.Fullscreen = fullscreenflag;

            GC.Collect();  // Request A Collection
            // This Forces A Swap
            Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);

            if(this.m_bFullscreen) // Attempt Fullscreen Mode?
            {
                Gdi.DEVMODE dmScreenSettings = new Gdi.DEVMODE();               // Device Mode
                // Size Of The Devmode Structure
                dmScreenSettings.dmSize = (short) Marshal.SizeOf(dmScreenSettings);
                dmScreenSettings.dmPelsWidth = width;                           // Selected Screen Width
                dmScreenSettings.dmPelsHeight = height;                         // Selected Screen Height
                dmScreenSettings.dmBitsPerPel = bits;                           // Selected Bits Per Pixel
                dmScreenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
                if(User.ChangeDisplaySettings(ref dmScreenSettings, User.CDS_FULLSCREEN) != User.DISP_CHANGE_SUCCESSFUL)
                {
                    // The Mode Fails
                    throw new Exception("Fullscreen not supported");
                }
            }

            Gdi.PIXELFORMATDESCRIPTOR pfd = new Gdi.PIXELFORMATDESCRIPTOR();    // pfd Tells Windows How We Want Things To Be
            pfd.nSize = (short) Marshal.SizeOf(pfd);                            // Size Of This Pixel Format Descriptor
            pfd.nVersion = 1;                                                   // Version Number
            pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW |                              // Format Must Support Window
                Gdi.PFD_SUPPORT_OPENGL |                                        // Format Must Support OpenGL
                Gdi.PFD_DOUBLEBUFFER;                                           // Format Must Support Double Buffering
            pfd.iPixelType = (byte) Gdi.PFD_TYPE_RGBA;                          // Request An RGBA Format
            pfd.cColorBits = (byte) bits;                                       // Select Our Color Depth
            pfd.cRedBits = 0;                                                   // Color Bits Ignored
            pfd.cRedShift = 0;
            pfd.cGreenBits = 0;
            pfd.cGreenShift = 0;
            pfd.cBlueBits = 0;
            pfd.cBlueShift = 0;
            pfd.cAlphaBits = 0;                                                 // No Alpha Buffer
            pfd.cAlphaShift = 0;                                                // Shift Bit Ignored
            pfd.cAccumBits = 0;                                                 // No Accumulation Buffer
            pfd.cAccumRedBits = 0;                                              // Accumulation Bits Ignored
            pfd.cAccumGreenBits = 0;
            pfd.cAccumBlueBits = 0;
            pfd.cAccumAlphaBits = 0;
            pfd.cDepthBits = 16;                                                // 16Bit Z-Buffer (Depth Buffer)
            pfd.cStencilBits = 0;                                               // No Stencil Buffer
            pfd.cAuxBuffers = 0;                                                // No Auxiliary Buffer
            pfd.iLayerType = (byte) Gdi.PFD_MAIN_PLANE;                         // Main Drawing Layer
            pfd.bReserved = 0;                                                  // Reserved
            pfd.dwLayerMask = 0;                                                // Layer Masks Ignored
            pfd.dwVisibleMask = 0;
            pfd.dwDamageMask = 0;

            this._hDC = User.GetDC(this.RenderControl.Handle); // Attempt To Get A Device Context
            if(this._hDC == IntPtr.Zero)
            {
                this.KillGLWindow();
                throw new Exception("Can't Create A GL Device Context.");
            }

            pixelFormat = Gdi.ChoosePixelFormat(this._hDC, ref pfd);                  // Attempt To Find An Appropriate Pixel Format
            if(pixelFormat == 0)
            {                                              // Did Windows Find A Matching Pixel Format?
                this.KillGLWindow();
                throw new Exception("Can't Find A Suitable PixelFormat.");
            }

            if(!Gdi.SetPixelFormat(this._hDC, pixelFormat, ref pfd)) // Are We Able To Set The Pixel Format?
            {
                this.KillGLWindow();  // Reset The Display
                throw new Exception("Can't Set The PixelFormat.");
            }

            this._hRC = Wgl.wglCreateContext(this._hDC);  // Attempt To Get The Rendering Context
            if(this._hRC == IntPtr.Zero)
            {
                this.KillGLWindow();
                throw new Exception("Can't Create A GL Rendering Context.");
            }

            if(!Wgl.wglMakeCurrent(this._hDC, this._hRC)) // Try To Activate The Rendering Context
            {
                this.KillGLWindow(); // Reset The Display
                throw new Exception("Can't Activate The GL Rendering Context.");
            }

            //form.Show();
            //form.TopMost = true;
            //form.Focus();

            this.ReSizeGLScene(this.RenderControl.Width, this.RenderControl.Height);                                       // Set Up Our Perspective GL Screen

            if(!InitGL())
            {                                                     // Initialize Our Newly Created GL Window
                this.KillGLWindow();                                                 // Reset The Display
                throw new Exception("Initialization Failed.");
            }
        }