Image is not displayed correctly

BlitzMax Forums/BlitzMax OpenGL Programming/Image is not displayed correctly

Trying to create a function to draw polygon images. Or whatever they should be called. It's just a simple Max2D function.

It works but contains 2 bugs. The most irritating bug is that it looks like the image is folded. But why? And how do I stop it from doing that?
The second bug is that I need to use the image before I use it with this function, but that's just probably something I forgot to cut-n-paste from other sources in the codearchives and the max2D source, so no biggie.

But the folding bug irritates me a lot. Got any helpful advice?

(You need an image to try this code out.)
SuperStrict
Graphics 800,600,0

Local texture:timage = LoadImage("rock0_1.png")
Local poly:Float[] = [200.0,150.0, 200.0,450.0, 600.0,550.0, 600.0,50.0]
Local i:Int = 0

DrawImage( texture, 0,0 ) ' Bug in drawtexpoly, I must display the texture before I try to use it...
While Not KeyHit( KEY_ESCAPE )
	' Move the corners
	poly[5] = 450+Sin(i)*150
	poly[7] = 150-Sin(i)*150
	i:+1

	Cls
	drawtexpoly( poly, texture )
	Flip
Wend
End

Function drawtexpoly( verts:Float[] , Img:Timage, frame:Int = 0 )
	If Not img Then Throw "no img"
	If Not verts Then Throw "no verts"
	If Len(verts) <> 8 Then Throw "verts <> 8"

	Local x1:Int, y1:Int, x2:Int, y2:Int, x3:Int, y3:Int, x4:Int, y4:Int
	x1 = verts[0] ; y1 = verts[1]
	x2 = verts[2] ; y2 = verts[3]
	x3 = verts[4] ; y3 = verts[5]
	x4 = verts[6] ; y4 = verts[7]

	Local frm:TGLImageFrame = TGLImageFrame( Img.frames[0] )
	If frm = Null Then Return
	Local texEnabled:Int = glIsEnabled( GL_TEXTURE_2D )
	If texEnabled=0 Then
		glEnable( GL_TEXTURE_2D )
	EndIf
	
	Local lastTex:Int
	glGetIntegerv( GL_TEXTURE_BINDING_2D, Varptr lastTex )
	glBindTexture( GL_TEXTURE_2D, frm.name )

	glBegin GL_QUADS
		glTexCoord2f 0,0
		glVertex2f x1,y1
		glTexCoord2f 0,1
		glVertex2f x2,y2
		glTexCoord2f 1,1
		glVertex2f x3,y3
		glTexCoord2f 1,0
		glVertex2f x4,y4
	glEnd
	
	glBindTexture( GL_TEXTURE_2D, lastTex )
	If texEnabled=0 Then glDisable( GL_TEXTURE_2D )
EndFunction