Create Textures

BlitzMax Forums/MiniB3D Module/Create Textures

Shagwana(Posted 2009) [#1]
Well I want to be able to load and plot out my own custom texture loader however I stumbled across a problem!.

The reason I want this is so I can alter and build custom textures for my cars in my game, painting one yellow, another green etc etc.


I can use createtexture() to create my texture, however now how do I fill it with the colours I want. There is a BackBufferToTex() command, but that does not fill the pixmap associated with the texture with the pixels I want.

I can see the pixmap attached to the textures, they are free to be edited - as long as they are not resized!.

However I think a PushToGfxMem() command could be needed!. This would also be handy should the opengl contex be lost and we need to re-upload the textures to the graphics card.


Shagwana(Posted 2009) [#2]
I had a moment of clarity at work today, I guess I could use loadtextxture(pixmap) in order to load my texture that I have created!

After testing, this does not work
Local pGfxImage:TPixmap=LoadPixmap(sBasePath+"RndTexture1.jpg")
Local pTexture:TTexture=LoadTexture(pGfxImage)

ah boobies!


Shagwana(Posted 2009) [#3]
What I think is needed after a little play around is that all the loading is made stream based in minib3d - rather then the current file based.


jkrankie(Posted 2009) [#4]
I'll save you some time ;) Eventually you would have found the built in texfrompixmap function. After that you'd find out it doesn't work very well on some machines.

The following is a composite of various solutions that people have worked on whilst trying to get around the "unable to calculate tex size" issue. I can guarantee this works on pretty much everything, including crappy Intel GMA chips. I can't remember the people who's code this is, sorry.

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

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


you use it like this, which i've copied from my game:

Global glpixmap:TPixmap = LoadPixmap("images/particletextures.png")
Global glparticletex:Int = texfrompixmap(glpixmap)


then bind it when you want to draw.

Hope this helps...

Cheers
Charlie


jkrankie(Posted 2009) [#5]
How you turn that into a TTexture (if that's what you want) is down to you though, i didn't need to take it that far. I used some 'raw' GL to make a simple particle wotsit, as MiniB3D's sprites aren't quick enough for me (i'm throwing thousands about!)

Cheers
Charlie


Shagwana(Posted 2009) [#6]
btw this line
glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]

from your posted code was causing problems for me in minib3d when loading 24bit + textures. See this thread for what I was having issues with.


jkrankie(Posted 2009) [#7]
Ah, Well i'm only loading 8-bit bmps i think. better check though, thanks :)

Was the code any help? or did i miss the point? I only read your second post...

Cheers
Charlie


Sonic(Posted 2013) [#8]
sorry to revive this old thread, but i've run into a similar issue.

i am using minib3d and tsprites which are pre-generated at runtime by overlaying several images. currently i "savepixmappng" and then "loadanimtexture" to get these into ttexture format, but i've found it fairly unstable on multiple uses.

does anyone have a handy "pixmap to animtexture" function they would be willing to share? i'd be willing to pay for it to be written, also, as it would really help my process.

thanks,

jasper


Kryzon(Posted 2013) [#9]
Do you have an e-mail?


Sonic(Posted 2013) [#10]
i do! it's jasper (at) superflatgames dot com...

cheers!