public override void Load() { //draw 1 FormTestWinGLControl form = new FormTestWinGLControl(); var canvas = PixelFarm.Drawing.DrawingGL.CanvasGLPortal.P.CreateCanvas(0, 0, 800, 600); PixelFarm.Drawing.Bitmap bmp = null; PixelFarm.Drawing.TextureBrush tbrush = null; form.SetGLPaintHandler((o, s) => { canvas.ClearSurface(PixelFarm.Drawing.Color.White); if (tbrush == null) { var bitmap = new System.Drawing.Bitmap("../../../SampleImages/plain01.png"); bmp = new PixelFarm.Drawing.Bitmap(bitmap.Width, bitmap.Height, bitmap); tbrush = new PixelFarm.Drawing.TextureBrush(bmp); } //2. fill polygon with gradient brush canvas.FillPolygon( tbrush, new PixelFarm.Drawing.PointF[] { new PixelFarm.Drawing.PointF(60, 50), new PixelFarm.Drawing.PointF(100, 50), new PixelFarm.Drawing.PointF(70, 100) }); canvas.Note1 = 1; //temp canvas.FillPolygon( tbrush, new PixelFarm.Drawing.PointF[] { new PixelFarm.Drawing.PointF(0, 50), new PixelFarm.Drawing.PointF(50, 50), new PixelFarm.Drawing.PointF(10, 100) }); canvas.Note1 = 0; //------------------------------------------------------------------------- }); form.Show(); }
public void FillPolygon(PixelFarm.Drawing.Brush brush, float[] vertex2dCoords, int npoints) { //------------- //Tesselate //2d coods lis //n point switch (this.SmoothMode) { case CanvasSmoothMode.AggSmooth: { //closed polygon //closed polygon int j = npoints / 2; //first point if (j < 2) { return; } ps.MoveTo(vertex2dCoords[0], vertex2dCoords[1]); int nn = 2; for (int i = 1; i < j; ++i) { ps.LineTo(vertex2dCoords[nn++], vertex2dCoords[nn++]); } //close ps.CloseFigure(); VertexStore vxs = ps.Vxs; sclineRas.Reset(); sclineRas.AddPath(vxs); switch (brush.BrushKind) { case Drawing.BrushKind.Solid: { var color = ((PixelFarm.Drawing.SolidBrush)brush).Color; sclineRasToGL.FillWithColor(sclineRas, sclinePack8, color); } break; default: { } break; } } break; default: { var vertextList = TessPolygon(vertex2dCoords); //----------------------------- //switch how to fill polygon switch (brush.BrushKind) { case Drawing.BrushKind.LinearGradient: case Drawing.BrushKind.Texture: { var linearGradientBrush = brush as PixelFarm.Drawing.LinearGradientBrush; GL.ClearStencil(0); //set value for clearing stencil buffer //actual clear here GL.Clear(ClearBufferMask.StencilBufferBit); //------------------- //disable rendering to color buffer GL.ColorMask(false, false, false, false); //start using stencil GL.Enable(EnableCap.StencilTest); //place a 1 where rendered GL.StencilFunc(StencilFunction.Always, 1, 1); //replace where rendered GL.StencilOp(StencilOp.Replace, StencilOp.Replace, StencilOp.Replace); //render to stencill buffer //----------------- if (this.Note1 == 1) { ////create stencil with Agg shape int j = npoints / 2; //first point if (j < 2) { return; } ps.Clear(); ps.MoveTo(vertex2dCoords[0], vertex2dCoords[1]); int nn = 2; for (int i = 1; i < j; ++i) { ps.LineTo( vertex2dCoords[nn++], vertex2dCoords[nn++]); } //close ps.CloseFigure(); VertexStore vxs = ps.Vxs; sclineRas.Reset(); sclineRas.AddPath(vxs); sclineRasToGL.FillWithColor(sclineRas, sclinePack8, PixelFarm.Drawing.Color.White); //create stencil with normal OpenGL } else { //create stencil with normal OpenGL int j = vertextList.Count; int j2 = j * 2; //VboC4V3f vbo = GenerateVboC4V3f(); ArrayList <VertexC4V2f> vrx = new ArrayList <VertexC4V2f>(); uint color_uint = PixelFarm.Drawing.Color.Black.ToABGR(); //color.ToABGR(); for (int i = 0; i < j; ++i) { var v = vertextList[i]; vrx.AddVertex(new VertexC4V2f(color_uint, (float)v.m_X, (float)v.m_Y)); } DrawVertexList(DrawMode.Triangles, vrx, vrx.Count); } //-------------------------------------- //render color //-------------------------------------- //reenable color buffer GL.ColorMask(true, true, true, true); //where a 1 was not rendered GL.StencilFunc(StencilFunction.Equal, 1, 1); //freeze stencill buffer GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep); if (this.Note1 == 1) { //------------------------------------------ //we already have valid ps from stencil step //------------------------------------------ VertexStore vxs = ps.Vxs; sclineRas.Reset(); sclineRas.AddPath(vxs); //------------------------------------------------------------------------------------- //1. we draw only alpha channel of this black color to destination color //so we use BlendFuncSeparate as follow ... GL.ColorMask(false, false, false, true); //GL.BlendFuncSeparate( // BlendingFactorSrc.DstColor, BlendingFactorDest.DstColor, //the same // BlendingFactorSrc.One, BlendingFactorDest.Zero); //use alpha chanel from source*** GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.Zero); sclineRasToGL.FillWithColor(sclineRas, sclinePack8, PixelFarm.Drawing.Color.Black); //at this point alpha component is fill in to destination //------------------------------------------------------------------------------------- //2. then fill again!, //we use alpha information from dest, //so we set blend func to ... GL.BlendFunc(BlendingFactorSrc.DstAlpha, BlendingFactorDest.OneMinusDstAlpha) GL.ColorMask(true, true, true, true); GL.BlendFunc(BlendingFactorSrc.DstAlpha, BlendingFactorDest.OneMinusDstAlpha); { //draw box of gradient color if (brush.BrushKind == Drawing.BrushKind.LinearGradient) { var colors = linearGradientBrush.GetColors(); var points = linearGradientBrush.GetStopPoints(); uint c1 = colors[0].ToABGR(); uint c2 = colors[1].ToABGR(); //create polygon for graident bg ArrayList <VertexC4V2f> vrx = GLGradientColorProvider.CalculateLinearGradientVxs( points[0].X, points[0].Y, points[1].X, points[1].Y, colors[0], colors[1]); DrawVertexList(DrawMode.Triangles, vrx, vrx.Count); } else if (brush.BrushKind == Drawing.BrushKind.Texture) { //draw texture image PixelFarm.Drawing.TextureBrush tbrush = (PixelFarm.Drawing.TextureBrush)brush; PixelFarm.Drawing.Image img = tbrush.TextureImage; GLBitmap bmpTexture = (GLBitmap)tbrush.InnerImage2; this.DrawImage(bmpTexture, 0, 0); //GLBitmapTexture bmp = GLBitmapTexture.CreateBitmapTexture(fontGlyph.glyphImage32); //this.DrawImage(bmp, 0, 0); //bmp.Dispose(); } } //restore back //3. switch to normal blending mode GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); } else { //draw box of gradient color var colors = linearGradientBrush.GetColors(); var points = linearGradientBrush.GetStopPoints(); uint c1 = colors[0].ToABGR(); uint c2 = colors[1].ToABGR(); //create polygon for graident bg var vrx = GLGradientColorProvider.CalculateLinearGradientVxs( points[0].X, points[0].Y, points[1].X, points[1].Y, colors[0], colors[1]); DrawVertexList(DrawMode.Triangles, vrx, vrx.Count); } GL.Disable(EnableCap.StencilTest); } break; default: { //unknown brush //int j = vertextList.Count; //int j2 = j * 2; //VboC4V3f vbo = GenerateVboC4V3f(); //ArrayList<VertexC4V3f> vrx = new ArrayList<VertexC4V3f>(); //uint color_int = color.ToABGR(); //for (int i = 0; i < j; ++i) //{ // var v = vertextList[i]; // vrx.AddVertex(new VertexC4V3f(color_int, (float)v.m_X, (float)v.m_Y)); //} ////------------------------------------- //GL.EnableClientState(ArrayCap.ColorArray); //GL.EnableClientState(ArrayCap.VertexArray); //int pcount = vrx.Count; //vbo.BindBuffer(); //DrawTrianglesWithVertexBuffer(vrx, pcount); //vbo.UnbindBuffer(); //GL.DisableClientState(ArrayCap.ColorArray); //GL.DisableClientState(ArrayCap.VertexArray); ////-------------------------------------- } break; } } break; } }
public override void Load() { //draw 1 FormTestWinGLControl form = new FormTestWinGLControl(); var canvas = PixelFarm.Drawing.DrawingGL.CanvasGLPortal.P.CreateCanvas(0, 0, 800, 600); PixelFarm.Drawing.Bitmap bmp = null; PixelFarm.Drawing.TextureBrush tbrush = null; form.SetGLPaintHandler((o, s) => { canvas.ClearSurface(PixelFarm.Drawing.Color.White); //if (tbrush == null) //{ // var bitmap = new System.Drawing.Bitmap("../../../SampleImages/plain01.png"); // bmp = new PixelFarm.Drawing.Bitmap(bitmap.Width, bitmap.Height, bitmap); // tbrush = new PixelFarm.Drawing.TextureBrush(bmp); //} //fill rectangle is easy //canvas.FillRectangle(linearGrBrush, 0, 0, 150, 150); //canvas.FillRectangle(PixelFarm.Drawing.Color.Blue, 0, 0, 150, 150); //---------------------------------------------------- //2. fill polygon with gradient brush canvas.FillPolygon( PixelFarm.Drawing.Color.Red, new PixelFarm.Drawing.PointF[] { new PixelFarm.Drawing.PointF(0, 50), new PixelFarm.Drawing.PointF(50, 50), new PixelFarm.Drawing.PointF(10, 100) }); canvas.Note1 = 1; //temp var linearGrBrush = new LinearGradientBrush( new PixelFarm.Drawing.PointF(25, 25), PixelFarm.Drawing.Color.Black, new PixelFarm.Drawing.PointF(100, 100), PixelFarm.Drawing.Color.Blue); //canvas.FillPolygon( // tbrush, // new PixelFarm.Drawing.PointF[]{ // new PixelFarm.Drawing.PointF(60, 50), // new PixelFarm.Drawing.PointF(100, 50), // new PixelFarm.Drawing.PointF(70, 100)}); //canvas.FillPolygon( // linearGrBrush, // new PixelFarm.Drawing.PointF[]{ // new PixelFarm.Drawing.PointF(60, 50), // new PixelFarm.Drawing.PointF(100, 50), // new PixelFarm.Drawing.PointF(70, 100)}); canvas.FillPolygon( linearGrBrush, new PixelFarm.Drawing.PointF[] { new PixelFarm.Drawing.PointF(60, 50), new PixelFarm.Drawing.PointF(100, 50), new PixelFarm.Drawing.PointF(70, 100) }); canvas.Note1 = 0; //------------------------------------------------------------------------- }); form.Show(); }