Exemplo n.º 1
0
		public void DisplayPixelBuffer (CVPixelBuffer pixelBuffer)
		{
			DrawTextInCorner (pixelBuffer);
			CVReturn error;

			if (pixelBuffer != null) {
				int frameWidth = (int)pixelBuffer.Width;
				int frameHeight = (int)pixelBuffer.Height;

				if (videoTextureCache == null) {
					Console.WriteLine ("No video texture cache");
					return;
				}

				CleanUpTextures ();
				CVAttachmentMode attachmentMode;
				var colorAttachments = pixelBuffer.GetAttachment <NSString> (CVImageBuffer.YCbCrMatrixKey, out attachmentMode);

				if (colorAttachments == CVImageBuffer.YCbCrMatrix_ITU_R_601_4)
					preferredConversion = colorConversion601;
				else
					preferredConversion = colorConversion709;

				GL.ActiveTexture (TextureUnit.Texture0);
				lumaTexture = videoTextureCache.TextureFromImage (pixelBuffer, true, All.RedExt, frameWidth, frameHeight,
					All.RedExt, DataType.UnsignedByte, 0, out error);

				if (lumaTexture == null)
					Console.WriteLine ("Error at CVOpenGLESTextureCach.TextureFromImage");

				GL.BindTexture (lumaTexture.Target, lumaTexture.Name);
				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.ActiveTexture (TextureUnit.Texture1);
				chromaTexture = videoTextureCache.TextureFromImage (pixelBuffer, true, All.RgExt, frameWidth / 2, frameHeight / 2,
					All.RgExt, DataType.UnsignedByte, 1, out error);

				if (chromaTexture == null)
					Console.WriteLine ("Error at CVOpenGLESTextureCach.TextureFromImage");

				GL.BindTexture (chromaTexture.Target, chromaTexture.Name);
				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, frameBufferHandle);
				GL.Viewport (0, 0, backingWidth, backingHeight);
			}

			GL.ClearColor (0f, 0f, 0f, 1f);
			GL.Clear (ClearBufferMask.ColorBufferBit);

			GL.UseProgram (Program);
			GL.Uniform1 (uniforms [(int)UniformIndex.RotationAngle], 0f);
			GL.UniformMatrix3 (uniforms [(int)UniformIndex.ColorConversionMatrix], 1, false, preferredConversion);

			CGRect vertexSamplingRect = AVUtilities.WithAspectRatio (Bounds, PresentationRect);

			var normalizedSamplingSize = new CGSize (0f, 0f);
			var cropScaleAmount = new CGSize (vertexSamplingRect.Width / Bounds.Width, vertexSamplingRect.Height / Bounds.Height);

			if (cropScaleAmount.Width > cropScaleAmount.Height) {
				normalizedSamplingSize.Width = 1f;
				normalizedSamplingSize.Height = cropScaleAmount.Height / cropScaleAmount.Width;
			} else {
				normalizedSamplingSize.Width = 1f;
				normalizedSamplingSize.Height = cropScaleAmount.Width / cropScaleAmount.Height;
			}

			float[] quadVertexData = {
				-1f * (float)normalizedSamplingSize.Width, -1f * (float)normalizedSamplingSize.Height,
				(float)normalizedSamplingSize.Width, -1f * (float)normalizedSamplingSize.Height,
				-1f * (float)normalizedSamplingSize.Width, (float)normalizedSamplingSize.Height,
				(float)normalizedSamplingSize.Width, (float)normalizedSamplingSize.Height,
			};

			GL.VertexAttribPointer ((int)AttributeIndex.Vertex, 2, VertexAttribPointerType.Float, false, 0, quadVertexData);
			GL.EnableVertexAttribArray ((int)AttributeIndex.Vertex);

			var textureSamplingRect = new CGRect (0, 0, 1, 1);
			float[] quadTextureData = {
				(float)textureSamplingRect.GetMinX (), (float)textureSamplingRect.GetMaxY (),
				(float)textureSamplingRect.GetMaxX (), (float)textureSamplingRect.GetMaxY (),
				(float)textureSamplingRect.GetMinX (), (float)textureSamplingRect.GetMinY (),
				(float)textureSamplingRect.GetMaxX (), (float)textureSamplingRect.GetMinY ()
			};

			GL.VertexAttribPointer ((int)AttributeIndex.TextureCoordinates, 2, VertexAttribPointerType.Float, false, 0, quadTextureData);
			GL.EnableVertexAttribArray ((int)AttributeIndex.TextureCoordinates);

			GL.DrawArrays (BeginMode.TriangleStrip, 0, 4);
			GL.BindRenderbuffer (RenderbufferTarget.Renderbuffer, colorBufferHandle);
			context.PresentRenderBuffer ((int)RenderbufferTarget.Renderbuffer);
		}
Exemplo n.º 2
0
        public void DisplayPixelBuffer(CVPixelBuffer pixelBuffer)
        {
            DrawTextInCorner(pixelBuffer);
            CVReturn error;

            if (pixelBuffer != null)
            {
                int frameWidth  = (int)pixelBuffer.Width;
                int frameHeight = (int)pixelBuffer.Height;

                if (videoTextureCache == null)
                {
                    Console.WriteLine("No video texture cache");
                    return;
                }

                CleanUpTextures();
                CVAttachmentMode attachmentMode;
                var colorAttachments = pixelBuffer.GetAttachment <NSString> (CVImageBuffer.YCbCrMatrixKey, out attachmentMode);

                if (colorAttachments == CVImageBuffer.YCbCrMatrix_ITU_R_601_4)
                {
                    preferredConversion = colorConversion601;
                }
                else
                {
                    preferredConversion = colorConversion709;
                }

                GL.ActiveTexture(TextureUnit.Texture0);
                lumaTexture = videoTextureCache.TextureFromImage(pixelBuffer, true, All.RedExt, frameWidth, frameHeight,
                                                                 All.RedExt, DataType.UnsignedByte, 0, out error);

                if (lumaTexture == null)
                {
                    Console.WriteLine("Error at CVOpenGLESTextureCach.TextureFromImage");
                }

                GL.BindTexture(lumaTexture.Target, lumaTexture.Name);
                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.ActiveTexture(TextureUnit.Texture1);
                chromaTexture = videoTextureCache.TextureFromImage(pixelBuffer, true, All.RgExt, frameWidth / 2, frameHeight / 2,
                                                                   All.RgExt, DataType.UnsignedByte, 1, out error);

                if (chromaTexture == null)
                {
                    Console.WriteLine("Error at CVOpenGLESTextureCach.TextureFromImage");
                }

                GL.BindTexture(chromaTexture.Target, chromaTexture.Name);
                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, frameBufferHandle);
                GL.Viewport(0, 0, backingWidth, backingHeight);
            }

            GL.ClearColor(0f, 0f, 0f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.UseProgram(Program);
            GL.Uniform1(uniforms [(int)UniformIndex.RotationAngle], 0f);
            GL.UniformMatrix3(uniforms [(int)UniformIndex.ColorConversionMatrix], 1, false, preferredConversion);

            CGRect vertexSamplingRect = AVUtilities.WithAspectRatio(Bounds, PresentationRect);

            var normalizedSamplingSize = new CGSize(0f, 0f);
            var cropScaleAmount        = new CGSize(vertexSamplingRect.Width / Bounds.Width, vertexSamplingRect.Height / Bounds.Height);

            if (cropScaleAmount.Width > cropScaleAmount.Height)
            {
                normalizedSamplingSize.Width  = 1f;
                normalizedSamplingSize.Height = cropScaleAmount.Height / cropScaleAmount.Width;
            }
            else
            {
                normalizedSamplingSize.Width  = 1f;
                normalizedSamplingSize.Height = cropScaleAmount.Width / cropScaleAmount.Height;
            }

            float[] quadVertexData =
            {
                -1f * (float)normalizedSamplingSize.Width, -1f * (float)normalizedSamplingSize.Height,
                (float)normalizedSamplingSize.Width,       -1f * (float)normalizedSamplingSize.Height,
                -1f * (float)normalizedSamplingSize.Width, (float)normalizedSamplingSize.Height,
                (float)normalizedSamplingSize.Width,       (float)normalizedSamplingSize.Height,
            };

            GL.VertexAttribPointer((int)AttributeIndex.Vertex, 2, VertexAttribPointerType.Float, false, 0, quadVertexData);
            GL.EnableVertexAttribArray((int)AttributeIndex.Vertex);

            var textureSamplingRect = new CGRect(0, 0, 1, 1);

            float[] quadTextureData =
            {
                (float)textureSamplingRect.GetMinX(), (float)textureSamplingRect.GetMaxY(),
                (float)textureSamplingRect.GetMaxX(), (float)textureSamplingRect.GetMaxY(),
                (float)textureSamplingRect.GetMinX(), (float)textureSamplingRect.GetMinY(),
                (float)textureSamplingRect.GetMaxX(), (float)textureSamplingRect.GetMinY()
            };

            GL.VertexAttribPointer((int)AttributeIndex.TextureCoordinates, 2, VertexAttribPointerType.Float, false, 0, quadTextureData);
            GL.EnableVertexAttribArray((int)AttributeIndex.TextureCoordinates);

            GL.DrawArrays(BeginMode.TriangleStrip, 0, 4);
            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, colorBufferHandle);
            context.PresentRenderBuffer((int)RenderbufferTarget.Renderbuffer);
        }