DrawPrimitives (mojo2) – Setting Color

Monkey Forums/Monkey Programming/DrawPrimitives (mojo2) – Setting Color

Shinkiro1(Posted 2016) [#1]
So I am using DrawPrimitives(order:Int, count:int, vertices:Float[], material:Material=null) for drawing particles. Is there a way to supply color data for each x,y pair?
I would envision something like this:
DrawPrimitives(order:Int, count:int, vertices:Float[], color:Float[], material:Material=null) 


The cool thing is that DrawPrimitives is a LOT faster than submitting each particle individually. (Like 3ms for ~500k particles on my MacBook)


Shinkiro1(Posted 2016) [#2]
Ok, got it. I had to hack mojo.graphics for this as BeginPrims is private (should probably be protected). I added the following method.

Method DrawPrimitivesColored:Void( order:Int,count:Int,vertices:Float[],pmColors:Int[],material:Material=Null )
		Local pmcolor:=_pmcolor
		
		BeginPrims material,order,count
		Local p:=0
		For Local i:=0 Until count
			_pmcolor=pmColors[i]
			For Local j:=0 Until order
				PrimVert vertices[p],vertices[p+1],0,0
				p+=2
			Next
		Next
		
		_pmcolor=pmcolor
	End


pmColors is an array of premultiplied colors you provide (has the same length as count).
I like this solution as it is really performant.

The drawback is that it's different from the rest of the API in that it takes pm colors instead of RGBA Floats.
Also I am not sure if I am packing the colors right. It looks right, but in DrawGradientRect() the colors first get multiplied with the current color.
Do I have to do the same here?

Function PremultiplyColors:Int(r:Float, g:Float, b:Float, a:Float)
    a *= 255
    Return Int(a) Shl 24 | Int(b * a) Shl 16 | Int(g * a) Shl 8 | Int(r * a)
End


Here is a little particle effect I made :)