TGC Codebase Backup



Build + Set matrix normals from heightmap by FROGGIE!

14th Feb 2005 13:05
Summary

Function to build and set the matrix normals using a simple height map



Description

This code will build a matrix using data given from a heightmap. It will then set the matrix's normals to give it a smooth appearance.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
   -------------------------------------------------------------------
   program: Function to build + normalise matrix from height map
   -------------------------------------------------------------------
   written by: James Barker [FROGGIE!]
   date: 10/02/05
   -------------------------------------------------------------------
remend

` height map - Height map must be in BMP format. Width and height must be divisible by 2

` function parametres needed:
` filename of bitmap, matrix number, matrix size x, matrix size z, maximum height of the matrix
` MATRIX MUST NOT EXIST!
FUNCTION map_matrix(file_name$, mtx_number, mtx_x, mtx_z, mtx_max_height)

` load the file
LOAD BITMAP file_name$,1

` set a couple of variables
map_x = BITMAP WIDTH(1)
map_y = BITMAP HEIGHT(1)

DEC map_x
DEC map_y

` build the matrix
MAKE MATRIX mtx_number,mtx_x,mtx_z,map_x,map_y

` set the heights of the matrix according to bitmap
` black (0,0,0) = height of 0
` white (255,255,255) = maximum height
FOR z = 0 TO map_y
FOR x = 0 TO map_x

   col_value AS DWORD
   SET CURRENT BITMAP 1
   col_value = (RGBR(POINT(x,z)))*255

      map_percent = (col_value/255)*100
      mtx_height# = (mtx_max_height/100)*map_percent

         SET MATRIX HEIGHT mtx_number,x,z,(mtx_height#/255)

NEXT x
NEXT z

` normalise the matrix
 FOR z_normal = 1 To map_y
 FOR x_normal = 1 To map_x

    height_8# = GET MATRIX HEIGHT(mtx_number,x_normal,z_normal-1)
    height_4# = GET MATRIX HEIGHT(mtx_number,x_normal-1,z_normal)
    height_0# = GET MATRIX HEIGHT(mtx_number,x_normal,z_normal)
    height_2# = GET MATRIX HEIGHT(mtx_number,x_normal,z_normal)

    x1# = (x_normal-1)*25.0 : y1# = height_0#
    x2# = (x_normal+0)*25.0 : y2# = height_4#
      dx# = x2#-x1# : dy# = y2#-y1#
         ax# = ATANFULL(dx#,dy#)
         ax# = WRAPVALUE(90-ax#)

    z1# = (z_normal-1)*25.0 : y1# = height_2#
    z2# = (z_normal+0)*25.0 : y2# = height_8#
      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 mtx_number,x_normal,z_normal,nx#,ny#,nz#

 NEXT x_normal
 NEXT z_normal

` refresh
DELETE BITMAP 1
SET CURRENT BITMAP 0
UPDATE MATRIX mtx_number

ENDFUNCTION