This function will obviously load a matrix. It will load it from a heightmap and automatically work out it\'s size, so you dont have to worry about that.
The parameters are:
width#, length#, scale#, heightmap$, texture$, smoothing
- Width and length arent the tiles, just the sizes
- scale controls how much you want the heightmap to take affect.
- the heightmap has to be a black and white image (must be under 70x70 for DBC)
- the whole texture image is pasted onto the matrix. The bigger the image, the smoother it is.
- set smoothing to 0 for no smoothing, or any other value to make the matrix appear smooth.
I stole the matrix normals code from a DBC demo. Dunno how to work them yet, heheh
Code:
function loadMatrix(sx,sy,scale#,file$,tex$,smooth)
open to read 1,file$
read byte 1,null : read byte 1,null
read long 1,null : read long 1,null
read long 1,null : read long 1,null
read long 1,BmpWidth
read long 1,BmpHeight
read word 1,null
read word 1,BitDepth
read long 1,null : read long 1,null
read long 1,null : read long 1,null
read long 1,null : read long 1,null
if BitDepth=24
ypos=BmpHeight
bmpwidth#=bmpwidth
evenwidth1#=(BmpWidth#*3)/4
evenwidth2=(BmpWidth*3)/4
padlen=0
if evenwidth1#<>evenwidth2
evenwidth1#=evenwidth1#-evenwidth2
padlen=4-(evenwidth1#*4)
endif
dim hmap(bmpwidth,bmpheight)
For Ylp=1 to BmpHeight
for Xlp=1 to Bmpwidth
read byte 1,blue
read byte 1,Green
read byte 1,red
hmap(xlp,ypos)=(blue+green+red)/3
next Xlp
dec Ypos
if padlen<>0
for padlp=1 to padlen
read byte 1,padbyte
next padlp
endif
next Ylp
endif
close file 1
mat=freeMatrix()
make matrix mat,sx,sy,bmpwidth,BmpHeight
for x=1 to bmpwidth
for y=1 to bmpheight
set matrix height mat,x,y,hmap(x,y)*scale#
next y
next x
if smooth>0
smoothMatrix(mat,bmpwidth,bmpheight,smooth)
endif
mapi=freeImage()
load image tex$,mapi
prepare matrix texture mat,mapi,bmpwidth,BmpHeight
tile=1
for y=bmpheight-1 to 0 step -1
for x=0 to bmpwidth-1
set matrix tile mat,x,y,tile
inc tile
next x
next y
normalizeMatrix(mat,bmpheight,bmpwidth)
update matrix mat
undim hmap()
endfunction mat
function smoothMatrix(mat,sx,sy,itr)
for i=1 to itr
for x=2 to sx-1
for y=2 to sy-1
hl#=get matrix height(mat,x-1,y)
hr#=get matrix height(mat,x+1,y)
hb#=get matrix height(mat,x,y-1)
hf#=get matrix height(mat,x,y+1)
nh#=(hl#+hr#+hb#+hf#)/4
set matrix height mat,x,y,nh#
next y
next x
next i
update matrix mat
endfunction
function normalizeMatrix(mat,tx,tz)
for z=1 to tz-1
for x=1 to tx-1
h8#=get matrix height(mat,x,z-1)
h4#=get matrix height(mat,x-1,z)
h#=get matrix height(mat,x,z)
h2#=get matrix height(mat,x,z)
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 mat,x,z,nx#,ny#,nz#
next x
next z
endfunction
function freeMatrix()
for i=1 to 65535
if matrix exist(i)=0 then exitfunction i
next i
endfunction 0
