

  '=======
  'TEXTURE
  '=======

  '19:53 04/05/2011
  '13:15 28/11/2011

  type Pixel4
    dword whole
    =
    byte r,g,b,a
  end type
  '



  '===============
  'NOISE FUNCTIONS
  '===============

  sys seed=0x12345678

  '-----------------------
  Function Rnd() as Single
  '=======================
  '
  static Single f, d=1/0x7fffffff
  mov eax,seed
  rol eax,7
  imul eax,eax,13
  mov seed,eax
  push eax
  fild dWord [esp]
  add esp,4
  fmul dWord d
  fstp dWord f
  return f
  End Function

  '----------------------
  Function Rndi() as long
  '======================
  '
  mov eax,seed
  xor eax,0x35353535
  imul eax,eax
  ror eax,17
  imul eax,seed
  ror eax,7
  mov seed,eax
  return eax
  End Function


  '------------------------------
  Function Noise(sys i) as Single
  '==============================
  Static As Single f, d=1/0x7fffffff
  mov eax,i
  xor eax,0x35353535
  imul eax,eax
  ror eax,17
  imul eax,i
  ror eax,7
  push eax
  fild dWord [esp]
  add esp,4
  fmul dWord d
  fstp dWord returnvar
  End Function


  '-----------------------------
  Function Noiseb(sys i) as long
  '=============================
  Static As Single f, d=1/0x7fffffff
  mov eax,i
  xor eax,0x35353535
  imul eax,eax
  ror eax,17
  imul eax,i
  ror eax,7
  and eax,255
  mov returnvar,eax
  End Function




  '==============================
  'INTERPOLATION FOR PERLIN NOISE
  '==============================


  '-----------------------------------------
  Sub CosInterpolate(single*a, sys n, sys s)
  '=========================================
  '
  'a array  of values' starting array value passed byref
  'n number of points period
  's stride
  '
  'example: Interpolate a[1],100,10
  'places interpolated values at a[11] a[21] .. a[91]
  '
  indexbase 0
  '
  sys i
  single v1,v2,sf,vf
  v1=a[0]
  v2=a[n]
  sf=pi*s/n
  n-=1
  for i=1 to n step s
    vf=.5*cos(sf*i)+.5
    a[i]=v1*(1-vf) + v2*vf
  next
  end sub


  '----------------------------------------
  Sub InterpolateXY(single *a, sys nx,ny,s)
  '========================================
  indexbase 0
  sys i,j,k,t,u
  t=nx*s 'element in each row
  u=t*s  'next row in framework
  nx--
  ny--
  'interpolate x framework
  'rows are spaced s apart
  for j=0 to ny step s
  for i=0 to nx step s
    CosInterpolate a[k+i],s,1
  next
  k+=u
  next
  'interpolate y tofill in the rest
  k=0
  for j=0 to nx
  k=j
  for i=0 to ny step s
    CosInterpolate a[k],s,t
    k+=u
  next    
  next 
  end sub


  '---------------------------------------------
  Sub InterpolateBlock(single *a, sys nx,ny,s,b)
  '=============================================
  sys i,j,k,t,u
  'SEED NOISE
  for j=0 to ny step s
  for i=0 to nx step s
  k=j*nx+i
  a[k]=noise[k+b]
  next
  next
  'INTERPOLATE
  InterpolateXY a,nx,ny,s
  end sub

  


  '=====================
  'CREATE OPENGL TEXTURE
  '=====================


  '-----------------------------------------------------
  Sub MakeTexture(sys PixelArray, xSize, ySize, Texnum )
  '=====================================================
  '
  glBindTexture GL_TEXTURE_2D, texNum
  '
  'GL_NEAREST
  'GL_LINEAR
  '
  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
  '
  'GL_NEAREST
  'GL_LINEAR
  'GL_NEAREST_MIPMAP_NEAREST         0x2700
  'GL_LINEAR_MIPMAP_NEAREST          0x2701
  'GL_NEAREST_MIPMAP_LINEAR          0x2702
  'GL_LINEAR_MIPMAP_LINEAR           0x2703
  '
  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
  glTexImage2D GL_TEXTURE_2D, 0, 4, xSize, ySize, 0, GL_RGBA, GL_UNSIGNED_BYTE, PixelArray
  '
  End Sub

  #ifdef GDIplus

  '============================
  'GDIPLUS TEXTURE IMAGE LOADER
  '============================

  '-----------------------------------------------------
  sub GetTexImage(string name, sys size, string*texture)
  '=====================================================
  '
  'typedef void *pvoid
   typedef sys pvoid
  '
  int     hstatus
  pvoid   pImage,pThumbnail
  int     width,height
  wstring filename
  '
  '
  ' // Create an image and a thumbnail of the image.
  '
  FileName = name 'widestring
  hStatus = GdipLoadImageFromFile FileName, pImage
  hStatus = GdipGetImageThumbnail pImage, size, size, pThumbnail, NULL, NULL
  '
  sys pb,w,ref,x,y,xe,ye
  '
  pb=pthumbnail
  hStatus = GdipImageRotateFlip pb, RotateNoneFlipY ' =6 invert
  pixel4 pix
  ref=1
  texture=nuls 4*size*size
  xe=size-1 : ye=xe
  w=*texture
  for y=0 to ye
    for x=0 to xe
      GdipBitmapGetPixel pb, x, y, pix.whole
      'swap pix.r, pix.b
      lea ecx,pix.whole
      mov eax,[ecx]
      mov [ecx+2],al
      shr eax,16
      mov [ecx],al
      *w=pix.whole : w+=4
      'GdipBitmapSetPixel pb, x, y, pix.whole
    next
  next
  '
  'Cleanup
  '
  if pThumbnail then GdipDisposeImage pThumbnail
  if pImage then GdipDisposeImage pImage

  end sub

  #endif


