gosub setup
gosub images
gosub fog
gosub matrix
gosub lights
gosub sun
gosub camera
gosub sky
Peak(1)
 
ink rgb(255,255,255),0
 
`LOOP
REPEAT
   text 0,0,"Press RETURN to generate a random mountain."
   inc scroll#,.000003
   for f = 1 to 5
      scroll object texture f+10,scroll#*f,scroll#*rnd(f)
   next f
 
   if ReturnKey()=1 then repeat:until Returnkey()=0 : Peak(1)
   sync
UNTIL Mouseclick()=1
 
end
 
`--------------------
` Functions
`--------------------
FUNCTION Peak(m)
   For z = 0 to 20
      For x = 0 to 20
         diff#=ABS(10-x)+ABS(10-z)
         h#=(100-(diff#*6))-rnd(diff#^1.5)
         if h#<0 then h#=0
 
         set matrix height m,x,z,h#
         if x=0 or z=0 or x=20 or z=20 then set matrix height m,x,z,0
      Next x
   Next z
 
   Normalise(m)
   Update Matrix m
ENDFUNCTION
`----
`By Lee Bamber From DB Example - Adds shaded areas to matrix to give depth
Function Normalise(MatNum)
  For z=1 to 20
    For x=1 to 20
      h8#=get matrix height(MatNum,x,z-1)*5
      h4#=get matrix height(MatNum,x-1,z)*5
      h#=get matrix height(MatNum,x,z)*5
      h2#=get matrix height(MatNum,x,z)*5
      x1#=(x-1)*25.0
      y1#=h#
      x2#=(x+0)*25.0
      y2#=h4#
      dx#=x2#-x1#
      dy#=y2#-y1#
      ax#=atanfull(dx#,dy#)
      ax#=wrapvalue(90-ax#)
      z1#=(z-1)*25.0
      y1#=h2#
      z2#=(z+0)*25.0
      y2#=h8#
      dz#=z2#-z1#
      dy#=y2#-y1#
      az#=atanfull(dz#,dy#)
      az#=wrapvalue(90-az#)
      nx#=sin(ax#)
      ny#=cos(ax#)
      nz#=sin(az#)
      Set matrix normal MatNum,x,z,nx#,ny#,nz#
    next x
  next z
  Update Matrix MatNum
EndFunction
 
`--------------------
` Gosubs
`--------------------
Setup:
   set display mode 1024,768,32
   randomize timer()
   hide mouse
   sync on
   sync rate 30
   autocam off
Return
`----
Matrix:
   make matrix 1,100,100,20,20
   set matrix wireframe off 1
   prepare matrix texture 1,1,20,20
   for z = 0 to 19
      for x = 0 to 19
         set matrix tile 1,x,z,x+(z*20)
      next x
   next z
Return
`----
Lights:
   color ambient light 0
   set ambient light 20
   make light 1
   set directional light 0,0,0,1
   set directional light 1,1,-.5,1
   color backdrop rgb(0,100,255)
Return
`----
Sun:
   for s = 10 to 25
      make object sphere 10+s,s*3
      color object 10+s,rgb(255,100,0)
      ghost object on 10+s
      position object 10+s,-230,40,200
   next s
`----
Camera:
   position camera 200,70,-200
   point camera -20,60,50
   set camera fov 0.8
Return
`----
Images:
   `mountain image
   ink RGB(200,200,180),rgb(40,40,45)
   cls
   for n = 1 to 200
      x=rnd(256):y=rnd(256)
      line x,y,x+rnd(64),y+rnd(64)
   next n
   get image 1,0,0,256,256
 
   `clouds image
   cls rgb(255,255,255)
   for n = 1 to 1000
      x=rnd(255)
      ink rgb(x,x,x),0
      dot rnd(63),rnd(63)
   next n
   get image 3,0,0,64,64
 
   `sky image
   for a=0 to 128
      r=255-(1.8*a)
      g=148-(0.8*a)
      ink rgb(r,g,200),0
      circle 100,128,a
      circle 100,127,a
   next a
   get image 4,0,0,256,256
Return
`----
Sky:
   make object plain 40,1000,1000
   position object 40,-150,0,250
   yrotate object 40,360-45
   texture object 40,4
   set object 40,1,0,1
Return
`----
Fog:
   for f = 1 to 6
      make object plain 10+f,1000,1000
      texture object 10+f,3
      xrotate object 10+f,90
      fix object pivot 10+f
      yrotate object 10+f,45
      if f>1 then ghost object on 10+f
      position object 10+f,50,10+(5*f),50
   next f
Return