// --- Private Static Methods --- #region bool CreateGLWindow(string title, int width, int height, int bits, bool fullscreenflag) /// <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 Lesson10(); // 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 }
void createGLWindow(string title) { int height = 400; int width = 600; int bpp = 16; int pixelFormat; form = null; GC.Collect(); Gdi.DEVMODE dmScreenSettings = new Gdi.DEVMODE(); dmScreenSettings.dmSize = (short)Marshal.SizeOf(dmScreenSettings); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel = bpp; dmScreenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT; form = new EngineOld();; //form.FormBorderStyle = FormBorderStyle.None; form.Width = width; // Set Window Width form.Height = height; // Set Window Height form.Text = 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)bpp; // 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; DC = User.GetDC(form.Handle); // Attempt To Get A Device Context if (DC == 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); //**errorHandler } // Attempt To Find An Appropriate Pixel Format pixelFormat = Gdi.ChoosePixelFormat(DC, ref pfd); 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); } if (!Gdi.SetPixelFormat(DC, 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); } RC = Wgl.wglCreateContext(DC); // Attempt To Get The Rendering Context if (RC == 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); } if (!Wgl.wglMakeCurrent(DC, RC)) { // Try To Activate The Rendering Context killGLWindow(); // Reset The Display MessageBox.Show("Can't Activate The GL Rendering Context.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } form.Show(); // Show The Window //form.TopMost = true; // Topmost Window form.Focus(); // Focus The Window InitLayout(); }
/// <summary> /// Creates and sets the pixel format and creates and connects the deviceContext and renderContext. /// </summary> internal void InitContexts() { int selectedPixelFormat; //Make sure the handle for this control has been created if (this.Handle == IntPtr.Zero) { throw new Exception("InitContexts: The control's window handle has not been created!"); } //Setup pixel format Gdi.PIXELFORMATDESCRIPTOR pixelFormat = new Gdi.PIXELFORMATDESCRIPTOR(); pixelFormat.nSize = (short)Marshal.SizeOf(pixelFormat); pixelFormat.nVersion = 1; pixelFormat.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER; pixelFormat.iPixelType = (byte)Gdi.PFD_TYPE_RGBA; pixelFormat.cColorBits = 32; pixelFormat.cRedBits = 0; pixelFormat.cRedShift = 0; pixelFormat.cGreenBits = 0; pixelFormat.cGreenShift = 0; pixelFormat.cBlueBits = 0; pixelFormat.cBlueShift = 0; pixelFormat.cAlphaBits = 0; pixelFormat.cAlphaShift = 0; pixelFormat.cAccumBits = 0; pixelFormat.cAccumRedBits = 0; pixelFormat.cAccumGreenBits = 0; pixelFormat.cAccumBlueBits = 0; pixelFormat.cAccumAlphaBits = 0; pixelFormat.cDepthBits = 16; pixelFormat.cStencilBits = 0; pixelFormat.cAuxBuffers = 0; pixelFormat.iLayerType = (byte)Gdi.PFD_MAIN_PLANE; pixelFormat.bReserved = 0; pixelFormat.dwLayerMask = 0; pixelFormat.dwVisibleMask = 0; pixelFormat.dwDamageMask = 0; //Create device context this.deviceContext = User.GetDC(this.Handle); if (this.deviceContext == IntPtr.Zero) { MessageBox.Show("InitContexts: Unable to create an OpenGL device context!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } //Choose the Pixel Format that is the closest to our pixelFormat selectedPixelFormat = Gdi.ChoosePixelFormat(this.deviceContext, ref pixelFormat); //Make sure the requested pixel format is available if (selectedPixelFormat == 0) { MessageBox.Show("InitContexts: Unable to find a suitable pixel format!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } //Sets the selected Pixel Format if (!Gdi.SetPixelFormat(this.deviceContext, selectedPixelFormat, ref pixelFormat)) { MessageBox.Show("InitContexts: Unable to set the requested pixel format!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } //Create rendering context this.renderContext = Wgl.wglCreateContext(this.deviceContext); if (this.renderContext == IntPtr.Zero) { MessageBox.Show("InitContexts: Unable to create an OpenGL rendering context!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } this.MakeCurrentContext(); }
public bool InitGLWindow(Control parent, int width, int height, int bits) { int pixelFormat; // Holds The Results After Searching For A Match this.parent = parent; GC.Collect(); // Request A Collection // This Forces A Swap Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1); 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(parent.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); } 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); // Everything Went OK }
/// <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); }
public static RenderFrame Create(string title, int width, int height, int bits, bool fullscreen) { int pixelFormat; // force a swap GC.Collect(); Kernel.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1); if (fullscreen) { Gdi.DEVMODE dmScreenSettings = new Gdi.DEVMODE(); dmScreenSettings.dmSize = (short)Marshal.SizeOf(dmScreenSettings); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel = bits; dmScreenSettings.dmFields = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT; if (User.ChangeDisplaySettings(ref dmScreenSettings, User.CDS_FULLSCREEN) != User.DISP_CHANGE_SUCCESSFUL) { // fullscreen not supported return(null); } } RenderFrame form = new RenderFrame(); form.FormBorderStyle = FormBorderStyle.None; form.Width = width; form.Height = height; form.Text = title; form.fullscreen = fullscreen; 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)bits; pfd.cRedBits = 0; pfd.cRedShift = 0; pfd.cGreenBits = 0; pfd.cGreenShift = 0; pfd.cBlueBits = 0; pfd.cBlueShift = 0; pfd.cAlphaBits = 0; pfd.cAlphaShift = 0; pfd.cAccumBits = 0; pfd.cAccumRedBits = 0; pfd.cAccumGreenBits = 0; pfd.cAccumBlueBits = 0; pfd.cAccumAlphaBits = 0; pfd.cDepthBits = 16; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = (byte)Gdi.PFD_MAIN_PLANE; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; form.hDC = User.GetDC(form.Handle); if (form.hDC == IntPtr.Zero) { form.kill(); throw new UserException("Can't Create A GL Device Context."); } pixelFormat = Gdi.ChoosePixelFormat(form.hDC, ref pfd); if (pixelFormat == 0) { form.kill(); throw new UserException("Can't Find A Suitable PixelFormat."); } if (!Gdi.SetPixelFormat(form.hDC, pixelFormat, ref pfd)) { form.kill(); throw new UserException("Can't Set The PixelFormat."); } form.hRC = Wgl.wglCreateContext(form.hDC); if (form.hRC == IntPtr.Zero) { form.kill(); throw new UserException("Can't Create A GL Rendering Context."); } if (!Wgl.wglMakeCurrent(form.hDC, form.hRC)) { form.kill(); throw new UserException("Can't Activate The GL Rendering Context."); } form.Show(); form.TopMost = true; form.Focus(); if (fullscreen) { Cursor.Hide(); } else { Cursor.Show(); } #if DEBUG form.Location = new System.Drawing.Point(0, 0); #endif return(form); }
float mag_speed, rotate_speed; //マウスの拡大縮小,回転速度 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(); // OpenGLをサポート ウィンドウに描画 ダブルバッファ pfd.dwFlags = Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_DOUBLEBUFFER; pfd.iPixelType = Gdi.PFD_TYPE_RGBA; //RGBAフォーマット pfd.cColorBits = 32; // 32bit/pixel pfd.cAlphaBits = 8; // アルファチャンネル8bit (0にするとアルファチャンネル無しになる) pfd.cDepthBits = 16; // デプスバッファ16bit pfd.cStencilBits = 8; //ステンシルバッファを利用するときはこれを追加しなければならない //デバイスコンテキストのハンドルを取得。 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"); } //OpenGLのレンダリングコンテキストを作成。 this.hRC = Wgl.wglCreateContext(this.hDC); 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.addModel("c:\\users\\taiki\\bmw.3ds"); camera.setViewPort(this.Width, this.Height); this.SetupGL(); }
/// <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(); }