Beispiel #1
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
        }
Beispiel #2
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
    }
        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;
        }
Beispiel #4
0
		public override void Create( string name, int width, int height, bool isFullScreen, NamedParameterList miscParams )
		{
			if ( _hWindow != IntPtr.Zero )
				dispose( true );

			_hWindow = IntPtr.Zero;
			this.name = name;
			this.IsFullScreen = isFullScreen;
			this._isClosed = false;

			// load window defaults
			this.left = this.top = -1; // centered
			this.width = width;
			this.height = height;
			this._displayFrequency = 0;
			this.isDepthBuffered = true;
			this.colorDepth = IsFullScreen ? 32 : 16;

			IntPtr parentHwnd = IntPtr.Zero;
			string title = name;
			bool vsync = false;
			int fsaa = 0;
			string border = "";
			bool outerSize = false;

			#region Parameter Handling
			if ( miscParams != null )
			{
				foreach ( KeyValuePair<string, object> entry in miscParams )
				{
					switch ( entry.Key )
					{
						case "title":
							title = entry.Value.ToString();
							break;
						case "left":
							left = Int32.Parse( entry.Value.ToString() );
							break;
						case "top":
							top = Int32.Parse( entry.Value.ToString() );
							break;
						case "depthBuffer":
							isDepthBuffered = bool.Parse( entry.Value.ToString() );
							break;
						case "vsync":
							vsync = entry.Value.ToString() == "Yes" ? true : false;
							break;
						case "fsaa":
							fsaa = Int32.Parse( entry.Value.ToString() );
							break;
						case "externalWindowHandle":
							_hWindow = (IntPtr)entry.Value;
							if ( _hWindow != IntPtr.Zero )
							{
								_isExternal = true;
								IsFullScreen = false;
							}
							break;
						case "externalGLControl":
							break;
						case "border":
                            border = ( (string)miscParams[ "border" ] ).ToLower(); 
							break;
						case "outerDimensions":
							break;
						case "displayFrequency":
							if ( IsFullScreen )
								_displayFrequency = Int32.Parse( entry.Value.ToString() );
							break;
						case "colorDepth":
							if ( IsFullScreen )
								colorDepth = Int32.Parse( entry.Value.ToString() );
							break;
						case "parentWindowHandle":
							if ( !IsFullScreen )
								parentHwnd = (IntPtr)entry.Value;
							break;
						default:
							break;
					}
				}
			}
			#endregion Parameter Handling

			if ( !_isExternal )
			{
				DefaultForm form = new DefaultForm();

				form.ClientSize = new System.Drawing.Size( width, height );
				form.MaximizeBox = false;
				form.MinimizeBox = false;
				form.StartPosition = SWF.FormStartPosition.CenterScreen;

				if ( IsFullScreen )
				{
					// Set the display to the desired resolution
					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." );
					}

					// Adjust form to size the screen
					form.Top = 0;
					form.Left = 0;
					form.FormBorderStyle = SWF.FormBorderStyle.None;
					form.WindowState = SWF.FormWindowState.Maximized;
#if !DEBUG
					form.TopMost = true;
					form.TopLevel = true;
#endif
				}
				else
				{
					if ( parentHwnd != IntPtr.Zero )
					{
						form.Owner = (SWF.Form)SWF.Control.FromHandle( parentHwnd );
					}
					else
					{
                        if ( border == "none" )
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.None;
                        }
                        else if ( border == "fixed" )
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
                            form.MaximizeBox = false;
                        }
                    }

					form.Top = top;
					form.Left = left;
					//form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
					form.WindowState = SWF.FormWindowState.Normal;
					form.Text = title;
				}

                WindowEventMonitor.Instance.RegisterWindow( this );

				form.RenderWindow = this;
				_hWindow = form.Handle;
				form.Show();

			}

			IntPtr old_hdc = Wgl.wglGetCurrentDC();
			IntPtr old_context = Wgl.wglGetCurrentContext();

			SWF.Control ctrl = SWF.Form.FromHandle( _hWindow );
			//Form frm = (Form)ctrl.TopLevelControl;
			this.top = ctrl.Top;
			this.left = ctrl.Left;
			this.width = ctrl.ClientRectangle.Width;
			this.height = ctrl.ClientRectangle.Height;

			_hDeviceContext = User.GetDC( _hWindow );

			// Do not change vsync if the external window has the OpenGL control
			if ( !_isExternalGLControl )
			{
				if ( !_glSupport.SelectPixelFormat( _hDeviceContext, ColorDepth, fsaa ) )
				{
					if ( fsaa == 0 )
						throw new Exception( "selectPixelFormat failed" );

					LogManager.Instance.Write( "FSAA level not supported, falling back" );
					if ( !_glSupport.SelectPixelFormat( _hDeviceContext, ColorDepth, 0 ) )
						throw new Exception( "selectPixelFormat failed" );
				}
			}

			// attempt to get the rendering context
			_hRenderingContext = Wgl.wglCreateContext( _hDeviceContext );

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

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

			// Do not change vsync if the external window has the OpenGL control
			if ( !_isExternalGLControl )
			{
				// Don't use wglew as if this is the first window, we won't have initialised yet
				//IntPtr wglSwapIntervalEXT = Wgl.wglGetProcAddress( "wglSwapIntervalEXT" );
				//if ( wglSwapIntervalEXT != IntPtr.Zero )
				//Wgl.wglSwapIntervalEXT( wglSwapIntervalEXT, vsync );
				if ( Wgl.IsExtensionSupported( "wglSwapIntervalEXT" ) )
					Wgl.wglSwapIntervalEXT( vsync ? 1 : 0 ); // Tao 2.0
			}

			if ( old_context != IntPtr.Zero )
			{
				// Restore old context
				if ( !Wgl.wglMakeCurrent( old_hdc, old_context ) )
					throw new Exception( "wglMakeCurrent() failed" );

				// Share lists with old context
				if ( !Wgl.wglShareLists( old_context, _hRenderingContext ) )
					throw new Exception( "wglShareLists() failed" );
			}

			// Create RenderSystem context
			_glContext = new Win32Context( _hDeviceContext, _hRenderingContext );

			// make this window active
			this.IsActive = true;
		}
Beispiel #5
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;
        }
Beispiel #6
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.");
            }
        }