Author Topic: Noise  (Read 1931 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Noise
« on: May 05, 2013, 07:07:05 AM »
Demonstrating the use of Noise functions to generate textures.

These functions use a pseudo-random series of numbers, at various frequencies with interpolated values.

 function Interpolation(float a,b,c) as float
  ============================================
  return a+(b-a)*c*c*(3-2*c)
  end function


  function InterpLinear(float a,b,c) as float
  ===========================================
  return a*(1-c)+b*c;                                          
  end function


  function Noise(int v) as float
  ==============================
  static double d=1/0x7fffffff
  mov eax,v
  rol eax,5
  mul v
  xor eax,0x9595adad
  mul v
  rol eax,3
  and eax,0x7fffffff 'always positive
  mov v,eax
  return v*d 'range 0..1.0
  end function

  'FOR COLORS ETC

  sub AddNoiseC(sys pp,wi,ht,st,r,g,b,a,off)
  ==========================================
  'pixbase,width,height,stride,colorval,offset
  sys x,y,mk
  byte rr,gg,bb,aa
  float v1,v2,v3,v4,v5,v6,v7,dv
  dv=1/st
  mk=-st
  for y=0 to <ht
    y1=y and mk
    y2=y1+st
    y3=y1*wi+off
    y4=y2*wi+off
    for x=0 to <wi
      x1=x and mk
      x2=x1+st
      v1=noise(y3+x1)
      v2=noise(y3+x2)
      v3=noise(y4+x1)
      v4=noise(y4+x2)
      '
      v5=interpolation(v1,v2,(x-x1)*dv)
      v6=interpolation(v3,v4,(x-x1)*dv)
      v7=interpolation(v5,v6,(y-y1)*dv)
      rr=r*v7
      gg=g*v7
      bb=b*v7
      aa=a*v7
      if a<0 then aa=255 'max alpha (opaque)
      *pp+=rr+gg*0x100+bb*0x10000+aa*0x1000000
      pp+=4
    next
  next
  end sub


  static sys res=256,texspace
  texspace=res*res*4
  string imgs[2]
  imgs[1]=nuls texspace
  imgs[2]=nuls texspace
  'GetTexImage "../images/crate.jpg",res,imgs[1]
  '
  'pixbase,width,height,stride,colorval,offset
  --------------------------------------------
  AddNoiseC strptr imgs[1],res,res,32,050,050,100,-1,0x53535353
  AddNoiseC strptr imgs[1],res,res,08,050,050,100, 0,0x35353535
  AddNoiseC strptr imgs[1],res,res,04,070,070,050, 0,0x53535353
  MakeTexture strptr imgs[1],res,res,texn[1]
  AddNoiseC strptr imgs[2],res,res,08,100,100,000,-1,0x35353535
  AddNoiseC strptr imgs[2],res,res,02,100,100,000, 0,0x35353535
  AddNoiseC strptr imgs[2],res,res,01,050,050,000, 0,0x35353535
  MakeTexture strptr imgs[2],res,res,texn[2]

The 2 .inc files belong in inc/

X