About Monkey 2 › Forums › Monkey 2 Programming Help › Rendering Gradients?
Tagged: gradients
This topic contains 7 replies, has 4 voices, and was last updated by
scurty 1 year, 4 months ago.
-
AuthorPosts
-
December 4, 2017 at 11:57 pm #12119
Hate to be “that guy” my knowledge of rendering of no0b level.
How does one draw this kind of primitive using mojo? Or does it have to be done in GL?
To be clear without an Image if possible.
I see DrawPrimitives() though not sure how to utilize the arguments.December 5, 2017 at 5:14 am #12129I’m not passing texture coordinates or an image, just the vertex coordinates and the colors for each vertex.
You could turn this into a function that draws the primitive using rotation, etc.Notice that you can have each vertex a different color to get a 4 color gradient!
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowGlobal verts:= New Stack<Float>Global colors:= New Stack<UInt>Method New( title:String="Gradient",width:Int=800,height:Int=600,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )'vertex pairs: top left, top right, bottom right, bottom leftverts.AddAll( New Float[]( 100, 100, Width-100, 100, Width-100, Height-100, 100, Height-100 ) )'colors: same order as vertex pairscolors.AddAll( New UInt[]( RGBA( Color.Red ), RGBA( Color.Yellow ), RGBA( Color.Yellow ), RGBA( Color.Red ) ) )ClearColor = Color.DarkGreyEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawPrimitives( 4, verts.Length/8, verts.Data.Data, 8, Null, 8, colors.Data.Data, 4, Null, Null )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndFunction RGBA:UInt( c:Color )Return UInt(c.a*255) Shl 24 | UInt(c.b*255) Shl 16 | UInt(c.g*255) Shl 8 | UInt(c.r*255)EndDecember 5, 2017 at 6:33 am #12130Nice!
One note about converting Color into UInt:
Monkey123Function RGBA:UInt( c:Color )Return UInt(c.a*255) Shl 24 | UInt(c.b*255) Shl 16 | UInt(c.g*255) Shl 8 | UInt(c.r*255)EndThere is an ABGR format! I suppose, it’s a native opengl format.
That’s why if you write color as $FF0000FF you’ll get Red color but you can expect Blue here.
December 5, 2017 at 1:08 pm #12143Fantastic! So much better than what I was gonna do. Thanks.
December 5, 2017 at 10:25 pm #12153There is an ABGR format
Thanks, I was wondering about that.
When I tried actual RGBA order I was getting the wrong colors and didn’t know why!December 6, 2017 at 3:36 am #12163Hmmm, I could possibly add a ‘GradientColors’ array or similar to canvas for this?
December 6, 2017 at 4:13 am #12166Something like:
Monkey12345DrawGradientRect( r:Rectf,startColor:Color,endColor:Color,rotation:Float=0 )(and the same with x,y,w,h for bounds)DrawGradientCircle( origin:Vec2f,radius:Float,centerColor:Color,partsColors:Color[],partsPercentage:Float[] )(and the same with x,y for origin)Don’t know how to implement Circle gradient, but maybe you know.
Or to make centerColor an usual part with percentage of 0% and to remove this parameter.
And a custom case for circle is using 2 colors – center and outer:
Monkey12DrawGradientCircle( 50,50,200,Color.Pink,New Color[](Color.Sky),New Float[](1) )' draws circle at 50,50 with radius 200 with colors from Pink at center to Sky at the outer radius; 1 = 100%December 7, 2017 at 2:53 pm #12190That would be cute, lovely and waay easier than accessing the raw pointer from an Array.
I picture:Monkey123456789Local GradientColors = New GradientColor(points_array, colors_array)' then on renderCanvas.DrawPolyGradient(poly_points_array, GradientColors)' orCanvas.ColorContext = GradientColorsCanvas.DrawCircle(bleh)ColorContext would be new of course. Or just use .Color and have GradientColor extend Color
-
AuthorPosts
You must be logged in to reply to this topic.