/// <summary> /// Handles the Tick event of the timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void timer_Tick(object sender, EventArgs e) { // If we don't have a scene, we're done. if (Scene == null) { return; } // Lock on the Scene. lock (Scene) { // Start the stopwatch so that we can time the rendering. stopwatch.Restart(); // Draw the scene. Scene.Draw(Camera); // Draw the FPS. if (DrawFPS) { Scene.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", frameTime, 1000.0 / frameTime)); Scene.OpenGL.Flush(); } if (Scene.OpenGL.RenderContextProvider is RenderContextProviders.DIBSectionRenderContextProvider) { RenderContextProviders.DIBSectionRenderContextProvider provider = Scene.OpenGL.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. image.Source = newFormatedBitmapSource; } else if (Scene.OpenGL.RenderContextProvider is RenderContextProviders.FBORenderContextProvider) { RenderContextProviders.FBORenderContextProvider provider = Scene.OpenGL.RenderContextProvider as RenderContextProviders.FBORenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. image.Source = newFormatedBitmapSource; } else if (Scene.OpenGL.RenderContextProvider is RenderContextProviders.PBORenderContextProvider) { PBORenderContextProvider provider = Scene.OpenGL.RenderContextProvider as PBORenderContextProvider; var width = provider.Width; var height = provider.Height; var pixelsPtr = provider.PixelPtr; var size = provider.Size; var stride = provider.Stride; writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelsPtr, size, stride, 0, 0); image.Source = writeableBitmap; } // Stop the stopwatch. stopwatch.Stop(); // Store the frame time. frameTime = stopwatch.Elapsed.TotalMilliseconds; } }
/// <summary> /// Handles the Tick event of the timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> void timer_Tick(object sender, EventArgs e) { // Lock on OpenGL. lock (gl) { // Start the stopwatch so that we can time the rendering. stopwatch.Restart(); // Make GL current. gl.MakeCurrent(); // If there is a draw handler, then call it. var handler = OpenGLDraw; if (handler != null) { handler(this, eventArgsFast); } else { gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT); } // Draw the FPS. if (DrawFPS) { gl.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", frameTime, 1000.0 / frameTime)); gl.Flush(); } // Render. gl.Blit(IntPtr.Zero); switch (RenderContextType) { case RenderContextType.DIBSection: { RenderContextProviders.DIBSectionRenderContextProvider provider = gl.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. image.Source = newFormatedBitmapSource; } break; case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { RenderContextProviders.FBORenderContextProvider provider = gl.RenderContextProvider as RenderContextProviders.FBORenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. image.Source = newFormatedBitmapSource; } break; case RenderContextType.PBO: { PBORenderContextProvider provider = gl.RenderContextProvider as PBORenderContextProvider; var width = provider.Width; var height = provider.Height; var pixelsPtr = provider.PixelPtr; var size = provider.Size; var stride = provider.Stride; writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelsPtr, size, stride, 0, 0); image.Source = writeableBitmap; } break; default: break; } // Stop the stopwatch. stopwatch.Stop(); // Store the frame time. frameTime = stopwatch.Elapsed.TotalMilliseconds; } }
public ImageSource GetFrame() { if (!BlitImage) { return(ImgSource); } lock (Gl) { Gl.Blit(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; switch (RenderContextType) { case RenderContextType.DIBSection: { DIBSectionRenderContextProvider provider = Gl.RenderContextProvider as DIBSectionRenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. return(newFormatedBitmapSource); } case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = Gl.RenderContextProvider as FBORenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. return(newFormatedBitmapSource); } case RenderContextType.PBO: { PBORenderContextProvider provider = Gl.RenderContextProvider as PBORenderContextProvider; var width = provider.Width; var height = provider.Height; var pixelsPtr = provider.PixelPtr; if (pixelsPtr == IntPtr.Zero) { return(null); } var size = provider.Size; var stride = provider.Stride; var rect = new Int32Rect(0, 0, width, height); WriteableBitmap.WritePixels(rect, pixelsPtr, size, stride, 0, 0); return(WriteableBitmap); } default: break; } } return(null); }