autocam off
color backdrop 0
hide light 0
make light 1
position light 1,50,50,100
set light range 1,100
hide mouse
 
randomize timer()
sync on
 
terraform()
 
projector=check_free_image_number()
Make Camera 1
Set Camera Aspect 1,Screen Width()/Screen Height()
Set Camera Range 1,0.01,10000
Set Camera FOV 1,130
Set Camera To Image 1,projector,1024,1024,0
projector2=check_free_image_number()
Make Camera 2
Set Camera Aspect 2,Screen Width()/Screen Height()
Set Camera Range 2,0.01,10000
Set Camera FOV 2,132
Set Camera To Image 2,projector2,512,512,0
Position Camera 0,0,-20000,-.5
Rotate Camera 0,0,0,0
Make Object Plain 1,20,16
`Set Object Cull 1,0
Position Object 1,0,-20000,10
Texture Object 1,projector
Set Object Light 1,0
Set Object Ambient 1,0
Set Detail Mapping On 1,projector
Set Light Mapping On 1,Shade_Map()
Set Object Emissive 1,Rgb(200,200,200)
 
Make Object Plain 2,20,16
Texture Object 2,projector2
Set Object Light 2,0
Set Object Ambient 2,0
Set Object Emissive 2,Rgb(50,50,50)
Set Alpha Mapping On 2,10
Position Object 2,0,-20000,9.9
 
do
 
   move_camera(0.1,2)
   move_water()
 
   text 0,0,"FPS: "+str$(screen fps())
   sync
loop
 
 
 
 
Function Shade_Map()
   Box 0,0,250,250,10,10,10,10
   w#=255
   For i= 1 to 100
      Ink Rgb(w#,w#,w#),0
      Circle 124,125,i
      Circle 125,125,i
      Circle 125,126,i
      w#=w#-2.53
   Next i
   Blur Bitmap 0,6
   Ink Rgb(255,255,255),0
   Shade=check_free_image_number()
   Get Image Shade,0,0,250,250
Endfunction Shade
 
 
 
 
 
 
 
 
 
function terraform
sync
text 0,0,"Generating terrain ....."
sync
create bitmap 1,screen width(),screen height()
set current bitmap 1
global matrixsize
matrixsize=100
fog on
fog color rgb(180,180,250)
fog distance matrixsize*0.6
global object_seed
object_seed=1000000
global image_seed
image_seed=1000000
global grassimage
create_grass()
global skyimage
create_sky()
global waterimage
global water
global water2
create_water_image()
create_water()
`set size of terrain
rows=60
columns=60
 
`create terrain
make matrix 1,matrixsize,matrixsize,columns,rows
 
`set mountain peaks
number_of_peaks=rnd(35)+10
max_height=15
 
dim peakx(number_of_peaks)
dim peakz(number_of_peaks)
dim height(number_of_peaks)
for peak=1 to number_of_peaks
  `set x,z coordinates for peak
  peakx(peak)=rnd(columns-10)+5
  peakz(peak)=rnd(rows-10)+5
  `set height of peak
  height(peak)=rnd(max_height)+10
  `create peak
  set matrix height 1,peakx(peak),peakz(peak),height(peak)
next peak
 
num=rows*columns
dim obnum(rows+1,columns+1)
dim v#(rows,columns)
 
`give each tile an x,z coordinate for use later
for x=1 to rows
for z=1 to columns
obnum(x,z)=ob
ob=ob+1
next z
next x
 
`texture matrix
prepare matrix texture 1,grassimage,rows,columns
tile=1
for x=rows-1 to 0 step -1
for z=0 to columns-1
set matrix tile 1,z,x,tile
inc tile
next z
next x
 
 
`adjust height of points between peaks
elasticity#=0.3
damping#=0
 
for time=1 to 100
 
for peak=1 to number_of_peaks
  set matrix height 1,peakx(peak),peakz(peak),height(peak)
next peak
 
for x=1+1 to rows-1
for z=1+1 to columns-1
if x<rows
distxp1#=get matrix height(1,x+1,z)-get matrix height (1,x,z)
endif
if x>1
distxm1#=get matrix height(1,x-1,z)-get matrix height (1,x,z)
endif
if z<columns
distzp1#=get matrix height(1,x,z+1)-get matrix height (1,x,z)
endif
if z>1
distzm1#=get matrix height(1,x,z-1)-get matrix height (1,x,z)
endif
vectorsum#=distxp1#+distxm1#+distzp1#+distzm1#
a#=vectorsum#*elasticity#
v#(obnum(x,z))=v#(obnum(x,z))+a#
set matrix height 1,x,z,get matrix height(1,x,z)+v#(obnum(x,z))
v#(obnum(x,z))=v#(obnum(x,z))*damping#
next z
next x
update matrix 1
next time
 
create_skysphere()
position camera 65,get ground height(1,65,65)+2,65
point camera 75,get ground height(1,75,75),75
set current bitmap 0
ink rgb(255,255,255),0
 
endfunction
 
 
 
 
function check_free_object_number
 
`this handy function means that you never have to worry about
`remembering object numbers.  You just give them names you can remember,
`and this function will find the next available number to assosciate
`with that name.  Just call the function by, for example, writing:
`
`ball=check_free_object_number
`make object sphere ball,1
 
object=object_seed
repeat
inc object
until object exist(object)=0
 
endfunction object
 
function check_free_image_number()
 
`this functionworks in the same way as the check_free_object function,
`except it works for images.
 
image=image_seed
repeat
inc image
until image exist(image)=0
 
endfunction image
 
function create_grass
 
`make a shaded box
box 0,0,100,100,rgb(100,150,100),rgb(50,200,20),rgb(80,0,10),rgb(150,200,0)
 
`and speckle it with random dots
for x=0 to 100
  for y=0 to 100
    if rnd(5)=0
      r=rnd(150)
      g=rnd(250)
      b=rnd(100)
      ink rgb(r,g,b),0
      dot x,y
    endif
  next x
next y
 
 
grassimage=check_free_image_number()
get image grassimage,0,0,100,100
 
endfunction
 
function create_skysphere
 
`get image for sphere by taking snapshot of terrain
image=check_free_image_number()
set camera fov 90
position camera matrixsize/2.0,3,-matrixsize*0.3
sync
get image image,0,0,screen width(),screen height()
set camera fov 60
 
`sphere 1 (mountains near) - just a sphere textured with the captured image
object=check_free_object_number()
make object sphere object,matrixsize*2
set object cull object,0
texture object object,image
set object texture object,2,1
scale object texture object,3,1
set object fog object,0
set object light object,0
set object transparency object,4
position object object,matrixsize/2,0,matrixsize/2
 
`sphere 2 (mountains far) - and another one further out. The inner sphere must be
`transparent, though, to see this one.
object=check_free_object_number()
make object sphere object,matrixsize*2.5
yrotate object object,30
set object cull object,0
texture object object,image
set object texture object,2,1
scale object texture object,3,1
set object fog object,0
`set object light object,0
set object transparency object,4
ghost object on object,4
set object emissive object,rgb(180,150,250)
fade object object,70
position object object,matrixsize/2,0,matrixsize/2
 
`sphere 3 (sky) - a third, outer sphere for the sky
object=check_free_object_number()
make object sphere object,matrixsize*4
set object cull object,0
texture object object,skyimage
scale object texture object,1,1.8
set object light object,0
set object fog object,0
 
endfunction
 
function create_sky()
`creates a dark to light bluish gradient
cls
for n=0 to 250
  ink rgb(n,n,250),0
  line 0,n,250,n
next n
 
skyimage=check_free_image_number()
get image skyimage,0,0,250,250
 
endfunction
 
 
function create_water_image
 
`creates a shaded box
box 0,0,100,100,rgb(150,150,200),rgb(50,200,200),rgb(80,0,200),rgb(150,100,200)
 
`and speckles with white bits
for x=0 to 100
  for y=0 to 100
    if rnd(5)=0
      r=(250)
      g=(250)
      b=(250)
      ink rgb(r,g,b),0
      dot x,y
    endif
  next x
next y
 
 
waterimage=check_free_image_number()
get image waterimage,0,0,100,100
 
 
 
endfunction
 
 
function create_water
 
`just a plane textured with the water image
water=check_free_object_number()
make object plain water,matrixsize*4,matrixsize*4
texture object water,waterimage
set object texture water,2,1
scale object texture water,4,4
xrotate object water,90
set object fog water,0
set object emissive water,rgb(220,220,200)
ghost object on water
set alpha mapping on water,20
water2=check_free_object_number()
make object plain water2,matrixsize*4,matrixsize*4
texture object water2,waterimage
xrotate object water2,90
position object water2,0,1,0
set object texture water2,2,1
scale object texture water2,4,4
set object light water2,0
set object fog water2,0
 
endfunction
 
function move_water
 
`moves the texture along a bit
scroll object texture water,0.001,0
scroll object texture water2,-0.001,0
`and moves the water up and down according to a sine wave
position object water,0,0.5*sin(theta#)+3,0
inc theta#,1
 
endfunction
 
function move_camera(movespeed#,turnspeed#)
 
control camera using arrowkeys 1,movespeed#,turnspeed#
position camera 1,camera position x(1),get ground height(1,camera position x(1),camera position z(1))+0.4,camera position z(1)
position camera 2,camera position x(1),camera position y(1),camera position z(1)
Rotate Camera 2,Camera Angle X(1),Camera Angle Y(1),Camera Angle Z(1)
endfunction