TGC Codebase Backup



Smoothed Matrix from BitMap HeighMap. by Anonymous Coder

23rd Mar 2005 10:12
Summary

This tutorial will show you how to load a heightmap and create a SMOOTH Matrix out of it. PLEASE D/L Attached ZIP file.



Description

This code will take a Heightmap BitMap and creat a smoothes (Thats right, No pointy slopes) in the size and resolution you choose.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `***************************************
`   Smooth matrix from heightmap Image
`***************************************
` By: Shokata
dim matYA(8) `This array will contain 8 surrounding tiles for each tile
load bitmap "file.jpg",1 `Load bitmap file that contains GrayScale HeightMap.
set current bitmap 1 `set Darkbasic to refer Bitmap 1 as the bitmap to work with
`get bitmap Dimenstions
intBMP_W = bitmap width(1)
intBMP_H = bitmap height(1)
make matrix 1, intBMP_W*10 , intBMP_H*10 , intBMP_W , intBMP_H `Create matrix. ("*10" is to multiply the matrix size)

`this is a loop that checks all the red colors of the pixels in the bitmap and sets
`the compatible tile to the correct hieght
for matX =1 to intBMP_W
  For matZ =1 to intBMP_H
     `Get the Red (rgbr) information from pixel (point) at x and y position
      matYA(1) = rgbr(point (matX+1,matZ+1))
      matYA(2) = rgbr(point (matX+1,matZ))
      matYA(3) = rgbr(point (matX+1,matZ-1))
      matYA(4) = rgbr(point (matX,matZ-1))
      matYA(5) = rgbr(point (matX,matZ+1))
      matYA(6) = rgbr(point (matX-1,matZ+1))
      matYA(7) = rgbr(point (matX-1,matZ))
      matYA(8) = rgbr(point (matX-1,matZ-1))
      ` Get the average height of all the pixels
      matY# = (matYA(1)+matYA(2)+matYA(3)+matYA(4)+matYA(5)+matYA(6)+matYA(7)+matYA(8))/8
remstart
   What we did here is thake all the pixel arround a certain pixel
   for Exp:
            1 2 3 4 5
            2
            3   X
            4
            5
   if the pixel we check now is (3,3) then we'll check the average height of these pixel:
         (4,4) & (4,3) & (4,2) & (3,4) & (3,2) & (2,4) & (2,3) & (2,2)

      set the found average to be the compatible tile's height
      "matY#/2" => The lower the number the heigher the slopes will be.
remend
      set matrix height 1 , matX , matZ , matY#/2
  next
next
delete bitmap 1 ` Get rid of the HeightMap bitmap
update matrix 1 ` update the matrix changes we've made
load image "sand.jpg",1 ` load image to be matrix's texture
prepare matrix texture 1,1,1,1 ` set the texture to our matrix
position camera 736,70,600 `position camera above the matrix
point camera 50,0,50 ` point the camera to the matrix

`this is the main loop
do
  ` set the camera to be controlled by the arrow keys
   control camera using arrowkeys 0,1,1
   print camera position x()
   print camera position z()
loop

` Hope this tutorial was helpfull.
` have a great Day.