Author Topic: Loading Texture  (Read 4422 times)

0 Members and 1 Guest are viewing this topic.

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Loading Texture
« 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

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: Loading Texture
« Reply #1 on: August 07, 2011 »
What kind of errors, and could you perhaps post the code that load the images?

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Loading Texture
« Reply #2 on: August 07, 2011 »
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?

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: Loading Texture
« Reply #3 on: August 07, 2011 »
The following are the two functions that BlitzResearch have in their glgraphics module for converting a pixmap to an opengl texture:

Code: [Select]
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.

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Loading Texture
« Reply #4 on: August 08, 2011 »
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.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Loading Texture
« Reply #5 on: August 09, 2011 »
 :offtopic:
Are you working towards one big demo? :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Loading Texture
« Reply #6 on: August 10, 2011 »
Hi Shockwave,  Yes or though I would not say big.