protected override void Dispose(bool disposing) { if (disposed) { return; } if (disposing) { CleanUpTextures(); if (videoTextureCache != null) { videoTextureCache.Flush(CVOptionFlags.None); videoTextureCache.Dispose(); } if (context != null) { context.Dispose(); } } GL.DeleteFramebuffers(1, ref frameBufferHandle); GL.DeleteRenderbuffers(1, ref colorBufferHandle); disposed = true; base.Dispose(disposing); }
public virtual CVOpenGLESTexture LumaTextureForPixelBuffer(CVPixelBuffer pixelBuffer) { CVOpenGLESTexture lumaTexture = null; CVReturn err; if (VideoTextureCache == null) { Console.Error.WriteLine("No video texture cache"); return(lumaTexture); } // Periodic texture cache flush every frame VideoTextureCache.Flush(0); // CVOpenGLTextureCacheCreateTextureFromImage will create GL texture optimally from CVPixelBufferRef. // UV lumaTexture = VideoTextureCache.TextureFromImage(pixelBuffer, true, All.RedExt, pixelBuffer.Width, pixelBuffer.Height, All.RedExt, DataType.UnsignedByte, 0, out err); if (lumaTexture == null || err != CVReturn.Success) { Console.Error.WriteLine("Error at creating luma texture using CVOpenGLESTextureCacheCreateTextureFromImage: " + err.ToString()); } return(lumaTexture); }
public void DisplayPixelBuffer(CVImageBuffer imageBuffer) { // First check to make sure we have a FrameBuffer to write to. if (frameBuffer == 0) { var success = CreateFrameBuffer(); if (!success) { Console.WriteLine("Problem initializing OpenGL buffers."); return; } } if (videoTextureCache == null) { Console.WriteLine("Video Texture Cache not initialized"); return; } if (!(imageBuffer is CVPixelBuffer pixelBuffer)) { Console.WriteLine("Could not get Pixel Buffer from Image Buffer"); return; } // Create a CVOpenGLESTexture from the CVImageBuffer var frameWidth = (int)pixelBuffer.Width; var frameHeight = (int)pixelBuffer.Height; using (var texture = videoTextureCache.TextureFromImage(imageBuffer, true, All.Rgba, frameWidth, frameHeight, All.Bgra, DataType.UnsignedByte, 0, out CVReturn ret)) { if (texture == null || ret != CVReturn.Success) { Console.WriteLine("Could not create Texture from Texture Cache"); return; } GL.BindTexture(texture.Target, texture.Name); // Set texture parameters GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge); GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer); // Set the view port to the entire view GL.Viewport(0, 0, renderBufferWidth, renderBufferHeight); var squareVerticies = new float[, ] { { -1.0F, -1.0F }, { 1.0F, -1.0F }, { -1.0F, 1.0F }, { 1.0F, 1.0F } }; // The texture verticies are setup such that we flip the texture vertically. // This is so that our top left origin buffers match OpenGL's bottom left texture coordinate system. var textureSamplingRect = TextureSamplingRectForCroppingTextureWithAspectRatio(new CGSize(frameWidth, frameHeight), Bounds.Size); var textureVertices = new float[, ] { { (float)textureSamplingRect.Left, (float)textureSamplingRect.Bottom }, { (float)textureSamplingRect.Right, (float)textureSamplingRect.Bottom }, { (float)textureSamplingRect.Left, (float)textureSamplingRect.Top }, { (float)textureSamplingRect.Right, (float)textureSamplingRect.Top } }; // Draw the texture on the screen with OpenGL ES 2 RenderWithSquareVerticies(squareVerticies, textureVertices); GL.BindTexture(texture.Target, texture.Name); // Flush the CVOpenGLESTexture cache and release the texture videoTextureCache.Flush(CVOptionFlags.None); } }