protected virtual void InitializeGL() { m_uint_HWND = (uint)p.Handle.ToInt32(); m_uint_DC = WGL.GetDC(m_uint_HWND); // Not doing the following WGL.wglSwapBuffers() on the DC will // result in a failure to subsequently create the RC. WGL.wglSwapBuffers(m_uint_DC); WGL.PIXELFORMATDESCRIPTOR pfd = new WGL.PIXELFORMATDESCRIPTOR(); WGL.ZeroPixelDescriptor(ref pfd); pfd.nVersion = 1; pfd.dwFlags = (WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL | WGL.PFD_DOUBLEBUFFER); pfd.iPixelType = (byte)(WGL.PFD_TYPE_RGBA); pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = (byte)(WGL.PFD_MAIN_PLANE); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //for Stencil support pfd.cStencilBits = 32; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! int pixelFormatIndex = 0; pixelFormatIndex = WGL.ChoosePixelFormat(m_uint_DC, ref pfd); if (pixelFormatIndex == 0) { MessageBox.Show("Unable to retrieve pixel format"); return; } if (WGL.SetPixelFormat(m_uint_DC, pixelFormatIndex, ref pfd) == 0) { MessageBox.Show("Unable to set pixel format"); return; } //Create rendering context m_uint_RC = WGL.wglCreateContext(m_uint_DC); if (m_uint_RC == 0) { MessageBox.Show("Unable to get rendering context"); return; } if (WGL.wglMakeCurrent(m_uint_DC, m_uint_RC) == 0) { MessageBox.Show("Unable to make rendering context current"); return; } initRenderingGL(); }
/// <summary> /// Event handler called when the form is loaded. It retrieves the controls /// window handle and device context and creates the rendering context. /// </summary> protected virtual void InitializeGL(object sender, EventArgs e) { m_uint_HWND = (uint)this.Handle.ToInt32(); m_uint_DC = WGL.GetDC(m_uint_HWND); // CAUTION: Not doing the following WGL.wglSwapBuffers() on the DC will // result in a failure to subsequently create the RC. WGL.wglSwapBuffers(m_uint_DC); //Get the pixel format WGL.PIXELFORMATDESCRIPTOR pfd = new WGL.PIXELFORMATDESCRIPTOR(); WGL.ZeroPixelDescriptor(ref pfd); pfd.nVersion = 1; pfd.dwFlags = (WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL | WGL.PFD_DOUBLEBUFFER); pfd.iPixelType = (byte)(WGL.PFD_TYPE_RGBA); pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = (byte)(WGL.PFD_MAIN_PLANE); int pixelFormatIndex = 0; pixelFormatIndex = WGL.ChoosePixelFormat(m_uint_DC, ref pfd); if (pixelFormatIndex == 0) { MessageBox.Show("Unable to retrieve pixel format"); return; } if (WGL.SetPixelFormat(m_uint_DC, pixelFormatIndex, ref pfd) == 0) { MessageBox.Show("Unable to set pixel format"); return; } //Create rendering context m_uint_RC = WGL.wglCreateContext(m_uint_DC); if (m_uint_RC == 0) { MessageBox.Show("Unable to get rendering context"); return; } if (WGL.wglMakeCurrent(m_uint_DC, m_uint_RC) == 0) { MessageBox.Show("Unable to make rendering context current"); return; } //Set up OpenGL related characteristics ResizeGL(null, null); GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL.glEnable(GL.GL_DEPTH_TEST); GL.glDepthFunc(GL.GL_LEQUAL); //GL.glEnable(GL.GL_CULL_FACE); GL.glCullFace(GL.GL_BACK); }
// DWORD GetLastError(VOID); // ############################################################################ // ############################################################################ // ############################################################################ // ############################################################################ // ============================================================================ // The following function, DemoCreateRenderingContext(ref_uint_DC,ref_uint_RC), // can be used as a simple way to create an OpenGL "Rendering Context" (RC). // **** DO NOT CALL DemoCreateRenderingContext() DIRECTLY IF YOU CHOOSE TO // CALL DemoInitOpenGL() (below) TO ESTABLISH OPENGL. **** // ============================================================================ public static unsafe void DemoCreateRenderingContext ( ref uint ref_uint_DC, ref uint ref_uint_RC ) { ref_uint_RC = 0; PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR(); // -------------------------------------------------------------------------- pfd.nSize = 40; // sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER); pfd.iPixelType = (byte)(PFD_TYPE_RGBA); pfd.cColorBits = 32; 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 = 32; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = (byte)(PFD_MAIN_PLANE); pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Choose Pixel Format // -------------------------------------------------------------------------- int iPixelFormat = 0; PIXELFORMATDESCRIPTOR *_pfd = &pfd; iPixelFormat = WGL.ChoosePixelFormat(ref_uint_DC, _pfd); if (0 == iPixelFormat) { uint uint_LastError = WGL.GetLastError(); string string_Message = "ChoosePixelFormat() FAILED: Error: " + uint_LastError; WGL.MessageBox(0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Set Pixel Format // -------------------------------------------------------------------------- int int_Result_SPF = 0; int_Result_SPF = WGL.SetPixelFormat(ref_uint_DC, iPixelFormat, _pfd); if (0 == int_Result_SPF) { uint uint_LastError = WGL.GetLastError(); string string_Message = "SetPixelFormat() FAILED. Error: " + uint_LastError; WGL.MessageBox(0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Create Rendering Context (RC) // NOTE: You will get the following error: // 126 : ERROR_MOD_NOT_FOUND // if you attempt to create a render context too soon after creating a // window and getting its Device Context (DC). // See the comments for WGL.DemoInitOpenGL() on how to use a call to // WGL.wglSwapBuffers( ref_uint_DC ) before attempting to create the RC. // -------------------------------------------------------------------------- ref_uint_RC = WGL.wglCreateContext(ref_uint_DC); if (0 == ref_uint_RC) { uint uint_LastError = WGL.GetLastError(); string string_Message = "wglCreateContext() FAILED. Error: " + uint_LastError; WGL.MessageBox(0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK); return; } // -------------------------------------------------------------------------- // -------------------------------------------------------------------------- // Make the new Render Context (RC) the current Render Context (RC) // -------------------------------------------------------------------------- int int_Result_MC = 0; int_Result_MC = WGL.wglMakeCurrent(ref_uint_DC, ref_uint_RC); if (0 == int_Result_MC) { uint uint_LastError = WGL.GetLastError(); string string_Message = "wglMakeCurrent() FAILED. Error: " + uint_LastError; WGL.MessageBox(0, string_Message, "WGL.DemoGetRenderingContext() : ERROR", MB_OK); // *************************** WGL.wglDeleteContext(ref_uint_RC); ref_uint_RC = 0; // *************************** return; } // -------------------------------------------------------------------------- }