Texture Problem

BlitzMax Forums/OpenGL Module/Texture Problem

Jayjay(Posted 2011) [#1]
Hi and thanks in advance!

I want to use textures and after looking in the forums I see that the following should work:

glEnable GL_TEXTURE_2D
Local image:TImage = LoadImage "C:\Users\Jason\Desktop\BlitzCode\Texture01.bmp")
Local GLTexture:Int = GLTexFromPixmap(image.Pixmaps[0])


But It keeps given me an error saying it cannot get the dimensions of the pixmap?

I have tried using a 512 x 512 size and a 32 x 32 size but the same occurs. Any ideas where I am going wrong?

Thanks


ImaginaryHuman(Posted 2011) [#2]
Does it do the same when you LoadPixmap instead?


Jayjay(Posted 2011) [#3]
Yes, it does and I have tried different file types and sizes ie. jpg, bmp, 512,512 or 128,128 or 32,32??

Any Ideas?


beanage(Posted 2011) [#4]
Try LockImage() for obtaining the pixmap.


Samichan(Posted 2011) [#5]
Local myimg:TImage = LoadImage("whatever.png")
Local myGLtex:Int = TGLImageFrame(myimg.Frame(0)).Name


ImaginaryHuman(Posted 2011) [#6]
Ya, if you LoadImage it's already created an OpenGL texture.


jkrankie(Posted 2011) [#7]
yeah, you'd be better to load a pixmap. Also, GLTexFromPixmap can be funny on some intel systems and give an 'Unable to calculate tex size' style error. I use these replacement functions in my games to get around this:

Function TexFromPixmap:Int(pixmap:TPixmap, mipmap:Int = True)
	If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
	Local width:Int=pixmap.width,height:Int=pixmap.height
	AdjustTexSize width,height
	If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
	
	Local old_name:Int,old_row_len:Int
	glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
	glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len

	Local Name:Int
	glGenTextures 1,Varptr name
	glBindtexture GL_TEXTURE_2D,name
	
	Local mip_level:Int
	Repeat
		glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]
		glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.pixels
		If Not mipmap Exit
		If width=1 And height=1 Exit
		If width>1 width:/2
		If height>1 height:/2
		pixmap=ResizePixmap( pixmap,width,height )
		mip_level:+1
	Forever
	
	glBindTexture GL_TEXTURE_2D,old_name
	glPixelStorei GL_UNPACK_ROW_LENGTH,old_row_len

	Return name
End Function

Function AdjustTexSize(Width:Int Var, Height:Int Var)
	Function Pow2Size:Int(N:Int)
		Local Size:Int

		Size = 1
		While Size < N
			Size = Size Shl 1
		Wend

		Return Size
	End Function

	Width  = Pow2Size(Width)
	Height = Pow2Size(Height)
End Function



Cheers
Charlie