/// <summary> /// This method is used to set the dimensions and the viewport of the OpenGL control. /// </summary> /// <param name="width">The width of the OpenGL drawing area.</param> /// <param name="height">The height of the OpenGL drawing area.</param> private void UpdateOpenGLControl(int width, int height) { // Lock on OpenGL lock (this.OpenGL) { this.OpenGL.SetDimensions(width, height); // Set the viewport this.OpenGL.Viewport(0, 0, width, height); // If we have a project handler, call it if (width != -1 && height != -1) { OpenGLEventHandler handler = this.Resized; if (handler != null) { handler(this, this.eventArgsFast); } else { // Otherwise we do our own projection this.OpenGL.MatrixMode(OpenGL.GL_PROJECTION); this.OpenGL.LoadIdentity(); // Calculate The Aspect Ratio Of The Window this.OpenGL.Perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f); this.OpenGL.MatrixMode(OpenGL.GL_MODELVIEW); this.OpenGL.LoadIdentity(); } } } }
/// <summary> /// Handles the Tick event of the timer control. /// </summary> private void TimerTick() { try { if (this.Dispatcher.HasShutdownStarted || this.Dispatcher.HasShutdownFinished) { this.timer.Abort(); return; } this.Dispatcher.Invoke( () => { // Lock on OpenGL lock (this.OpenGL) { // Start the stopwatch so that we can time the rendering this.stopwatch.Restart(); // Make GL current this.OpenGL.MakeCurrent(); // If there is a draw handler, then call it OpenGLEventHandler handler = this.OpenGLDraw; if (handler != null) { handler(this, this.eventArgsFast); } else { this.OpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT); } // Draw the FPS if (this.DrawFPS) { this.OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f, string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", this.frameTime, this.currentFrameRate)); this.OpenGL.Flush(); } // Render this.OpenGL.Blit(IntPtr.Zero); switch (this.RenderContextType) { case RenderContextType.DIBSection: { DIBSectionRenderContextProvider provider = this.OpenGL.RenderContextProvider as DIBSectionRenderContextProvider; IntPtr hBitmap = provider.DIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { FormatConvertedBitmap newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap); // Copy the pixels over this.image.Source = newFormatedBitmapSource; } } break; case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = this.OpenGL.RenderContextProvider as FBORenderContextProvider; IntPtr hBitmap = provider.InternalDIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { FormatConvertedBitmap newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap); // Copy the pixels over this.image.Source = newFormatedBitmapSource; } } break; default: break; } // Stop the stopwatch this.stopwatch.Stop(); // Store the frame time this.frameTime = this.stopwatch.Elapsed.TotalMilliseconds; } }, DispatcherPriority.Send); if (this.frameRate > 0.0) { Thread.Sleep(new TimeSpan(0, 0, 0, 0, (int)(Math.Max(1000.0 - this.frameTime, this.frameRate) / this.frameRate))); } } catch (Exception ex) { ex.ToString(); } }