Textured Polys for GLMax2D

BlitzMax Forums/BlitzMax Module Tweaks/Textured Polys for GLMax2D

Haramanai(Posted 2006) [#1]
First you must make a change in the source…
Open the file GLMax2D.Bmx
There is a Private. This private protects constants , globals and functions.
What we need is access to the functions BindTex , EnableTex and DisableTex.
There some ways to do this,
1. Nest the functions with a Public and Private.
This is going to look like this:
Public
BindTex( name )
…
EnableTex( name )
…
DisableTex( name )
…
Private

2. The other way is to make some custom functions anywhere in the Public area that will call the Private function.
Example:
Function BindTexture( name)
         BindTex( name )
End function

3. Or just put your functions in the end of the file like.
Rem
bbdoc: Loads a texture to be used with GLDrawTexture
End Rem
Function GLLoadTexture:Int(image:TImage)
	If Not image Return - 1
	Local texture:Int = GLTexFromPixmap(image.pixmaps[0]) 
	bindTex( texture )
	Return texture
End Function

Rem
bbdoc: Draws a textured polygon
End Rem
Function GLDrawTexture( texture:Int , xy:Float[] , uv:Float[] )
	EnableTex texture

	glBegin GL_POLYGON
	For Local i:Int=0 Until Len xy Step 2
		glTexCoord2f uv[i] , uv[i+1]
		glVertex2f xy[i] , xy[i+1]
	Next
	glEnd
End Function


Then you can create anything textured with the OpenGL commands.
For example take those functions ( I use the first method )
Function LoadTexture:Int(image:TImage)
	If Not image Return - 1
	Local texture:Int = GLTexFromPixmap(image.pixmaps[0]) 
	bindTex( texture )
	Return texture
End Function

Function DrawTex( texture:Int , xy:Float[] , uv:Float[] )
	EnableTex texture

	glBegin GL_POLYGON
	For Local i:Int=0 Until Len xy Step 2
		glTexCoord2f uv[i] , uv[i+1]
		glVertex2f xy[i] , xy[i+1]
	Next
	glEnd
End Function

GLTexFromPixmap(image.pixmaps[0]) this way of loading textures I think is the best. Cause you can use all the flags and almost commands that you use when you are load images. Like SetMaskColor.
Then we need two float arrays with the values of the xy of the points of the polygon and the uv.
Like
Local xy:Float[] = [0.0 , 0.0 , 640.0 , 0.0 , 640.0 , 480.0 ]
Local uv:Float[] = [0.0 , 0.0 , 1.0 , 0.0 , 1.0 , 1.0]

Here is a complete example.
SuperStrict

SetGraphicsDriver GLMax2DDriver()
Graphics 640 , 480

Local xy:Float[] = [0.0 , 0.0 , 640.0 , 0.0 , 640.0 , 480.0 ]
Local uv:Float[] = [0.0 , 0.0 , 10.0 , 0.0 , 10.0 , 10.0]

SetMaskColor 255 , 255 , 255
Local filter$="Image Files:png,jpg,bmp;Text Files:txt;All Files:*"
Local filename$=RequestFile( "Select graphic file to open",filter$ )
If Not filename Then End
Local texture:Int = LoadTexture( LoadImage(filename) )

DrawTex( texture , xy , uv ) 
Flip
WaitKey()


Function LoadTexture:Int(image:TImage)
	If Not image Return - 1
	Local texture:Int = GLTexFromPixmap(image.pixmaps[0]) 
	bindTex( texture )
	Return texture
End Function

Function drawTex( texture:Int , xy:Float[] , uv:Float[] )
	EnableTex texture

	glBegin GL_POLYGON
	For Local i:Int=0 Until Len xy Step 2
		glTexCoord2f uv[i] , uv[i+1]
		glVertex2f xy[i] , xy[i+1]
	Next
	glEnd
End Function


Keep in mind that you must create all the polygons clock-wish unless if you wand a flipped y texture.
Also remember that this is only for GLMax2D and before you can use the function LoadTexture you must set the Graphics Driver to the GLMax2DDriver and create the graphics content.

Sorry for my bad English and syntax.

I hope to get an Official way to draw textured polygons one day.
Any comments are welcome.