lol i got a bit bored so i ended up doing some more work on the matrix. I had a go at normalizing and it looks allright though my code is a little hard to understand.
Rem Project: matrix heightmap
Rem Created: 17/01/2003 17:12:05
Rem ***** Main Source File *****
rem Standard Setup Code
sync on : sync rate 0 : autocam off
color backdrop rgb(0,0,128) : hide mouse
set text font "arial" : set text size 14 :set text to bold: set text transparent
rem variables
largest_value#=0.0
scale_factor#=0.0
max_matrix_height#=80.0
brightness#=0.6
blur_factor=10
bitmapheight=128
bitmapwidth=128
matrix_scale=4
movespeed#=10
`arrays and types
dim matrix_vertex_height(bitmapheight-1, bitmapwidth-1) as float
type norm X as float Y as float Z as float endtype
dim normal(bitmapheight-1, bitmapwidth-1) as norm
dim normal_blur(bitmapheight-1, bitmapwidth-1) as norm
dim normal_values(4) as float
`vectors
edgeA=1
edgeB=2
normal=3
final=4
rf=make vector3(edgeA)
rf=make vector3(edgeB)
rf=make vector3(normal)
rf=make vector3(final)
rem load media
load bitmap "heightmap.bmp", 1
rem ambient light
position light 0, 128, 100, 128
rem extract heights from bitmap and place into an array
`set bitmap to heigtmap
set current bitmap 1
lock pixels
`extract data row by row
for n=0 to bitmapheight-1
for m=0 to bitmapwidth-1
matrix_vertex_height(n, m)=point(n, m)
if matrix_vertex_height(n, m) > largest_value# then largest_value#=matrix_vertex_height(n, m)
next m
next n
unlock pixels
delete bitmap 1
rem make a matrix and position all the vertice based upon heights in array
`firstly find scale factor for matrix
scale_factor#=largest_value#/max_matrix_height#
for n=0 to bitmapheight-1
for m=0 to bitmapwidth-1
matrix_vertex_height(n, m)=matrix_vertex_height(n, m)/scale_factor#
next m
next n
`find the damn normals of the matrix
for n=bitmapheight-1 to 0 step -1
for m=0 to bitmapwidth-1
if n>0 and m<bitmapwidth-1
set vector3 edgeA, matrix_scale, (matrix_vertex_height(n, m+1) - matrix_vertex_height(n, m)) , 0.0
set vector3 edgeB, 0.0, (matrix_vertex_height(n-1, m) - matrix_vertex_height(n, m)) , matrix_scale
cross product vector3 normal, edgeB, edgeA
normalize vector3 final, normal
multiply vector3 final, brightness#
normal(n, m).X=x vector3(final)
normal(n, m).Y=y vector3(final)
normal(n, m).Z=z vector3(final)
else
`fudge the edge normals
if n>0
normal(n, m).X=normal(n, m-1).X
normal(n, m).Y=normal(n, m-1).Y
normal(n, m).Z=normal(n, m-1).Z
else
normal(n, m).X=normal(n+1, m).X
normal(n, m).Y=normal(n+1, m).Y
normal(n, m).Z=normal(n+1, m).Z
endif
endif
next m
next n
`blur the normal data so it looks smooth
for o=0 to blur_factor
for n=0 to bitmapheight-1
for m=0 to bitmapwidth-1
`for the X normal
normal_values(0)=normal(n, m).X
if n<bitmapheight-1
normal_values(1)=normal(n+1, m).X
else
normal_values(1)=normal(n, m).X
endif
if m<bitmapwidth-1
normal_values(2)=normal(n, m+1).X
else
normal_values(2)=normal(n, m).X
endif
if n>0
normal_values(3)=normal(n-1, m).X
else
normal_values(3)=normal(n, m).X
endif
if m>0
normal_values(4)=normal(n, m-1).X
else
normal_values(4)=normal(n, m).X
endif
normal_blur(n, m).X=(normal_values(0) + normal_values(1) + normal_values(2) + normal_values(3) + normal_values(4))/5.0
`for the Y normal
normal_values(0)=normal(n, m).Y
if n<bitmapheight-1
normal_values(1)=normal(n+1, m).Y
else
normal_values(1)=normal(n, m).Y
endif
if m<bitmapwidth-1
normal_values(2)=normal(n, m+1).Y
else
normal_values(2)=normal(n, m).Y
endif
if n>0
normal_values(3)=normal(n-1, m).Y
else
normal_values(3)=normal(n, m).Y
endif
if m>0
normal_values(4)=normal(n, m-1).Y
else
normal_values(4)=normal(n, m).Y
endif
normal_blur(n, m).Y=(normal_values(0) + normal_values(1) + normal_values(2) + normal_values(3) + normal_values(4))/5.0
`for the Z normal
normal_values(0)=normal(n, m).Z
if n<bitmapheight-1
normal_values(1)=normal(n+1, m).Z
else
normal_values(1)=normal(n, m).Z
endif
if m<bitmapwidth-1
normal_values(2)=normal(n, m+1).Z
else
normal_values(2)=normal(n, m).Z
endif
if n>0
normal_values(3)=normal(n-1, m).Z
else
normal_values(3)=normal(n, m).Z
endif
if m>0
normal_values(4)=normal(n, m-1).Z
else
normal_values(4)=normal(n, m).Z
endif
normal_blur(n, m).Z=(normal_values(0) + normal_values(1) + normal_values(2) + normal_values(3) + normal_values(4))/5.0
next m
next n
`transfer blurred normals back to original normal array
for n=0 to bitmapheight-1
for m=0 to bitmapwidth-1
normal(n, m).X=normal_blur(n, m).X
normal(n, m).Y=normal_blur(n, m).Y
normal(n, m).Z=normal_blur(n, m).Z
next m
next n
next o
`make matrix
make matrix 1, bitmapwidth*matrix_scale, bitmapheight*matrix_scale, bitmapwidth-1, bitmapheight-1
set matrix wireframe off 1
`set heights and normalize
for n=0 to bitmapheight-1
for m=0 to bitmapwidth-1
set matrix height 1, n, m, matrix_vertex_height(n, m)
set matrix normal 1, n, m, normal(n, m).X, normal(n, m).Y, normal(n, m).Z
next m
next n
`update the matrix
update matrix 1
rem display all the hard work!
set current bitmap 0
`position camera
position camera 64, 50, 0
point camera 64, 0, 64
do
`move camera about
if mouseclick()=1 then move camera movespeed#
if mouseclick()=2 then move camera -1.0*movespeed#
yrotate camera 0, camera angle y(0)+ mousemovex()
xrotate camera 0, camera angle x(0)+ mousemovey()
if camera position y()-10<get ground height (1, camera position x(), camera position z()) then position camera camera position x(), get ground height(1, camera position x(), camera position z())+10, camera position z()
text 0, 0, "yummy normalized matrix :)"
text 0, 15, "mouseclick to move etc"
text 0, 30, "FPS rate: " + str$(screen fps())
text 0, 45, "position: (X " + str$(camera position x()) + ") (Y " + str$(camera position y()) + ") (Z " + str$(camera position z()) + ")"
sync
loop
Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp