Non-power-of-two Textures Example

BlitzMax Forums/BlitzMax OpenGL Programming/Non-power-of-two Textures Example

Regarding my topic concerning pixel-perfect images
I figured out how to use non-power-of-two textures. For those who like an example:

Strict

Local c_width:Float = 640
Local c_height:Float = 480
GLGraphics c_width, c_height

Local myExtensions:String = String.FromCString( glGetString( GL_EXTENSIONS ) ).ToLower()

?macos
If myExtensions.find("gl_ext_texture_rectangle") = -1 Then
	RuntimeError "Extension not supported!"
	End
End If
?

glviewport (0,0, c_width, c_height)		' origin of avbl screen + width & height
glmatrixmode (gl_projection)			' modelview, projection or texture matrix
glloadidentity ()						' set 4x4 identity matrix
gluortho2d (0.0,c_width,c_height, 0.0)	' creates a matrix For projecting 2d coordinates
glclear(gl_color_buffer_bit)

glmatrixmode (gl_modelview)
glLoadidentity ()

' Enable non-power-of-two textures
glEnable (gl_TEXTURE_RECTANGLE_EXT)	' <--- !!!

' load a pixmap
Local pmap:TPixmap = LoadPixmap ("mapkansas.bmp")
If pmap.format<>PF_RGBA8888 pmap=pmap.Convert( PF_RGBA8888 ) ' convert to RGBA
Local w:Float = PixmapWidth (pmap)
Local h:Float = PixmapHeight (pmap)

' Create/Assign Texture
' All GL_TEXTURE_2D is replaced by GL_TEXTURE_RECTANGLE_EXT
Local texName:Int
glPixelStorei GL_UNPACK_ALIGNMENT, 1
glGenTextures 1, Varptr texName
glBindTexture gl_texture_rectangle_ext, texName ' <--- !!!
glTexImage2D gl_texture_rectangle_ext, 0, GL_RGBA8, pmap.width, pmap.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pmap.pixels ' <--- !!!

glBegin (gl_quads)

	' TL
	glTexCoord2f 0.0, 0.0
	glvertex2f 0.0, 0.0
	
	' TR
	glTexCoord2f w, 0.0 ' <--- !!! All TexCoord's are in pixels instead of [0.0 - 1.0]
	glVertex2f w, 0.0
	
	' BR
	glTexCoord2f w, h
	glVertex2f w, h
	
	' BL
	glTexCoord2f 0.0, h
	glVertex2f 0.0, h	
	
glEnd
Flip

WaitKey()
End


ghislain

Wow. That's actually really useful to me, thanks.

And how well is it supported? I mean older cards and intel onboard would drastically lose performance if the texture isn't power of two anymore ...
are you sure this removes the power of 2 at all and not just the "square" which is inforced due to the used / targeted configurations of BM? (DX7 and OpenGL 1.2)

Well, I'm not sure how well it is supported.
At home I've got the official 'OpenGl Programming Guide v1.2'.
It doesn't mention gl_texture_rectangle_ext.

But it wasn't introduced until OpenGl v1.4 I believe.
The newer documentation does support it.

ghislain

Its introduced in 1.4 as extension and officially supported sinde 2.0 as far as I know. Unfortunately that doesn't seem to mean that it is also supported on a wide variety on gfx-cards. I'd go with power-of-two-textures to be safe.