/// <summary> /// All application initialization is performed here, before the main loop thread is executed and the render panel is displayed for the first time. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RenderForm_Load(object sender, EventArgs e) { OpenGL.PIXELFORMATDESCRIPTOR pixelformatdescriptor = new OpenGL.PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init(); dc = OpenGL.GetDC(renderPanel.Handle); int pixelFormat = OpenGL.ChoosePixelFormat(dc, ref pixelformatdescriptor); if (!OpenGL.SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor)) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } if ((hglrc = OpenGL.wglCreateContext(dc)) == IntPtr.Zero) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } if (!OpenGL.wglMakeCurrent(dc, hglrc)) { OpenGL.wglDeleteContext(hglrc); MessageBox.Show("Failed to init OpenGL context\nMake sure you have an OpenGL 2.0 compatible graphics card with the latest drivers installed!\nAlso verify if the pipeline config file exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(0); } if (!app.init()) { MessageBox.Show("Failed to init application\nMake sure you have an OpenGL 2.0 compatible graphics card with the latest drivers installed!\nAlso verify if the pipeline config file exists.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); h3d.release(); Environment.Exit(0); } app.resize(renderPanel.Size.Width, renderPanel.Size.Height); }
/// <summary> /// Создает контекст воспроизведения opengl /// </summary> protected virtual void CreateOpenGLContext() { OpenGL.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); hDC = OpenGL.GetDC(this.Handle); if (hDC == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } OpenGL.PIXELFORMATDESCRIPTOR pfd = new OpenGL.PIXELFORMATDESCRIPTOR(); pfd.Initialize(); pfd.dwFlags = OpenGL.PFD_DRAW_TO_WINDOW | OpenGL.PFD_SUPPORT_OPENGL | OpenGL.PFD_DOUBLEBUFFER; int npf = OpenGL.ChoosePixelFormat(hDC, ref pfd); OpenGL.SetPixelFormat(hDC, npf, ref pfd); hRC = OpenGL.wglCreateContext(hDC); if (hRC == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } OpenGL.wglMakeCurrent(hDC, hRC); OpenGL.glClearColor(0.5f, 0.5f, 0.75f, 1.0f); //OpenGL.glClearColor( 1f, 1f, 1f, 1.0f ); OpenGL.glPolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_LINE); OpenGL.glEnable(OpenGL.GL_DEPTH_TEST); }
/// <summary> /// Setup OpenGL. /// </summary> public void SetOpenGL() { // If there is no rendering context or device context. if (_deviceContext == IntPtr.Zero || _renderingContext == IntPtr.Zero) { // Make current. OpenGL.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); // Create a buffer descriptor. OpenGL.PIXELFORMATDESCRIPTOR pfd = new OpenGL.PIXELFORMATDESCRIPTOR(); pfd.nSize = (short)Marshal.SizeOf(pfd); pfd.nVersion = 1; pfd.dwFlags = OpenGL.PFD_DRAW_TO_WINDOW | OpenGL.PFD_SUPPORT_OPENGL | OpenGL.PFD_DOUBLEBUFFER; pfd.iPixelType = OpenGL.PFD_TYPE_RGBA; pfd.cColorBits = 24; 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 = 0; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = OpenGL.PFD_MAIN_PLANE; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; // Grab the device context. _deviceContext = OpenGL.GetDC(_control.Handle); if (_deviceContext == IntPtr.Zero) { throw new Exception("An error occured while attempting to allocate device context for OpenGL canvas."); } // Choose our pixel format. int pixelFormat = OpenGL.ChoosePixelFormat(_deviceContext, ref pfd); if (pixelFormat == 0) { throw new Exception("An error occured while attempting to choose pixel format for OpenGL canvas."); } // Set our pixel format. if (OpenGL.SetPixelFormat(_deviceContext, pixelFormat, ref pfd) == 0) { throw new Exception("An error occured while attempting to set pixel format for OpenGL canvas."); } // Grab our rendering context. _renderingContext = OpenGL.wglCreateContext(_deviceContext); if (_renderingContext == IntPtr.Zero) { throw new Exception("An error occured while attempting to allocate rendering context for OpenGL canvas (win32 error code: " + Marshal.GetLastWin32Error() + ")."); } } // If the current context is not the rendering context. if (_currentRenderingContext != _renderingContext) { if (OpenGL.wglMakeCurrent(_deviceContext, _renderingContext) == 0) { throw new Exception("An error occured while attempting to set current OpenGL canvas (" + Marshal.GetLastWin32Error() + ")."); } // Make this context our current one. _currentRenderingContext = _renderingContext; } }
/// <summary> /// Setup OpenGL. /// </summary> public void SetOpenGL() { // If there is no rendering context or device context. if (_deviceContext == IntPtr.Zero || _renderingContext == IntPtr.Zero) { // Make current. OpenGL.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero); // Create a buffer descriptor. OpenGL.PIXELFORMATDESCRIPTOR pfd = new OpenGL.PIXELFORMATDESCRIPTOR(); pfd.nSize = (short)Marshal.SizeOf(pfd); pfd.nVersion = 1; pfd.dwFlags = OpenGL.PFD_DRAW_TO_WINDOW | OpenGL.PFD_SUPPORT_OPENGL | OpenGL.PFD_DOUBLEBUFFER; pfd.iPixelType = OpenGL.PFD_TYPE_RGBA; pfd.cColorBits = 24; 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 = 0; pfd.cStencilBits = 0; pfd.cAuxBuffers = 0; pfd.iLayerType = OpenGL.PFD_MAIN_PLANE; pfd.bReserved = 0; pfd.dwLayerMask = 0; pfd.dwVisibleMask = 0; pfd.dwDamageMask = 0; // Grab the device context. _deviceContext = OpenGL.GetDC(_control.Handle); if (_deviceContext == IntPtr.Zero) throw new Exception("An error occured while attempting to allocate device context for OpenGL canvas."); // Choose our pixel format. int pixelFormat = OpenGL.ChoosePixelFormat(_deviceContext, ref pfd); if (pixelFormat == 0) throw new Exception("An error occured while attempting to choose pixel format for OpenGL canvas."); // Set our pixel format. if (OpenGL.SetPixelFormat(_deviceContext, pixelFormat, ref pfd) == 0) throw new Exception("An error occured while attempting to set pixel format for OpenGL canvas."); // Grab our rendering context. _renderingContext = OpenGL.wglCreateContext(_deviceContext); if (_renderingContext == IntPtr.Zero) throw new Exception("An error occured while attempting to allocate rendering context for OpenGL canvas (win32 error code: " + Marshal.GetLastWin32Error() + ")."); } // If the current context is not the rendering context. if (_currentRenderingContext != _renderingContext) { if (OpenGL.wglMakeCurrent(_deviceContext, _renderingContext) == 0) throw new Exception("An error occured while attempting to set current OpenGL canvas (" + Marshal.GetLastWin32Error() + ")."); // Make this context our current one. _currentRenderingContext = _renderingContext; } }