Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fbgfx.bi"
  2. const TEX_MASKED = &h1
  3. const TEX_MIPMAP = &h2
  4. const TEX_NOFILTER = &h4
  5. const TEX_HASALPHA = &h8
  6.  
  7. '------------------------------------------------------------------------
  8. '' Create texture creates textures from BLOAD buffer
  9. private function CreateTexture( byval buffer as any ptr, byval flags as integer = 0 ) as uinteger
  10.    
  11.     dim p as uinteger ptr
  12.     dim as integer w, h, x, y, col
  13.     dim tex as uinteger
  14.     dim as GLenum format, minfilter, magfilter
  15.     dim as FB.PUT_HEADER ptr header = buffer
  16.  
  17.     function = 0
  18.    
  19.     if header->type = FB.PUT_HEADER_NEW then
  20.         w = header->width
  21.         h = header->height
  22.     else
  23.         w = header->old.width
  24.         h = header->old.height
  25.     end if
  26.    
  27.  
  28.     if( (w < 64) or (h < 64) ) then
  29.         exit function
  30.     end if
  31.     if( (w and (w-1)) or (h and (h-1)) ) then
  32.         '' Width/height not powers of 2
  33.         exit function
  34.     end if
  35.  
  36.     redim dat(0 to (w * h) - 1) as uinteger
  37.     p = @dat(0)
  38.  
  39.     glGenTextures 1, @tex
  40.     glBindTexture GL_TEXTURE_2D, tex
  41.  
  42.     for y = h-1 to 0 step -1
  43.         for x = 0 to w-1
  44.             col = point(x, y, buffer)
  45.             '' Swap R and B so we can use the GL_RGBA texture format
  46.             col = rgb(col and &hFF, _
  47.                 (col shr 8) and &hFF, _
  48.                 (col shr 16) and &hFF)
  49.             if( (flags and TEX_MASKED) and (col = &hFF00FF) ) then
  50.                 *p = 0
  51.             else
  52.                 *p = col or &hFF000000
  53.             end if
  54.             p += 1
  55.         next x
  56.     next y
  57.  
  58.     if (flags and (TEX_MASKED or TEX_HASALPHA)) then
  59.         format = GL_RGBA
  60.     else
  61.         format = GL_RGB
  62.     end if
  63.  
  64.     if (flags and TEX_NOFILTER) then
  65.         magfilter = GL_NEAREST
  66.     else
  67.         magfilter = GL_LINEAR
  68.     end if
  69.  
  70.     if( flags and TEX_MIPMAP) then
  71.         gluBuild2DMipmaps GL_TEXTURE_2D, format, w, h, GL_RGBA, _
  72.             GL_UNSIGNED_BYTE, @dat(0)
  73.  
  74.         if (flags and TEX_NOFILTER) then
  75.             minfilter = GL_LINEAR_MIPMAP_NEAREST
  76.         else
  77.             minfilter = GL_LINEAR_MIPMAP_LINEAR
  78.         end if
  79.     else
  80.         glTexImage2D GL_TEXTURE_2D, 0, format, w, h, 0, GL_RGBA, _
  81.             GL_UNSIGNED_BYTE, @dat(0)
  82.         minfilter = magfilter
  83.     end if
  84.  
  85.     glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter
  86.     glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter
  87.  
  88.     function = tex
  89.  
  90. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement