include "..\GDIWindow\window.inc"
include "RenderUtil.inc"

Sub Pause()
AscKey=0
While AscKey=0
  Events
  sleep 50
  iF WinExit=1
    WinEnd
    ExitProcess 0
  End iF
Wend
End Sub

sys scr=800
Window "Ray4" ,scr,scr,2
sys_mode=4


ClsColor 32,0,0
FlipBuffer


'light
'=====

single lx,ly,lz


lx=sqr(1/3)
ly=lx
lz=lx

'lx=sqr(.5)
'ly=lx
'lz=0


function sphere(sys px,py,ra, single yx,pr,pg,pb,tx)
'===================================================
sys x,y,ri,pp,d,cc
single  r,nx,ny,nz,h
single li
single ry=ra*yx
'sys    ry=ra*yx
for y=-ry to ry
for x=-ra to ra
r=hypot(x,y/yx)
ri=r
if r<=ra
  nx=x/ra
  ny=y/ry
  nz=sqr(1-nx*nx-ny*ny) 'assume positive
  ny*=yx
  h=1/sqr(nx*nx+ny*ny+nz*nz)
  nx*=h
  ny*=h
  nz*=h
  li=lx*nx+ly*ny+lz*nz
  'cc=color li,pr,pg,pb
  cc=Texture li,pr,pg,pb,tx,x,y
  pixel px+x,scr-py-y,cc
  '
  'ANTIALIAS AVERAGING ON SPHERE EDGES
  '
  if ri=ra
    if x>=0 then d=1 else d=-1
    pp=Getpixel bHdc,px+x+d,scr-py-y
    pp=PixelAvg cc,pp
    pixel px+x,scr-py-y,pp
  end if
end if
next
next
end function


'objects
'=======

'background

single f,i
int x,y
for x=0 to <scr
i=6
for y=0 to <scr
pixel x,scr-y,i*(1+sin(f))
i+=.1
next
f+=.05
next

'sphere posX, posY, radius, yxRatio, red/1, green/1, blue/1, texture/1
sphere 220,640,70,.75,.7,.0,.5,.02
sphere 520,600,80,1,.5,.0,.7,.1
sphere 320,420,200,1,1,1,.5,.05
sphere 400,250,150,1,1,.3,.5,.03
sphere 550,250,100,1.9,1,1,1,.07
sphere 650,200,100,1.5,1,1,1,.07



FlipBuffer
pause
