private Image Output() { gl.Flush(); gl.Blit(IntPtr.Zero); var 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. var newFormatedBitmapSource = new FormatConvertedBitmap(); if (provider == null) { return(null); } newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); var outStream = new MemoryStream(); BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource)); enc.Save(outStream); //Console.ReadLine(); //gl.Flush(); //Console.ReadLine(); return(new Bitmap(outStream)); }
public void Render() { _gl.MakeCurrent(); _gl.ClearColor(0.2f, 0.2f, 0.2f, 0); _gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); _gl.LoadIdentity(); if (RenderScene != null) { RenderScene(this, new SimpleOpenGlEventArgs { OpenGl = _gl }); } _gl.Blit(IntPtr.Zero); var fboRenderContextProvider = (FBORenderContextProvider)_gl.RenderContextProvider; var formatConvertedBitmap = new FormatConvertedBitmap(); formatConvertedBitmap.BeginInit(); formatConvertedBitmap.Source = BitmapConversion.HBitmapToBitmapSource(fboRenderContextProvider.InternalDIBSection.HBitmap); formatConvertedBitmap.DestinationFormat = PixelFormats.Rgb24; formatConvertedBitmap.EndInit(); OpenGlImage.Source = formatConvertedBitmap; }
public override ImageSource Render() { GL.MakeCurrent(); GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); GL.ClearColor(1f, 0f, 0f, 0f); _postProcesser.Capture(() => { GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); _backgroundRenderer.Render(); _octreeRenderer.Render(); }); _postProcesser.Render(); GL.Blit(IntPtr.Zero); var provider = GL.RenderContextProvider as FBORenderContextProvider; if (provider == null) { return(null); } var newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); return(newFormatedBitmapSource); }
public FormatConvertedBitmap GetFrame() { lock (Gl) { // Render. Gl.Blit(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; FormatConvertedBitmap newFormatedBitmapSource = null; switch (RenderContextType) { case RenderContextType.DIBSection: { var provider = Gl.RenderContextProvider as DIBSectionRenderContextProvider; //hBitmap = provider.DIBSection.HBitmap; //break; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); break; } case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = Gl.RenderContextProvider as FBORenderContextProvider; //hBitmap = provider.InternalDIBSection.HBitmap; //break; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); break; } } //Bitmap res = hBitmap != null ? Bitmap.FromHbitmap(hBitmap) : null; //return res; return(newFormatedBitmapSource); } }
/// <summary> /// This method converts the output from the OpenGL render context provider to a /// FormatConvertedBitmap in order to show it in the image. /// </summary> /// <param name="handle">The handle of the bitmap from the OpenGL render context.</param> /// <returns>Returns the new format converted bitmap.</returns> private static FormatConvertedBitmap GetFormatedBitmapSource(IntPtr handle) { // 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(handle); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); return(newFormatedBitmapSource); }
private void UpdateImageSource(IntPtr hBitmap) { // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. var newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(hBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. OnImageSourceUpdated(new ImageSourceEventArg(newFormatedBitmapSource)); }
/// <summary> /// Fill the ImageSource from the provided bits IntPtr, using the provided hBitMap IntPtr /// if needed to determine key data from the bitmap source. /// </summary> /// <param name="bits">An IntPtr to the bits for the bitmap image. Generally provided from /// DIBSectionRenderContextProvider.DIBSection.Bits or from /// FBORenderContextProvider.InternalDIBSection.Bits.</param> /// <param name="hBitmap">An IntPtr to the HBitmap for the image. Generally provided from /// DIBSectionRenderContextProvider.DIBSection.HBitmap or from /// FBORenderContextProvider.InternalDIBSection.HBitmap.</param> public void FillImageSource(IntPtr bits, IntPtr hBitmap) { // If DPI hasn't been set, use a call to HBitmapToBitmapSource to fill the info // This should happen only ONCE (near the start of the application) if (_dpiX == 0) { var bitmapSource = BitmapConversion.HBitmapToBitmapSource(hBitmap); _dpiX = bitmapSource.DpiX; _dpiY = bitmapSource.DpiY; _format = bitmapSource.Format; _bytesPerPixel = gl.RenderContextProvider.BitDepth >> 3; // FBO render context flips the image vertically, so transform to compensate if (RenderContextType == RenderContextType.FBO) { image.RenderTransform = new ScaleTransform(1.0, -1.0); image.RenderTransformOrigin = new Point(0.0, 0.5); } else { image.RenderTransform = Transform.Identity; image.RenderTransformOrigin = new Point(0.0, 0.0); } } // If the image buffer is null, create it // This should happen when the size of the image changes if (_imageBuffer == null) { int width = gl.RenderContextProvider.Width; int height = gl.RenderContextProvider.Height; int imageBufferSize = width * height * _bytesPerPixel; _imageBuffer = new byte[imageBufferSize]; _writeableBitmap = new WriteableBitmap(width, height, _dpiX, _dpiY, _format, null); _imageRect = new Int32Rect(0, 0, width, height); _imageStride = width * _bytesPerPixel; } // Fill the image buffer from the bits and update the writeable bitmap System.Runtime.InteropServices.Marshal.Copy(bits, _imageBuffer, 0, _imageBuffer.Length); // FIXME Remove transparency // for (int i = 3; i < _imageBuffer.Length; i+=4) _imageBuffer[i] = 255; _writeableBitmap.WritePixels(_imageRect, _imageBuffer, _imageStride, 0); image.Source = _writeableBitmap; }