Screen shots:
Instead of using DBPro's built in matrices, I've used Phaelax's faster memblock matrix functions, which you can find here:
http://forum.thegamecreators.com/?m=forum_view&t=32935&b=6
Here's the function that you call to fractalize the memblock matrix:
fractal_matrix(number as integer, k as float, d as float,minh as float, maxh as float)
Paramters:
number as integer - memblock matrix number
k as float - controls the hilliness of the terrain
0 - flat
0.3 - small hills
0.7 - hilly
1 - mountainous
>1 - cliffs
d as float - controls the bumpiness of the terrain
0 - no bumps
0.5 - some bumps
0.9 - bumpy
1 - mountainous-like bumps
>1 spiky
minh as float - minimum any part of the matrix can have
maxh as float - maximum any part of the matrix can have
randomize timer()
sync on
sync rate 0
autocam off
hide mouse
REM INCLUDE THIS AT START OF YOUR PROGRAM
REM internal matrix data used by matrix functions
type matriks
ix as integer
iy as integer
xsegs as integer
zsegs as integer
width as float
depth as float
mem_numb as integer
obj as integer
image as integer
mesh as integer
prepared as boolean
x as float
y as float
z as float
cull as integer
light as integer
specular as integer
endtype
REM INCLUDE THIS AT START OF YOUR PROGRAM
REM dim array size by maximum number of matrices you'll have
set point light 0,0,4000,0
set light range 0,17000
dim matrix(1) as matriks
mx = 150
mz = 150
sizex# = 4000
sizez# = 4000
REM must be called before you can create a matrix
prepare_matrix_data(1,1,1,1)
REM Make a new matrix
create_matrix(1,sizex#, sizez#,mx,mz)
fractal_matrix(1,0.95,0.95,0,2000)
cls
for x=0 to 25
for z=0 to 25
c=rnd(205)
ink rgb(rnd(20),c,rnd(20)),0
box x,z,x+1,z+1
next z
next x
get image 1,0,0,25,25
cls
for x=0 to 25
for z=0 to 25
ink rgb(rnd(50)+100,rnd(50)+100,255),0
box x,z,x+1,z+1
next z
next x
get image 2,0,0,25,25
make object plain 123,40000,40000
xrotate object 123,90
texture object 123,2
scale object texture 123,100,100
set alpha mapping on 123,90
set object light 123,1
set object ambience 123,rgb(255,255,255)
set object specular 123,rgb(255,255,255)
REM prepare a texture for use with the matrix
prepare_matrix_texture(1,1,1,1)
color backdrop rgb(70,70,255)
h#=0
minh#=get_matrix_height(1,0,0)
for x=0 to matrix(1).xsegs-1
for z=0 to matrix(1).zsegs-1
g#=get_matrix_height(1,x,z)
h#=h#+g#
if g#<minh#
minh#=g#
endif
next z
next x
h#=h#/(matrix(1).xsegs*matrix(1).zsegs)
waterh#=(h#*4+minh#)/5
position object 123,2000,waterh#,2000
update_matrix(1)
configure_matrix_normals(1)
set_matrix_light(1,1)
update_matrix(1)
set camera range 1, 65000
position camera 2000,h#+200,2000
rotate camera 0,0,0
set camera fov 85
DO
g#=get_ground_height(1,camera position x(),camera position z())
y#=g#
if g#<waterh#
y#=waterh#
endif
`position camera camera position x(),y#+50,camera position z()
gosub camera_stuff
text 2,2,"Frame Rate: "+str$(screen fps())
sync
LOOP
REM ==========================================================================
REM Parameters:
REM matrix number
REM object used to create matrix
REM memblock number used to create matrix
REM mesh number used to create object from memblock
function prepare_matrix_data(number as integer, object as integer, mb as integer, mesh as integer)
matrix(number).obj = object
matrix(number).mem_numb = mb
matrix(number).mesh = mesh
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM number of image tiles across
REM number of image tiles down
function scale_matrix_texture(number as integer, across as integer, down as integer)
matrix(number).ix = across
matrix(number).iy = down
matrix(number).prepared = 1
tile = 1
ln = 0
for z = matrix(number).zsegs to 1 step -1
for x = 1 to matrix(number).xsegs
tl = ln*across + tile
set_matrix_tile(number, x, z, tl)
inc tile, 1
if tile > across then tile = 1
next x
inc ln, 1
if ln > down-1 then ln = 0
next z
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM image number
REM number of image tiles across
REM number of image tiles down
function prepare_matrix_texture(number as integer, image as integer, across as integer, down as integer)
matrix(number).ix = across
matrix(number).iy = down
matrix(number).image = image
matrix(number).prepared = 1
`texture object matrix(number).obj, matrix(number).image
for z = 1 to matrix(number).zsegs
for x = 1 to matrix(number).xsegs
set_matrix_tile(number, x, z, 1)
next x
next z
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM width of matrix
REM depth of matrix
REM number of X segments
REM number of Z segments
function create_matrix(number as integer, width as float, depth as float, xsegs as integer, zsegs as integer)
fvf = 338
size = 36
matrix(number).width = width
matrix(number).depth = depth
matrix(number).xsegs = xsegs
matrix(number).zsegs = zsegs
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = depth/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
make memblock mem_numb, total_size
write memblock dword mem_numb, 0, fvf
write memblock dword mem_numb, 4, size
write memblock dword mem_numb, 8, numb_verts
tile = 0
for z = 1 to zsegs
for x = 1 to xsegs
location = tile*size*6 + 12
x1# = tile_size_x# * (x-1)
y1# = 0
z1# = tile_size_z# * (z-1)
x2# = tile_size_x# * (x-1)
y2# = 0
z2# = tile_size_z# * z
x3# = tile_size_x# * x
y3# = 0
z3# = tile_size_z# * z
x4# = x1#
y4# = y1#
z4# = z1#
x5# = x3#
y5# = y3#
z5# = z3#
x6# = x3#
y6# = 0
z6# = z1#
rem vert 1 - x,y,z, normals x,y,z, diffuse color
write memblock float mem_numb, location, x1#
write memblock float mem_numb, location+4, y1#
write memblock float mem_numb, location+8, z1#
write memblock float mem_numb, location+12, 0
write memblock float mem_numb, location+16, 1
write memblock float mem_numb, location+20, 0
write memblock dword mem_numb, location+24, rgb(255,255,255)
rem vert 2 - x,y,z
temp_size = size
write memblock float mem_numb, location+temp_size, x2#
write memblock float mem_numb, location+temp_size+4, y2#
write memblock float mem_numb, location+temp_size+8, z2#
write memblock float mem_numb, location+temp_size+12, 0
write memblock float mem_numb, location+temp_size+16, 1
write memblock float mem_numb, location+temp_size+20, 0
write memblock dword mem_numb, location+temp_size+24, rgb(255,255,255)
rem vert 3 - x,y,z
temp_size = size*2
write memblock float mem_numb, location+temp_size, x3#
write memblock float mem_numb, location+temp_size+4, y3#
write memblock float mem_numb, location+temp_size+8, z3#
write memblock float mem_numb, location+temp_size+12, 0
write memblock float mem_numb, location+temp_size+16, 1
write memblock float mem_numb, location+temp_size+20, 0
write memblock dword mem_numb, location+temp_size+24, rgb(255,255,255)
rem vert 4 - x,y,z
temp_size = size*3
write memblock float mem_numb, location+temp_size, x4#
write memblock float mem_numb, location+temp_size+4, y4#
write memblock float mem_numb, location+temp_size+8, z4#
write memblock float mem_numb, location+temp_size+12, 0
write memblock float mem_numb, location+temp_size+16, 1
write memblock float mem_numb, location+temp_size+20, 0
write memblock dword mem_numb, location+temp_size+24, rgb(255,255,255)
rem vert 5 - x,y,z
temp_size = size*4
write memblock float mem_numb, location+temp_size, x5#
write memblock float mem_numb, location+temp_size+4, y5#
write memblock float mem_numb, location+temp_size+8, z5#
write memblock float mem_numb, location+temp_size+12, 0
write memblock float mem_numb, location+temp_size+16, 1
write memblock float mem_numb, location+temp_size+20, 0
write memblock dword mem_numb, location+temp_size+24, rgb(255,255,255)
rem vert 6 - x,y,z
temp_size = size*5
write memblock float mem_numb, location+temp_size, x6#
write memblock float mem_numb, location+temp_size+4, y6#
write memblock float mem_numb, location+temp_size+8, z6#
write memblock float mem_numb, location+temp_size+12, 0
write memblock float mem_numb, location+temp_size+16, 1
write memblock float mem_numb, location+temp_size+20, 0
write memblock dword mem_numb, location+temp_size+24, rgb(255,255,255)
inc tile, 1
next x
next z
make mesh from memblock 1, mem_numb
make object matrix(number).obj,1,0
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
REM tile is an image tile number (from prepare_matrix_texture())
function set_matrix_tile(number as integer, tileX as integer, tileZ as integer, tile as integer)
if tileX < 0 OR tileX > matrix(number).xsegs OR tileX < 0 OR tileZ > matrix(number).zsegs OR matrix(number).prepared = 0
exitfunction
endif
if matrix(number).ix = 0 then matrix(number).ix = 1
if matrix(number).iy = 0 then matrix(number).iy = 1
mem_numb = matrix(number).mem_numb
size = 36
boffset = size*6
location = 12 + ((tileZ-1)*matrix(number).xsegs*boffset + (tileX-1)*boffset)
x_temp = wrap(tile, matrix(number).ix)
mix# = matrix(number).ix
x_max# = x_temp/mix#
x_min# = (x_temp-1)/mix#
y_temp = int((tile - x_temp) / mix#) + 1.0
miy# = matrix(number).iy
y_max# = y_temp/miy#
y_min# = (y_temp-1)/miy#
if y_min# > 0 then y_min# = y_min# + 0.01
if x_min# > 0 then x_min# = x_min# + 0.01
rem vert 1 - UV coordinates
write memblock float mem_numb, location+28, x_min#
write memblock float mem_numb, location+32, y_max#
rem vert 2
write memblock float mem_numb, location+64, x_min#
write memblock float mem_numb, location+68, y_min#
rem vert 3
write memblock float mem_numb, location+100, x_max#
write memblock float mem_numb, location+104, y_min#
rem vert 4
write memblock float mem_numb, location+136, x_min#
write memblock float mem_numb, location+140, y_max#
rem vert 5
write memblock float mem_numb, location+172, x_max#
write memblock float mem_numb, location+176, y_min#
rem vert 6
write memblock float mem_numb, location+208, x_max#
write memblock float mem_numb, location+212, y_max#
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
REM diffuse color
function set_matrix_diffuse(number as integer, tile_x as integer, tile_z as integer, color as dword)
if tile_x < 0 OR tile_x > matrix(number).xsegs OR tile_z < 0 OR tile_z > matrix(number).zsegs
exitfunction
endif
size = 36
boffset = size*6
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = height/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
lensize = matrix(number).xsegs*boffset
REM verts 3 and 5
if tile_x > 0 and tile_z > 0
gx = tile_x - 1
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+96, color
write memblock dword mem_numb, location+168, color
endif
REM vert 2
if tile_z > 0 and tile_x < matrix(number).xsegs
gx = tile_x
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+60, color
endif
rem vert 6
if tile_x > 0 and tile_z < matrix(number).zsegs
gx = tile_x - 1
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+204, color
endif
rem verts 1 and 4
if tile_x < matrix(number).xsegs and tile_z < matrix(number).zsegs
gx = tile_x
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+24, color
write memblock dword mem_numb, location+132, color
endif
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
REM alpha value
function set_matrix_alpha(number as integer, tile_x as integer, tile_z as integer, alpha as integer)
if tile_x < 0 OR tile_x > matrix(number).xsegs OR tile_z < 0 OR tile_z > matrix(number).zsegs
exitfunction
endif
size = 36
boffset = size*6
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = height/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
lensize = matrix(number).xsegs*boffset
REM verts 3 and 5
if tile_x > 0 and tile_z > 0
gx = tile_x - 1
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+99, alpha
write memblock dword mem_numb, location+171, alpha
endif
REM vert 2
if tile_z > 0 and tile_x < matrix(number).xsegs
gx = tile_x
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+63, alpha
endif
rem vert 6
if tile_x > 0 and tile_z < matrix(number).zsegs
gx = tile_x - 1
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+207, alpha
endif
rem verts 1 and 4
if tile_x < matrix(number).xsegs and tile_z < matrix(number).zsegs
gx = tile_x
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock dword mem_numb, location+27, alpha
write memblock dword mem_numb, location+135, alpha
endif
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
REM height (height of tile you're setting)
function set_matrix_height(number as integer, tile_x as integer, tile_z as integer, height# as float)
if tile_x < 0 OR tile_x > matrix(number).xsegs OR tile_z < 0 OR tile_z > matrix(number).zsegs
exitfunction
endif
size = 36
boffset = size*6
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = height/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
lensize = matrix(number).xsegs*boffset
REM verts 3 and 5
if tile_x > 0 and tile_z > 0
gx = tile_x - 1
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+76, height#
write memblock float mem_numb, location+148, height#
endif
REM vert 2
if tile_z > 0 and tile_x < matrix(number).xsegs
gx = tile_x
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+40, height#
endif
rem vert 6
if tile_x > 0 and tile_z < matrix(number).zsegs
`and tile_z < matrix(number).zsegs
gx = tile_x - 1
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+184, height#
endif
rem verts 1 and 4
if tile_x < matrix(number).xsegs and tile_z < matrix(number).zsegs
gx = tile_x
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+4, height#
write memblock float mem_numb, location+112, height#
endif
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
REM nx,ny,nz - normal
function set_matrix_normal(number as integer, tile_x as integer, tile_z as integer, nx as float, ny as float, nz as float)
if tile_x < 0 OR tile_x > matrix(number).xsegs OR tile_z < 0 OR tile_z > matrix(number).zsegs
exitfunction
endif
size = 36
boffset = size*6
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = height/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
lensize = matrix(number).xsegs*boffset
REM verts 3 and 5
if tile_x > 0 and tile_z > 0
gx = tile_x - 1
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+84, nx
write memblock float mem_numb, location+88, ny
write memblock float mem_numb, location+92, nz
write memblock float mem_numb, location+156, nx
write memblock float mem_numb, location+160, ny
write memblock float mem_numb, location+164, nz
endif
REM vert 2
if tile_z > 0 and tile_x < matrix(number).xsegs
gx = tile_x
gz = tile_z - 1
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+48, nx
write memblock float mem_numb, location+52, ny
write memblock float mem_numb, location+56, nz
endif
rem vert 6
if tile_x > 0 and tile_z < matrix(number).zsegs
gx = tile_x - 1
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+192, nx
write memblock float mem_numb, location+196, ny
write memblock float mem_numb, location+200, nz
endif
rem verts 1 and 4
if tile_x < matrix(number).xsegs and tile_z < matrix(number).zsegs
gx = tile_x
gz = tile_z
location = gz*lensize + gx*boffset + 12
write memblock float mem_numb, location+12, nx
write memblock float mem_numb, location+16, ny
write memblock float mem_numb, location+20, nz
write memblock float mem_numb, location+120, nx
write memblock float mem_numb, location+124, ny
write memblock float mem_numb, location+128, nz
endif
endfunction
remstart
REM ==========================================================================
REM Parameters:
REM matrix number
REM X coordinate
REM Z coordinate
remend
function get_ground_height(number as integer, x#, z#)
h# = intersect object(matrix(number).obj,x#,0,z#,x#,10000,z#)
endfunction h#
remstart
function get_ground_height(number as integer, x#, z#)
tileSizeX# = matrix(number).width / matrix(number).xsegs
tileSizeZ# = matrix(number).depth / matrix(number).zsegs
row = (z# / tileSizeZ#)
column = (x# / tileSizeX#)
if row < 0 OR row > matrix(number).xsegs OR column < 0 or column > matrix(number).zsegs
exitfunction 0.0
endif
h1# = get_matrix_height(number, row, column)
h2# = get_matrix_height(number, row, column+1)
h3# = get_matrix_height(number, row+1, column+1)
h4# = get_matrix_height(number, row+1, column)
ahx# = ((h4#-h1#)+(h3#-h2#)) / 2.0
ahz# = ((h2#-h1#)+(h3#-h4#)) / 2.0
avg# = (ahx# + ahz#) / 2.0
xx# = x# mod tileSizeX#
zz# = z# mod tileSizeZ#
ax# = (xx# * ahx#) / tileSizeX#
az# = (zz# * ahz#) / tileSizeZ#
height# = (ax# + az#) / 2.0
endfunction height#
remend
REM ==========================================================================
REM Parameters:
REM matrix number
REM tile X (grid-based coordinate)
REM tile Z (grid-based coordinate)
function get_matrix_height(number as integer, tile_x as integer, tile_z as integer)
if tile_x < 0 OR tile_x > matrix(number).xsegs OR tile_z < 0 OR tile_z > matrix(number).zsegs
exitfunction
endif
size = 36
boffset = size*6
numb_verts = matrix(number).xsegs * matrix(number).zsegs * 6
total_size = numb_verts*size + 12
tile_size_x# = width/matrix(number).xsegs
tile_size_z# = height/matrix(number).zsegs
mem_numb = matrix(number).mem_numb
lensize = matrix(number).xsegs*boffset
if tile_x < matrix(number).xsegs and tile_z < matrix(number).zsegs
location = tile_z*lensize + tile_x*boffset + 12
yheight# = memblock float (mem_numb, location+4)
else
location = (tile_z-1)*lensize + (tile_x-1)*boffset + 12
yheight# = memblock float (mem_numb, location+76)
endif
endfunction yheight#
REM ==========================================================================
REM Parameters:
REM matrix number
REM image number
REM maximum height
function set_matrix_from_heightmap(number as integer, img as integer, max1 as float)
make memblock from image 256, img
width# = memblock dword(256, 0)
height# = memblock dword(256, 4)
ox# = width#/(matrix(number).xsegs+1)
oz# = height#/(matrix(number).zsegs+1)
for z = 0 to matrix(number).zsegs
for x = 0 to matrix(number).xsegs
location = (int(z*oz#)*width# + int(x*ox#))*4 + 12
c = memblock byte(256, location)
rem convert color value into height value
h# = (c*max1)/255.0
set_matrix_height(number,x,z,h#)
next x
next z
delete memblock 256
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM image number
function set_matrix_from_colormap(number as integer, img as integer)
make memblock from image 256, img
width# = memblock dword(256, 0)
height# = memblock dword(256, 4)
ox# = width#/(matrix(number).xsegs+1)
oz# = height#/(matrix(number).zsegs+1)
for z = 0 to matrix(number).zsegs
for x = 0 to matrix(number).xsegs
location = (int(z*oz#)*width# + int(x*ox#))*4 + 12
b = memblock byte(256, location)
g = memblock byte(256, location+1)
r = memblock byte(256, location+2)
c = rgb(r,g,b)
set_matrix_diffuse(number,x,z,c)
next x
next z
delete memblock 256
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM image number
function set_matrix_from_alphamap(number as integer, img as integer)
make memblock from image 256, img
width# = memblock dword(256, 0)
height# = memblock dword(256, 4)
ox# = width#/(matrix(number).xsegs+1)
oz# = height#/(matrix(number).zsegs+1)
for z = 0 to matrix(number).zsegs
for x = 0 to matrix(number).xsegs
location = (int(z*oz#)*width# + int(x*ox#))*4 + 12
a = memblock byte(256, location+3)
set_matrix_alpha(number,x,z,a)
next x
next z
delete memblock 256
endfunction
REM ==========================================================================
REM Parameters:
REM X,Y,Z coordinates
function position_matrix(number as integer, x as float, y as float, z as float)
matrix(number).x = x
matrix(number).y = y
matrix(number).z = z
position object matrix(number).obj, x, y, z
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
function update_matrix(number as integer)
change mesh from memblock matrix(number).mesh, matrix(number).mem_numb
delete object matrix(number).obj
make object matrix(number).obj, matrix(number).mesh, matrix(number).image
set object cull matrix(number).obj, matrix(number).cull
set object light matrix(number).obj, matrix(number).light
set object specular matrix(number).obj, matrix(number).specular
position object matrix(number).obj, matrix(number).x, matrix(number).y, matrix(number).z
if matrix(number).prepared = 0
set object wireframe matrix(number).obj, 1
endif
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM flag, on or off
function set_matrix_cull(number as integer, flag as integer)
matrix(number).cull = flag
set object cull matrix(number).obj, flag
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM flag, on or off
function set_matrix_light(number as integer, flag as integer)
matrix(number).light = flag
set object light matrix(number).obj, flag
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM flag, on or off
function set_matrix_specular(number as integer, flag as integer)
matrix(number).specular = flag
set object specular matrix(number).obj, flag
endfunction
REM ==========================================================================
REM Coded by Dr. DB himself, Lee Bamber
REM Parameters:
REM matrix number
function configure_matrix_normals(number as integer)
xsize#=matrix(number).width/matrix(number).xsegs
zsize#=matrix(number).depth/matrix(number).zsegs
v1=1001
v2=1002
v3=1003
v4=1004
v5=1005
v6=1006
v7=1007
v8=1008
k=make vector3(v1)
k=make vector3(v2)
k=make vector3(v3)
k=make vector3(v4)
k=make vector3(v5)
k=make vector3(v6)
k=make vector3(v7)
k=make vector3(v8)
for z=1 to matrix(number).zsegs-1
for x=1 to matrix(number).xsegs-1
rem Get matrix heights
x00#=x*xsize#
z00#=z*zsize#
y00#=get_matrix_height(number,x,z)
x10#=(x+1)*xsize#
z10#=z*zsize#
y10#=get_matrix_height(number,x+1,z)
x20#=(x-1)*xsize#
z20#=z*zsize#
y20#=get_matrix_height(number,x-1,z)
x01#=x*xsize#
z01#=(z+1)*zsize#
y01#=get_matrix_height(number,x,z+1)
x02#=x*xsize#
z02#=(z-1)*zsize#
y02#=get_matrix_height(number,x,z-1)
set vector3 v1,x10#-x00#,y10#-y00#,z10#-z00#
set vector3 v2,x01#-x00#,y01#-y00#,z01#-z00#
cross product vector3 v5,v2,v1
normalize vector3 v5,v5
set vector3 v1,x00#-x20#,y00#-y20#,z00#-z20#
set vector3 v2,x01#-x00#,y01#-y00#,z01#-z00#
cross product vector3 v6,v2,v1
normalize vector3 v6,v6
set vector3 v1,x10#-x00#,y10#-y00#,z10#-z00#
set vector3 v2,x00#-x02#,y00#-y02#,z00#-z02#
cross product vector3 v7,v2,v1
normalize vector3 v7,v7
set vector3 v1,x00#-x20#,y00#-y20#,z00#-z20#
set vector3 v2,x00#-x02#,y00#-y02#,z00#-z02#
cross product vector3 v8,v2,v1
normalize vector3 v8,v8
set vector3 v4,0,0,0
add vector3 v4,v4,v5
add vector3 v4,v4,v6
add vector3 v4,v4,v7
add vector3 v4,v4,v8
normalize vector3 v4,v4
nx#=x vector3(v4)
ny#=y vector3(v4)
nz#=z vector3(v4)
rem Setting matrix normal for smoothness
set_matrix_normal(number,x,z,nx#,ny#,nz#)
next x
next z
k=delete vector3(v1)
k=delete vector3(v2)
k=delete vector3(v3)
k=delete vector3(v4)
k=delete vector3(v5)
k=delete vector3(v6)
k=delete vector3(v7)
k=delete vector3(v8)
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM max height
function randomize_matrix(number as integer, height as float)
for z = 0 to matrix(number).zsegs
for x = 0 to matrix(number).xsegs
set_matrix_height(number, x, z, rnd(height*100.0)/100.0)
next x
next z
endfunction
REM ==========================================================================
REM Parameters:
REM matrix number
REM X,Y screen coordinates
REM vector number to store results in
function pick_matrix(number as integer, x as integer, y as integer, vector as integer)
if matrix(number).obj = 0 then exitfunction
t = pick object(x,y,matrix(number).obj,matrix(number).obj)
vx# = camera position X()+get pick vector x()
vy# = camera position y()+get pick vector y()
vz# = camera position z()+get pick vector z()
set vector3 vector, vx#, vy#, vz#
endfunction
REM wrapvalue function
REM Wraps n1# to n2#
function wrap(n1#,n2#)
while (n1# > n2#)
n1# = n1# - n2#
endwhile
endfunction n1#
camera_stuff:
rotate camera camera angle x()+mousemovey(),camera angle y()+mousemovex(),0
move camera keystate(17)-keystate(31)
x#=camera angle x()
rotate camera 0,camera angle y()+90,0
move camera keystate(32)-keystate(30)
rotate camera x#,camera angle y()-90,0
RETURN
function fractal_matrix(number as integer, k as float, d as float,minh as float, maxh as float)
fractal_matrix2(number,0,matrix(number).xsegs-1,0,matrix(number).zsegs-1,matrix(number).width,matrix(number).depth,k,d,minh,maxh)
endfunction
function fractal_matrix2(mat_num,minxtile,maxxtile,minztile,maxztile,xsize#,zsize#,k#,d#,minh#,maxh#)
if minxtile=maxxtile or minztile=maxztile
if minxtile=maxxtile and maxztile>minztile+1
cz=(maxztile+minztile)/2
h#=(get_matrix_height(mat_num,minxtile,minztile)+get_matrix_height(mat_num,maxxtile,maxztile))*0.5
repeat
dy#=zsize#*randf(-1,1)*0.5
h#=h#+dy#*k#
until h#>minh# and h#<maxh#
set_matrix_height(mat_num,minxtile,cz,h#)
fractal_matrix2(mat_num,minxtile,maxxtile,minztile,cz,0,zsize#*0.5,k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,minxtile,maxxtile,cz,maxztile,0,zsize#*0.5,k#*d#,d#,minh#,maxh#)
endif
if minztile=maxztile and maxxtile>minxtile+1
cx=(maxxtile+minxtile)/2
h#=(get_matrix_height(mat_num,minxtile,minztile)+get_matrix_height(mat_num,maxxtile,maxztile))*0.5
repeat
dy#=xsize#*randf(-1,1)*0.5
h#=h#+dy#*k#
until h#>minh# and h#<maxh#
set_matrix_height(mat_num,cx,minztile,h#)
fractal_matrix2(mat_num,minxtile,cx,minztile,maxztile,xsize#*0.5,0,k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,cx,maxxtile,minztile,maxztile,xsize#*0.5,0,k#*d#,d#,minh#,maxh#)
endif
else
cx=(minxtile+maxxtile)/2
cz=(minztile+maxztile)/2
size#=xsize#
if zsize#<xsize# then size#=zsize#
h#=0
h#=h#+get_matrix_height(mat_num,minxtile,minztile)
h#=h#+get_matrix_height(mat_num,minxtile,cz)
h#=h#+get_matrix_height(mat_num,minxtile,maxztile)
h#=h#+get_matrix_height(mat_num,cx,maxztile)
h#=h#+get_matrix_height(mat_num,maxxtile,maxztile)
h#=h#+get_matrix_height(mat_num,maxxtile,cz)
h#=h#+get_matrix_height(mat_num,maxxtile,minztile)
h#=h#+get_matrix_height(mat_num,cx,minztile)
h#=h#/8.0
repeat
dy#=size#*randf(-1,1)*0.5
h#=h#+dy#*k#
until h#>minh# and h#<maxh#
set_matrix_height(mat_num,cx,cz,h#)
if maxztile>minztile+1 or maxxtile>minxtile+1
fractal_matrix2(mat_num,cx,cx,minztile,cz,0,zsize#*0.5, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,cx,cx,cz,maxztile,0,zsize#*0.5, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,minxtile,cx,cz,cz,xsize#*0.5,0, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,cx,maxxtile,cz,cz,xsize#*0.5,0, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,minxtile,cx,minztile,cz,xsize#*0.5,zsize#*0.5, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,minxtile,cx,cz,maxztile,xsize#*0.5,zsize#*0.5, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,cx,maxxtile,minztile,cz,xsize#*0.5,zsize#*0.5, k#*d#,d#,minh#,maxh#)
fractal_matrix2(mat_num,cx,maxxtile,cz,maxztile,xsize#*0.5,zsize#*0.5, k#*d#,d#,minh#,maxh#)
endif
endif
endfunction
function randf(min#,max#)
d#=max#-min#
x#=rnd(d#*10000)/10000.0+min#
endfunction x#