home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Heightmap2.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  3.9 KB  |  116 lines

  1. ' Heightmap demo 2
  2. '
  3. ' Wire frame rendering, and animation
  4. '
  5. const heightScale# = 10 ' Height map scale factor
  6. dim xSize, ySize
  7. dim xMask, yMask        ' bitmasks to apply to X and Y coordinates
  8. dim file                ' File handle
  9. dim x, y                ' Working variables
  10. dim angle#              ' Viewing angle (used for animation)
  11.  
  12. goto Start
  13.  
  14. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  15. ' Routines
  16. CheckError:
  17.     ' Check for file error
  18.     if FileError () <> "" then
  19.         print FileError ()
  20.         CloseFile (file)
  21.         end
  22.     endif
  23.     return
  24.  
  25. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  26. ' Main program stars here    
  27. Start:
  28.  
  29. ' Open file
  30. file = OpenFileRead ("Files\Hills01.dat")
  31.     ' Note: Basic4GL will only open files in the "files" subfolder of the folder
  32.     '       where the program is saved.
  33. gosub CheckError
  34.  
  35. ' Read X and Y size
  36. xSize = Val (ReadLine (file))
  37. ySize = Val (ReadLine (file))
  38. gosub CheckError
  39.  
  40. ' Now that size is known, we can dim our 2D array
  41. dim h#(xSize - 1)(ySize - 1)        
  42. xMask = xSize - 1
  43. yMask = ySize - 1
  44.  
  45. for y = 0 to ySize - 1
  46.     for x = 0 to xSize - 1
  47.         h#(x)(y) = val (ReadText (file, true)) * heightScale#
  48.         gosub CheckError
  49.     next
  50. next          
  51. CloseFile (file)
  52.  
  53. ' Render heightmap to screen
  54.  
  55. ' Not using the Z buffer at this point
  56. glDisable (GL_DEPTH_TEST)
  57.  
  58. while true
  59.     
  60.     ' Clear the screen
  61.     glClear (GL_COLOR_BUFFER_BIT)
  62.     
  63.     ' Setup the transformations
  64.     '
  65.     ' We will set them up as follows.
  66.     ' Translate (move) the grid by -xSize/2, 0, -ySize/2
  67.     ' This will centre the map.
  68.     ' Rotate the grid by angle# around the Y axis (0, 1, 0). This is our animation part
  69.     ' Rotate the grid by 20 degrees around the X axis (1, 0 0) to give our elevated effect
  70.     ' Translate the grid by (0, 0, -50). This moves the grid into the screen, 
  71.     ' into a position where we can actually see it all!
  72.     
  73.     ' Note: Because of the nature of OpenGL, the transformations actually happen in reverse!
  74.     '       So we need to specify them in reverse
  75.     glLoadIdentity ()                       ' Clear out any existing transformations
  76.     glTranslatef (0, 0, -50)                ' "push" map away
  77.     glRotatef (20, 1, 0, 0)                 ' Elevation rotation
  78.     glRotatef (angle#, 0, 1, 0)             ' Animation rotation
  79.     glTranslatef (-xSize/2, 0, -ySize/2)    ' Centre the map    
  80.  
  81.     ' Draw the heightmap points
  82.     for x = 0 to xSize - 2
  83.         for y = 0 to ySize - 2
  84.         
  85.             ' Draw a grid square.
  86.             ' To do this we will use an OpenGL "line loop".
  87.             ' This takes all the vertices we pass it (between the glBegin() and glEnd())
  88.             ' and joins them together with lines. 
  89.             ' It also joins the last one back to the first one, creating the loop.
  90.             glBegin (GL_LINE_LOOP)
  91.                 glVertex3f (x  , h#(x  )(y  ), y  )
  92.                 glVertex3f (x+1, h#(x+1)(y  ), y  )
  93.                 glVertex3f (x+1, h#(x+1)(y+1), y+1)
  94.                 glVertex3f (x  , h#(x  )(y+1), y+1)
  95.             glEnd ()
  96.         next
  97.     next
  98.     
  99.     ' Show the result
  100.     SwapBuffers ()
  101.     
  102.     ' Animation
  103.     '
  104.     ' We animate the scene by increasing the rotation angle each frame.
  105.     ' The SyncTimer call is for speed synchronisation, so that the 
  106.     ' animation runs at the same speed on different computers.
  107.     '
  108.     ' The contents of the SyncTimer loop will be executed 1000/20 = 50 times per
  109.     ' second, regardless of how long it takes to draw each frame.
  110.     ' (What actually happens, is if the computer takes a while to draw the frame, 
  111.     ' the SyncTimer loop will be executed multiple times, so that the animation
  112.     ' "catches up" with the clock.)    
  113.     while SyncTimer (20)
  114.         angle# = angle# + 0.8
  115.     wend
  116. wend