int ambi=31  'light ambient / 255
int diff=224 'light diffuse / 255

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 function
End Function




function color(single li,pr,pg,pb) as sys
'========================================
sys cr,cg,cb
  if li>1 then li=1 'clamp
  if li<0 then li=0 'clamp
  cr=(diff*li+ambi)*pr
  cg=(diff*li+ambi)*pg
  cb=(diff*li+ambi)*pb
  return cb*0x10000+cg*0x100+cr 'composite
end function

function texture(single li,pr,pg,pb,tx,x,y) as sys
'=================================================
  sys cr,cg,cb,mz,my
  single n
  n=1-tx*3
  single mx=x*.25
  single my=y*.25
  n+=noise(mx+my*0x7fff)*tx
  mx=x*.5
  my=y*.5
  n+=noise(mx+my*0x7fff)*tx
  mx=x
  my=y
  n+=noise(mx+my*0x7fff)*tx
  '
  if li>1 then li=1 'clamp
  if li<0 then li=0 'clamp
  cr=(n*(diff*li+ambi))*pr
  cg=(n*(diff*li+ambi))*pg
  cb=(n*(diff*li+ambi))*pb
  return cb*0x10000+cg*0x100+cr 'composite
end function


function PixelAvg(sys cc,pp) as sys
'==================================
  lea esi,pp
  mov ecx,[cc]
  mov edx,[esi]
  'RED
  xor eax,eax
  mov al,cl
  add al,dl
  jnc fwd nc
  add eax,256
  .nc
  shr eax,1
  mov [esi],al
  'GREEN
  xor eax,eax
  shr ecx,8
  shr edx,8
  inc esi
  mov al,cl
  add al,dl
  jnc fwd nc
  add eax,256
  .nc
  shr eax,1
  mov [esi],al
  'BLUE
  xor eax,eax
  shr ecx,8
  shr edx,8
  inc esi
  mov al,cl
  add al,dl
  jnc fwd nc
  add eax,256
  .nc
  shr eax,1
  mov [esi],al
  return pp
end function


