Posted: 23rd Nov 2002 23:04
This might be useful to anyone who can't figure out matrix terrains, or as a simple way to make levels. Basically, you load an image, make a matrix and run the function to set all the matrix heights. It's in it's early stages, so if anyone has any comments/suggestions please say so.

+ Code Snippet
function matrix_heightmap(matnumber,imgnumber,x,y,scale)
make memblock from image 1,imgnumber
k=0
for i=0 to x
for j=0 to y
`text i*10,(y-j)*10,str$((memblock word(1,12+(k*2)))*scale)
set matrix height matnumber,j,(x-i),(memblock word(1,12+(k*2)))*scale
k=k+1
next j
next i
endfunction


Matnumber is the matrix number, imgnumber is the image number, x and y are the width and depth of the matrix and scale is the amount the height is to be scaled by. I haven't managed to scale the built in terrain commands, so I find this more useful. Remember the image has to be one bigger than the matrix grid size in each direction, for example if you had a 50x50 matrix, the image would have to be 51x51 pixels.

As yet there's no tilemapping function, I might make that soon, and then an editor to integrate the functions and produce images.
Posted: 24th Nov 2002 10:08
I tried it but "memblock word(1,12+(k*2)))*scale)" was always 0.

Do you have a complete example (with all the media included)?
Posted: 24th Nov 2002 20:34
Yes, here's one that turns the image into a bunch of canyons. By the way the images I'm using are 256 colours.

+ Code Snippet
sync on
sync rate 45
hide mouse
autocam off

load image "map.bmp",1
make matrix 1,1000,1000,50,50
matrix_heightmap(1,1,50,50,1)
update matrix 1

do
a#=wrapvalue(a#+1)
position camera 500+(sin(a#)*500),200,500+(cos(a#)*500)
point camera 500,0,500
sync
loop

function matrix_heightmap(matnumber,imgnumber,x,y,scale)
make memblock from image 1,imgnumber
k=0
for i=0 to x
for j=0 to y
`text i*10,(y-j)*10,str$((memblock word(1,12+(k*2)))*0.001)
set matrix height matnumber,j,(x-i),(memblock word(1,12+(k*2)))*0.001*scale
k=k+1
next j
next i
endfunction


This is the image I'm using:



called map.bmp
Posted: 24th Nov 2002 23:17
Well, I have a 1024x1024 (100k) jpg file that it is well represented by the terrain commands. However, when I use matrix_heightmap function the output has nothing to do with it.

If you want I could email the jpg image so that you can check what I mean.
Posted: 30th Nov 2002 23:39
Ok, I'll see what I get. TheDarthster@hotmail.com
Posted: 1st Dec 2002 21:09
Ok, I've emailed the file to you ...
Posted: 2nd Dec 2002 0:36
The image I received translated into a matrix fairly well except for spikes in the middle of the canyon which I wasn't expecting. The image was nowhere near 1024x1024 however.
Posted: 15th Dec 2002 15:46
When I tried to use a rectangular pic I soon realised that it should be "set matrix height matnumber,(x-i),j,(memblock word(1,12+(k*2)))*0.001*scale" in the function definition. Still having trouble getting it to work though... I get a matrix but the bumps on it bear little resemblance to the picture
Posted: 15th Dec 2002 23:43
Oops, I nearly always get my x and z mixed up with arrays. Oddly enough, every picture I've tried comes out exactly the way I expect, yet no-one else's does. Oh well, back to the drawing board...
Posted: 28th Jan 2003 7:44
Um, this worked or me, quite well infact. Thanks I was really looking for somethinglike this.
Posted: 28th Jan 2003 18:07
Here is my take on moulding a matrix from a heightmap, To make the matrix look smooth increase the variable blur factor.

+ Code Snippet
Rem Project: Matrix test
Rem Created: 28/01/2003 15:31:57


Rem ***** Main Source File *****
sync on
sync rate 60
hide mouse

rem ****************************************************************************************
rem  Setup Code ****************************************************************************

`variables
largest_value#=0.0
scale_factor#=0.0
max_matrix_height#=100.0
bitmapheight=51
bitmapwidth=51
matrix_scale=10
movespeed#=10.0
`INC the blur_factor for a smoother terrain
blur_factor=1

`arrays and types
dim matrix_vertex_height(bitmapheight-1, bitmapwidth-1) as float
dim smooth_height(bitmapheight-1, bitmapwidth-1) as float
dim height_value(5) as float

`load media
load bitmap "map.bmp", 1
load image "map.bmp", 1
rem ****************************************************************************************
rem ****************************************************************************************



rem ****************************************************************************************
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 (invert m to get correct orientation)
for n=0 to bitmapheight-1
   for m=0 to bitmapwidth-1
      matrix_vertex_height(n, (bitmapwidth-1)-m)=point(n, m)
      if matrix_vertex_height(n, (bitmapwidth-1)-m) > largest_value# then largest_value#=matrix_vertex_height(n, (bitmapwidth-1)-m)
   next m
next n
unlock pixels
delete bitmap 1

rem ****************************************************************************************
rem ****************************************************************************************



rem ****************************************************************************************
rem blur the heights to make matrix look smooth ********************************************
for o=1 to blur_factor
   for n=0 to bitmapheight-1
      for m=0 to bitmapwidth-1
         height_value(1)=matrix_vertex_height(n, m)
         height_value(2)=matrix_vertex_height(n+1, m)
         height_value(3)=matrix_vertex_height(n, m+1)
         height_value(4)=matrix_vertex_height(n-1, m)
         height_value(5)=matrix_vertex_height(n, m-1)
         if n=0 then height_value(4)=matrix_vertex_height(n, m)
         if n=bitmapheight-1 then height_value(2)=matrix_vertex_height(n, m)
         if m=0 then height_value(5)=matrix_vertex_height(n, m)
         if m=bitmapwidth-1 then height_value(3)=matrix_vertex_height(n, m)
         smooth_height(n, m)=(height_value(1)+ height_value(2) + height_value(3) + height_value(4) + height_value(5))/5.0
      next m
   next n


   `transfer smoothed values back to matrix vertex height
   for n=0 to bitmapheight-1
      for m=0 to bitmapwidth-1
         matrix_vertex_height(n, m)=smooth_height(n, m)
      next m
   next n
next o
rem ****************************************************************************************
rem ****************************************************************************************



rem ****************************************************************************************
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

`make matrix
make matrix 1, bitmapwidth*matrix_scale, bitmapheight*matrix_scale, bitmapwidth-1, bitmapheight-1

`set heights
for n=0 to bitmapheight-1
   for m=0 to bitmapwidth-1
      set matrix height 1, n, m, matrix_vertex_height(n, m)
   next m
next n

`update the matrix
update matrix 1

rem ****************************************************************************************
rem ****************************************************************************************



rem ****************************************************************************************
rem Simple Viewing loop ********************************************************************
do
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()

`paste height image
paste image 1, 0, 0
sync
loop
rem ****************************************************************************************
rem ****************************************************************************************
Posted: 28th Jan 2003 23:56
TheDarthster:

Are all the pictures that you are using small? Are set dimensions I should make may heightmap in? Because, I'm getting the same "matrix coordinates illegal" error...
Posted: 29th Jan 2003 0:50
The height map must be square. I.E 10*10,100*100
Also they need to be 1 pixel larger than the size of the matrix you are making. So if the matrix will be 10*10 your image should eb 11*11. If the matrix is 100*100 your image should be 101*101.
Posted: 29th Jan 2003 19:09
I'm using 50x50 matrices, and 51x51 pixel images. Make sure the matrix width and length x and y actually exist, don't try to set x and y higher than the matrix size. Also, if you are not using square images, see G Man X's comment for a correction.
Posted: 2nd Jul 2003 3:07
ok i used this code works good btw

but there is a slight problem if ur using the "set display mode"
set to 32 bit color then all u will get is a ugly matrix made up of spikes

so just set the color to 16 and it should work fine

btw can anyone explain 16 bit and 32 bit color difrences coz i cant see no difrence in vision of performance??

thanx