DrawLine is offset by -1,-1 on GLFW

Monkey Targets Forums/Desktop/DrawLine is offset by -1,-1 on GLFW

Grey Alien(Posted 2012) [#1]
Hi all, I'm getting some weird behaviour with DrawLine. I noticed that 0,0 is actually off screen when it should be visible and 640,640 is on-screen but it should not be visible.

Also check out the 3rd line I'm drawing below, it should be diagonal with a step in the middle but it is straight. The 4th line is diagonal but only has 1 step instead of the expected 2 steps.

If you run it in HTML mode it appears to work OK although it's hard to tell as the lines are anti-aliased.

Import mojo

Function Main:Int()

	New MyApp

	Return 1
End

Class MyApp Extends App

	Method OnCreate:Int()
		'Set frame rate
		SetUpdateRate 60
	
		Return 1
	End Method
		
	Method OnUpdate:Int()
		
		Return 1
	End Method

	Method OnRender:Int()
		Cls(0,0,0)

		DrawLine 0,0,0,100
		DrawLine 640,0,640,100
		DrawLine 1,150,2,250
		DrawLine 1,300,3,400
		
		Return 1
	End Method

End Class



muddy_shoes(Posted 2012) [#2]
I vaguely recall that lines in OpenGL need to be defined with respect to pixel centres and not pixel boundaries. 0,0 is the top-left of the top-left pixel. Change the values to start and end at centre points (.5 of a pixel) and see if it works. Different GL implementations will have slightly different behaviours though.


Grey Alien(Posted 2012) [#3]
That could be it (for the 0,0 issue). I think maybe the same thing is happening in HTML5 except the lines are anti-aliased.

However that doesn't explain when line 3 is not diagonal.


muddy_shoes(Posted 2012) [#4]
It does because all the positions are boundary positions. The lines aren't crossing the pixels you think they are.


Grey Alien(Posted 2012) [#5]
Yep it seems that adding 0.5 to the line coords shows them as diagonal as is expected.