public static void drawRect(RectangleF rect, Vector4 color, Matrix4 MVPMatrix) { float x = rect.X; float y = rect.Y; float width = rect.Width; float height = rect.Height; float[] Vertices = { x + width, y, x + width, y + height, x, y, x, y + height }; AmbientShader shader = AmbientShader.Singleton; // Use shader shader.Use(); shader.SetColor(color); shader.SetMVPMatrix(MVPMatrix); shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, Vertices); GL.DrawArrays(BeginMode.TriangleStrip, 0, 4); shader.DisableVertices(); GLHelper.GetError(); }
private void ShowZoom(uint texName, IntPtr buffer, Vector4 selectPos, float viewHeight, Matrix4 projMatrix, int offsetY) { // GL has inverted y axis Vector2 invertSelectPos = new Vector2(selectPos.X, viewHeight - selectPos.Y); Rectangle readRect = new Rectangle((int)selectPos.X - zoomSize.Width / 2, (int)invertSelectPos.Y - zoomSize.Height / 2, zoomSize.Width, zoomSize.Height); Rectangle drawRect = new Rectangle((int)selectPos.X - zoomSize.Width / 2, (int)selectPos.Y - zoomSize.Height / 2 + offsetY + zoomSize.Height, zoomSize.Width, -zoomSize.Height); readPixelsToTexture(readRect, texName, buffer); GLHelper.DrawSprite(texName, drawRect, projMatrix, null, Vector4.One); // Draw edge float[] Vertices = { drawRect.X, drawRect.Y, drawRect.X + drawRect.Width, drawRect.Y, drawRect.X + drawRect.Width, drawRect.Y + drawRect.Height, drawRect.X, drawRect.Y + drawRect.Height }; GL.LineWidth(2); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.SrcColor); AmbientShader shader = AmbientShader.Singleton; shader.Use(); shader.SetColor(new Vector4(0.2f, 0.2f, 0.2f, 0.7f)); shader.SetMVPMatrix(projMatrix); shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, Vertices); GL.DrawArrays(BeginMode.LineLoop, 0, 4); shader.DisableVertices(); GL.LineWidth(1); }
public static void DrawROI(RectangleF viewRect, Vector2 vROICenter, float fROIRadius, Rectangle roiRect, uint roiMaskTexName, Vector4 outlineColor, float outlineWidth, Vector4 bgColor, Matrix4 MVMatrix, Matrix4 PMatrix) { Matrix4 MVPMatrix = MVMatrix * PMatrix; GL.Enable(EnableCap.Blend); { GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // GL.BlendFunc(All.Zero, All.OneMinusSrcAlpha); GLHelper.DrawSprite(roiMaskTexName, roiRect, MVPMatrix, null, bgColor); } // Draw 4 areas outside ROI { Vector4 vTopLeft = Vector4.Transform(new Vector4(roiRect.X, roiRect.Y, 0, 1), MVMatrix); Vector4 vButtomRight = Vector4.Transform(new Vector4(roiRect.Right, roiRect.Bottom, 0, 1), MVMatrix); RectangleF boxRect; boxRect = RectangleF.Intersect(viewRect, new RectangleF(0, 0, viewRect.Width, vTopLeft.Y)); if (boxRect != RectangleF.Empty) { GLHelper.drawRect(boxRect, bgColor, PMatrix); } boxRect = RectangleF.Intersect(viewRect, new RectangleF(0, vButtomRight.Y, viewRect.Width, viewRect.Height - vButtomRight.Y)); if (boxRect != RectangleF.Empty) { GLHelper.drawRect(boxRect, bgColor, PMatrix); } boxRect = RectangleF.Intersect(viewRect, new RectangleF(0, vTopLeft.Y, vTopLeft.X, vButtomRight.Y - vTopLeft.Y)); if (boxRect != RectangleF.Empty) { GLHelper.drawRect(boxRect, bgColor, PMatrix); } boxRect = RectangleF.Intersect(viewRect, new RectangleF(vButtomRight.X, vTopLeft.Y, viewRect.Width - vButtomRight.X, vButtomRight.Y - vTopLeft.Y)); if (boxRect != RectangleF.Empty) { GLHelper.drawRect(boxRect, bgColor, PMatrix); } } GL.Disable(EnableCap.Blend); // ROI sphere { float r = fROIRadius; Vector2 pos = vROICenter; float[] vertices = GLHelper.CreateSphereVertices(pos, r, false); float oldLineWidth = 1; GL.GetFloat(GetPName.LineWidth, out oldLineWidth); GL.LineWidth(outlineWidth); AmbientShader shader = AmbientShader.Singleton; shader.Use(); shader.SetColor(outlineColor); shader.SetMVPMatrix(MVPMatrix); shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, vertices); GL.DrawArrays(BeginMode.LineLoop, 0, vertices.Length / 2); shader.DisableVertices(); GL.LineWidth(oldLineWidth); } }
public static void DrawRegions(ICollection <Region> regions, Matrix4 modelViewMatrix, Matrix4 projectionMatrix) { float offsetX = modelViewMatrix.M41; float offsetY = modelViewMatrix.M42; float scale = modelViewMatrix.M11; AmbientShader shader = AmbientShader.Singleton; shader.Use(); bool bDrawRegionData = false; if (bDrawRegionData) { shader.SetColor(new Vector4(1, 0, 0, 1)); shader.SetMVPMatrix(projectionMatrix); foreach (Region region in regions) { int count = region.Points.Count; if (count > 0) { float[] vertices = new float[count * 2]; int iSource = 0; int iTarget = 0; while (iSource < count) { vertices[iTarget++] = region.Points[iSource].X * scale + offsetX; vertices[iTarget++] = region.Points[iSource].Y * scale + offsetY; iSource++; } shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, vertices); GL.DrawArrays(BeginMode.Points, 0, count); shader.DisableVertices(); } } } Vector4 color = ColorHelper.Region; shader.SetColor(color); shader.SetMVPMatrix(modelViewMatrix * projectionMatrix); foreach (Region region in regions) { int x = region.Bounds.X; int y = region.Bounds.Y; int width = region.Bounds.Width; int height = region.Bounds.Height; float[] Vertices = { x + width, y, x + width, y + height, x, y + height, x, y, }; shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, Vertices); GL.DrawArrays(BeginMode.LineLoop, 0, 4); shader.DisableVertices(); } }
public static void updateROIMask(Rectangle roiRect, Size imageSize, uint frameBuffer, uint roiMaskTexName, uint maskTexName) { GLHelper.GetError(); // Setup viewport 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, roiMaskTexName); // Fill with 1.1.1.1 GL.ClearColor(1.0f, 1.0f, 1.0f, 1.0f); GL.Clear(ClearBufferMask.ColorBufferBit); GLHelper.GetError(); // Draw crop if (true) { float[] vertices = GLHelper.CreateSphereVertices(new Vector2((float)roiRect.Size.Width * 0.5f, (float)roiRect.Size.Height * 0.5f), (float)roiRect.Size.Height * 0.5f, true); // Set Model view Matrix4 MVPMatrix = Matrix4.CreateOrthographicOffCenter(0, (float)roiRect.Size.Width, 0, (float)roiRect.Size.Height, -1.0f, 1.0f); AmbientShader shader = AmbientShader.Singleton; shader.Use(); shader.SetColor(new Vector4(0, 0, 0, 0)); shader.SetMVPMatrix(MVPMatrix); shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, vertices); GLHelper.GetError(); GL.DrawArrays(BeginMode.TriangleFan, 0, vertices.Length / 2); shader.DisableVertices(); } // Draw mask if (true) { // Render to source texture float left = (float)roiRect.Location.X / (float)imageSize.Width; float right = (float)(roiRect.Location.X + roiRect.Size.Width) / (float)imageSize.Width; float top = (float)roiRect.Location.Y / (float)imageSize.Height; float button = (float)(roiRect.Location.Y + roiRect.Size.Height) / (float)imageSize.Height; // NB: texture coords are mirrored vertically float[] texCoords = { right, top, right, button, left, top, left, button }; Matrix4 mat = Matrix4.CreateOrthographicOffCenter(0, (float)roiRect.Size.Width, 0, (float)roiRect.Size.Height, -1.0f, 1.0f); Rectangle r = new Rectangle(0, 0, roiRect.Width, roiRect.Height); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GLHelper.DrawSprite(maskTexName, r, mat, texCoords, Vector4.One); GL.Disable(EnableCap.Blend); } // unbind framebuffer GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboOld); // Cleanup viewport GL.Viewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]); GLHelper.GetError(); }
public static void MaskDraw(Vector2 pos, Vector2 prevPos, bool bForeground, uint frameBuffer, uint texName, Size texSize, float[] sphereVertices, int scale) { // FIXME: radius is in image space and needs to be dps adjusted // Setup viewport int[] oldViewPort = new int[4]; GL.GetInteger(GetPName.Viewport, oldViewPort); GL.Viewport(0, 0, texSize.Width, texSize.Height); int fboOld = GLHelper.bindFrameBuffer(frameBuffer, texName); Vector4 foreground = new Vector4(1, 1, 1, 1); Vector4 background = new Vector4(0, 0, 0, 0); Vector4 color = bForeground ? foreground : background; AmbientShader shader = AmbientShader.Singleton; shader.Use(); shader.SetColor(color); Matrix4 ProjMatrix = Matrix4.CreateOrthographicOffCenter(0, (float)texSize.Width, 0, (float)texSize.Height, -1.0f, 1.0f); // Draw sphere at pos { // Set Model view Matrix4 MVPMatrix = Matrix4.Scale(scale) * Matrix4.CreateTranslation(new Vector3(pos)) * ProjMatrix; shader.SetMVPMatrix(MVPMatrix); shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, sphereVertices); GLHelper.GetError(); GL.DrawArrays(BeginMode.TriangleFan, 0, sphereVertices.Length / 2); shader.DisableVertices(); } // Connect prev to current if (!pos.Equals(prevPos)) { shader.SetMVPMatrix(ProjMatrix); // shader.SetMVPMatrix(projMatrix); // Vector2 v0 = CCContext.ViewToModel(prevPos, modelViewMatrix); // Vector2 v1 = CCContext.ViewToModel(pos, modelViewMatrix); Vector2 v0 = prevPos; Vector2 v1 = pos; Vector3 up = new Vector3(0, 0, 1); Vector3 dir = new Vector3(v1) - new Vector3(v0); dir = Vector3.Normalize(dir); Vector3 cross = Vector3.Cross(up, dir) * scale; float[] vertices = new float[4 * 2]; int i = 0; vertices[i++] = v0.X - cross.X; vertices[i++] = v0.Y - cross.Y; vertices[i++] = v0.X + cross.X; vertices[i++] = v0.Y + cross.Y; vertices[i++] = v1.X - cross.X; vertices[i++] = v1.Y - cross.Y; vertices[i++] = v1.X + cross.X; vertices[i++] = v1.Y + cross.Y; shader.EnableVertices(2, VertexAttribPointerType.Float, false, 0, vertices); GL.DrawArrays(BeginMode.TriangleStrip, 0, 4); // GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, 4); shader.DisableVertices(); } // unbind framebuffer GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboOld); GL.Viewport(oldViewPort[0], oldViewPort[1], oldViewPort[2], oldViewPort[3]); }