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=640
Window "Ray3" ,scr,scr,2
sys_mode=4


ClsColor 32,0,0
FlipBuffer
ClsColor 0,0,0


'light
'=====

single lx,ly,lz

lx=.6933
ly=.6933
lz=.6933

lx=.707
ly=.707
lz=0


function sphere(sys px,py,ra,single pr,pg,pb,tx)
'===============================================
sys x,y,ri,pp,d,cc
single  r,nx,ny,nz
single li
for y=-ra to ra
for x=-ra to ra
r=hypot(x,y)
ri=r
if r<=ra
  nx=x/ra
  ny=y/ra
  nz=sqr(1-nx*nx-ny*ny) 'assume positive
  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,640-py-y
    pp=PixelAvg cc,pp
    pixel px+x,640-py-y,pp
  end if
end if
next
next
end function


'objects
'=======

'sphere posX, posY, radius, red/1, green/1, blue/1, texture/1
sphere 120,520,70,.7,.0,.5,.02
sphere 420,500,80,.5,.0,.7,.1
sphere 320,320,200,1,1,.5,.05
sphere 300,200,150,1,.3,.5,.03
sphere 350,200,100,1,1,1,.07

FlipBuffer
pause
