static public void UpdateSegmentation(ICollection <Tag> tags, Rectangle roiRect, uint frameBuffer, uint thresholdTexName, uint roiTexName, uint[] segmentationPixelData, int[] roiThresholdMin, int[] roiThresholdMax) { float[] thresholdMin = { 1, 1, 1 }; float[] thresholdMax = { 0, 0, 0 }; if (tags.Count == 0) { thresholdMin = new float[] { (float)roiThresholdMin[0] / 255.0f, (float)roiThresholdMin[1] / 255.0f, (float)roiThresholdMin[2] / 255.0f }; thresholdMax = new float[] { (float)roiThresholdMax[0] / 255.0f, (float)roiThresholdMax[1] / 255.0f, (float)roiThresholdMax[2] / 255.0f }; } else { // TODO: use ALL thresholds foreach (Tag tag in tags) { float[] min = new float[3]; float[] max = new float[3]; tag.GetNormalizedMaxMin(ref min, ref max); thresholdMin[0] = Math.Min(thresholdMin[0], min[0]); thresholdMin[1] = Math.Min(thresholdMin[1], min[1]); thresholdMin[2] = Math.Min(thresholdMin[2], min[2]); thresholdMax[0] = Math.Max(thresholdMax[0], max[0]); thresholdMax[1] = Math.Max(thresholdMax[1], max[1]); thresholdMax[2] = Math.Max(thresholdMax[2], max[2]); // thresholdMin = new float[3] { min[0], min[1], min[2]}; // thresholdMax = new float[3] { max[0], max[1], max[2]}; Console.WriteLine("color:" + tag.Color[0] + "," + tag.Color[1] + "," + tag.Color[2]); } } // Setup thresholdTexName as framebuffer int[] oldViewPort = new int[4]; GL.GetInteger(GetPName.Viewport, oldViewPort); GL.Viewport(0, 0, roiRect.Size.Width, roiRect.Size.Height); int fboOld = GLHelper.bindFrameBuffer(frameBuffer, thresholdTexName); // Clear GL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f); GL.Clear(ClearBufferMask.ColorBufferBit); float x = 0; float y = 0; float width = roiRect.Size.Width; float height = roiRect.Size.Height; float[] Vertices = { x + width, y, x + width, y + height, x, y, x, y + height }; float[] Texture = { 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; Vector4 color = new Vector4(1, 1, 1, 1); Matrix4 mat = Matrix4.CreateOrthographicOffCenter(0, (float)roiRect.Size.Width, 0, (float)roiRect.Size.Height, -1.0f, 1.0f); ThresholdShader shader = ThresholdShader.Singleton; // Use shader shader.Use(); // Set texture shader.SetTexture(roiTexName); // Set threshold Vector4 vMin = new Vector4(thresholdMin[0], thresholdMin[2], thresholdMin[2], 1); Vector4 vMax = new Vector4(thresholdMax[0], thresholdMax[2], thresholdMax[2], 1); shader.SetThreshold(vMin, vMax); shader.setColor(color); // Set Model view GL.UniformMatrix4(shader.muMVPMatrixHandle, 1, false, ref mat.Row0.X); // Set vertex and texture coords GL.EnableVertexAttribArray(shader.maPositionHandle); GL.EnableVertexAttribArray(shader.maTexCoordHandle); GL.VertexAttribPointer(shader.maPositionHandle, 2, VertexAttribPointerType.Float, false, 0, Vertices); GL.VertexAttribPointer(shader.maTexCoordHandle, 2, VertexAttribPointerType.Float, false, 0, Texture); GL.DrawArrays(BeginMode.TriangleStrip, 0, 4); GL.DisableVertexAttribArray(shader.maPositionHandle); GL.DisableVertexAttribArray(shader.maTexCoordHandle); // Read pixel data { GL.ReadPixels(0, 0, roiRect.Size.Width, roiRect.Size.Height, PixelFormat.Rgba, PixelType.UnsignedByte, segmentationPixelData); // GL.ReadPixels<uint>(0, 0, roiRect.Size.Width, roiRect.Size.Height, All.Rgba, All.UnsignedByte, segmentationPixelData); } // unbind framebuffer and cleanup viewport GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboOld); GL.Viewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]); }
// TODO: find better way to access shaders public static void Reset() { singleton = null; }