DrawPoly behaving strangely

Monkey Forums/Monkey Programming/DrawPoly behaving strangely

mjcamerer(Posted 2013) [#1]
Hello everyone,

I am working on a project where I need to use the DrawPoly. I have noticed that the DrawPoly command is working differently than expected on some targets. I wrote some small example code to show the differences.

Here is what I have been doing. I create a Float array and assign 6 X,Y coordinate points. Then I use the DrawPoly command to draw the shape. The shape looks just like it should and behaves as expected on HTML5 and Flash. On Android and XNA, the shape gets distorted:

Correct Image (from HTML5):


Wrong Image (from XNA):


Here is the sample code:

Strict

Import mojo

Function Main:Int()
	New Game()
	Return 0
End

Class Game Extends App

	Field shapeArray:Float[12]
	
	Method OnCreate:Int()
	
		'Create Shape
		shapeArray[0] = 190
		shapeArray[1] = 179
		shapeArray[2] = 189
		shapeArray[3] = 259
		shapeArray[4] = 336
		shapeArray[5] = 255
		shapeArray[6] = 332
		shapeArray[7] = 152
		shapeArray[8] = 273
		shapeArray[9] = 150
		shapeArray[10] = 239
		shapeArray[11] = 209
		
	
		SetUpdateRate(30)
		Return 0
		
	End Method
	
	Method OnRender:Int()
		Cls
		DrawPoly(shapeArray) 'Draw Shape
		Return 0
	End Method
	
End Class


If anyone has any ideas or spots something I may be doing wrong I would greatly appreciate it!


muddy_shoes(Posted 2013) [#2]
The docs state that DrawPoly draws convex polygons. Your example isn't convex. It just happens that the underlying function used in HTML5 is capable of drawing non-convex polygons also.


Floyd(Posted 2013) [#3]
It seems to be failing on the part which is not convex, i.e. the "dent" at the upper left.

What happens if you reverse the order of vertices in the array, so the polygon is traversed in the opposite direction? I would try it but I don't have XNA or Android available.


skid(Posted 2013) [#4]
I haven't tried it but there is a triangulate routine in the code forum which should help you out.


Beaker(Posted 2013) [#5]
Worth hacking hacking around with this: http://www.monkeycoder.co.nz/Community/posts.php?topic=917&


mjcamerer(Posted 2013) [#6]
Thanks all! I didn't realize DrawPoly was limited to convex polygons, I'll try to look closer at the docs next time. I was able to solve this problem for all platforms I mentioned by using JIM's code in the link that skid posted. Thanks!


Difference(Posted 2013) [#7]
Triangulation will leave seems on some targets (IOS and others), because the underlying drawing method will antialias the edges of the triangles.

This really needs to be adressed by BRL. I have hacked in several methods into Mojo, that makes it possible to draw convex polygons. They are very simple, and should IMHO really be a part of mojo.

DrawTriangles(points:Float[],indexes:Int[]) are the simplest and makes nice polygons (with holes) possible and fast on all targets.

I'm quite happy to share my (suboptimal) code, but the Mojo licence does not permit that I post it here.