SetUV for OpenGL ?

BlitzMax Forums/BlitzMax Programming/SetUV for OpenGL ?

This should contain all you need...
'Why scrolling a non ^2 image may give unexpected results

Strict

SetGraphicsDriver GLMax2DDriver()
'SetGraphicsDriver BufferedD3D7Max2DDriver()

Graphics 800, 600, 0'32, -1

Local image:TImage = LoadImage(getenv_("BMXPATH") + "\samples\digesteroids\graphics\digestive.png")
If image = Null Then RuntimeError "Couldn't load image"
 
Local tw# = Pow2Size(image.width), th# = Pow2Size(image.height)
Local scalex# = (tw# - image.width) / image.width
Local scaley# = (th# - image.height) / image.height
Local scrollx#, scrolly#, showtex

SetBlend ALPHABLEND

SetTextureRepeat(image, True)

Repeat	
	If KeyDown(KEY_LEFT) Then scrollx# :+ 0.01
	If KeyDown(KEY_RIGHT) Then scrollx# :- 0.01
	If KeyDown(KEY_UP) Then scrolly# :+ 0.01
	If KeyDown(KEY_DOWN) Then scrolly# :- 0.01
	
	If KeyHit(KEY_SPACE) Then showtex = 1 - showtex
	
	Cls
	
	If showtex
		SetAlpha 0.33
		SetColor 0, 0, 100
		DrawRect 12, 36, tw#, th#
			
		SetColor 255, 255, 255
		SetUV(image, 1 + scrollx#, scrolly#, 1 + scalex# + scrollx#, 1 + scrolly#)
		SetScale scalex#, 1
		DrawImage image, 12 + image.width, 36
		
		SetUV(image, 1 + scrollx#, 1 + scrolly#, 1 + scalex# + scrollx#, 1 + scaley# + scrolly#)
		SetScale scalex#, scaley#
		DrawImage image, 12 + image.width, 36 + image.height
		
		SetUV(image, scrollx#, 1 + scrolly#, 1 + scrollx#, 1 + scaley# + scrolly#)
		SetScale 1, scaley#
		DrawImage image, 12, 36 + image.height
	EndIf
	
	SetAlpha 1
	SetScale 1, 1
	SetColor 0, 0, 100
	DrawRect 12, 36, image.width, image.height
	
	SetColor 255, 255, 255
	SetUV(image, scrollx#, scrolly#, 1 + scrollx#, 1 + scrolly#)
	DrawImage image, 12, 36
	
	DrawText "Show Texture = " + showtex, 12, 12
	DrawText "Hit SPACE to toggle texture, CURSOR keys to scroll image", 200, 576
	
	Flip 1
Until KeyHit(KEY_ESCAPE)

End

Function SetTextureRepeat(image:TImage, rep=True)' Under DX, *ALL* images drawn will repeat
'	Assert (image.width = Pow2Size(image.width)) And (image.height = Pow2Size(image.height)),..
'					"Image width and height must be a power of two"
	Local D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)
	
	If rep
?Win32 
		If D3D7Driver
			'DX
			D3D7Driver.device.SetTextureStageState 0, D3DTSS_ADDRESS, D3DTADDRESS_WRAP
		Else
?
			'GL		
			glBindTexture GL_TEXTURE_2D, TGLImageFrame(image.Frame(0)).name
			glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT
			glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT
?Win32
		EndIf
?
	Else
?Win32
		If D3D7Driver
			'DX
			D3D7Driver.device.SetTextureStageState 0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP
		Else
?
			'GL
			glBindTexture GL_TEXTURE_2D, TGLImageFrame(image.Frame(0)).name
			glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
			glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
?Win32
		EndIf
?
	EndIf
End Function

Function SetUV(image:TImage, u0!, v0!, u1!, v1!, frame=0)
	Local twr! = Double(image.width) / Pow2Size(image.width)
	Local thr! = Double(image.height) / Pow2Size(image.height)
	
	u0! = twr! * u0!
	v0! = thr! * v0! 
	u1! = twr! * u1!
	v1! = thr! * v1!
	
?Win32			
	If TGLMax2DDriver(_max2dDriver)
?
		'GL
		Local GLImageFrame:TGLImageFrame = TGLImageFrame(image.Frame(frame))

		GLImageFrame.u0# = u0!
		GLImageFrame.v0# = v0!
		GLImageFrame.u1# = u1!
		GLImageFrame.v1# = v1!
?Win32
	Else
		'DX
		TD3D7ImageFrame(image.Frame(frame)).SetUV(u0!, v0!, u1!, v1!)
	EndIf
?
End Function

Function Pow2Size(n)
	Local t = 1
	
	While t < n
		t :* 2
	Wend
	
	Return t
End Function