Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pot Noodle on August 06, 2011
-
Hi guys,
Can anyone explain how to load a png image in as a texture with Blitzmax & Opengl
I have two textures allready but they are loaded using LoadAnimImage.
This texture is to be the background, I have tried loading as pixmap but I just keep getting errors
Thanks
-
What kind of errors, and could you perhaps post the code that load the images?
-
Here is my code for loading the textures: :-\
Local Image1:TImage = LoadAnimImage("IncBin::fonts.png", 32, 32, 0, 59) ' font textures, OK
Global Chars:TPixmap[] = Image1.pixmaps
Local Image2:TImage = LoadAnimImage("IncBin::Stars.png", 11, 11, 0, 3) ' star textures, OK
Global Star:TPixmap[] = Image2.pixmaps
Global Galaxy:TImage = LoadImage("IncBin::galaxy.png") 'background texture, not OK
I would like to just display it in the background.
I am using Opengl and the error I get is can't convert Timage to int
glBindTexture(GL_TEXTURE_2D, Galaxy) <--- Error here!
I have Googled for hours but all I find is things like Tpixmaps but this gives similar errors
I did not want a pixmap array just for one image or is that the only way?
-
The following are the two functions that BlitzResearch have in their glgraphics module for converting a pixmap to an opengl texture:
Rem
bbdoc: Helper function to calculate nearest valid texture size
about: This functions rounds @width and @height up to the nearest valid texture size
End Rem
Function GLAdjustTexSize( width Var,height Var )
Function Pow2Size( n )
Local t=1
While t<n
t:*2
Wend
Return t
End Function
width=Pow2Size( width )
height=Pow2Size( height )
Repeat
Local t
glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
If t Return
If width=1 And height=1 RuntimeError "Unable to calculate tex size"
If width>1 width:/2
If height>1 height:/2
Forever
End Function
Rem
bbdoc: Helper function to create a texture from a pixmap
returns: Integer GL Texture name
about: @pixmap is resized to a valid texture size before conversion.
end rem
Function GLTexFromPixmap( pixmap:TPixmap,mipmap=True )
If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
Local width=pixmap.width,height=pixmap.height
GLAdjustTexSize width,height
If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
Local old_name,old_row_len
glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len
Local name
glGenTextures 1,Varptr name
glBindtexture GL_TEXTURE_2D,name
Local mip_level
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
I have no idea why you would want to convert to an opengl texture unless you are not using any of the code from the BRL modules to draw your graphics and are doing a clean opengl implementation. When you load an image with the LoadImage and LoadAnimImage command you should be able to just draw them using the DrawImage command.
If you end a variable name with [] it becomes an array, so you should not have those at the end of your variable if you do not want an array.
If you try and use the "Galaxy" variable directly with "glBindTexture" it will fail because the Galaxy variable points to the image object, not to the pixel data itself. If you want to point to the pixel data then there is a couple of options one of which is using the LockImage() command which returns a pixmap from the image.
-
Hi zawran, I am not converting an Opengl texture, There is no need to lock the Image as this would only return a pixmap
But looking at the helper function gave me an idea and it worked.
Global Image3:TPixmap = LoadPixmap("IncBin::galaxy.png")
Global Galaxy:Int = GLTexFromPixmap(Image3)
Now Galaxy is an Int Texture so I can do this glBindTexture(GL_TEXTURE_2D, Galaxy) and it works :updance:
Thanks for your help and time.
-
:offtopic:
Are you working towards one big demo? :)
-
Hi Shockwave, Yes or though I would not say big.