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

  1. ' Heightmap demo 3
  2. '
  3. ' Texturemapping
  4. '
  5. const heightScale# = 10 ' Height map scale factor
  6. const texScale# = 0.1   ' Number of textures per grid square
  7. dim xSize, ySize
  8. dim xMask, yMask        ' bitmasks to apply to X and Y coordinates
  9. dim file                ' File handle
  10. dim x, y                ' Working variables
  11. dim angle#              ' Viewing angle (used for animation)
  12. dim texture             ' OpenGL texture handle for our texture
  13. dim tx#, ty#            ' Texture coordinates
  14.  
  15. goto Start
  16.  
  17. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  18. ' Routines
  19. CheckError:
  20.     ' Check for file error
  21.     if FileError () <> "" then
  22.         print FileError ()
  23.         CloseFile (file)
  24.         end
  25.     endif
  26.     return
  27.  
  28. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  29. ' Main program stars here    
  30. Start:
  31.  
  32. ' Open file
  33. file = OpenFileRead ("Files\Hills01.dat")
  34.     ' Note: Basic4GL will only open files in the "files" subfolder of the folder
  35.     '       where the program is saved.
  36. gosub CheckError
  37.  
  38. ' Read X and Y size
  39. xSize = Val (ReadLine (file))
  40. ySize = Val (ReadLine (file))
  41. gosub CheckError
  42.  
  43. ' Now that size is known, we can dim our 2D array
  44. dim h#(xSize - 1)(ySize - 1)        
  45. xMask = xSize - 1
  46. yMask = ySize - 1
  47.  
  48. for y = 0 to ySize - 1
  49.     for x = 0 to xSize - 1
  50.         h#(x)(y) = val (ReadText (file, true)) * heightScale#
  51.         gosub CheckError
  52.     next
  53. next          
  54. CloseFile (file)
  55.  
  56. ' Load texture file 
  57. texture = LoadMipmapTexture ("textures\00005.jpg")
  58.  
  59. ' Enable texture mapping
  60. glEnable (GL_TEXTURE_2D)
  61.  
  62. ' Tell OpenGL to use our texture that we just loaded.
  63. ' This is called "bind"ing the texture.
  64. ' We will only be using one texture, so we can simply bind it here 
  65. ' at the start of the program and leave it bound.
  66. glBindTexture (GL_TEXTURE_2D, texture)
  67.  
  68. while true
  69.     
  70.     ' Clear the screen
  71.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  72.     ' Note: We are drawing texture mapped squares. These are solid, so therefore
  73.     ' we need to use the Z buffer to ensure the ones infront are drawn over the
  74.     ' ones behind.
  75.     ' Because we are using the Z buffer, we need to clear it each time.
  76.     ' Hence the GL_DEPTH_BUFFER_BIT part
  77.         
  78.     ' Setup the transformations
  79.     glLoadIdentity ()                       ' Clear out any existing transformations
  80.     glTranslatef (0, 0, -50)                ' "push" map away
  81.     glRotatef (20, 1, 0, 0)                 ' Elevation rotation
  82.     glRotatef (angle#, 0, 1, 0)             ' Animation rotation
  83.     glTranslatef (-xSize/2, 0, -ySize/2)    ' Centre the map    
  84.  
  85.     ' Draw the heightmap points
  86.     for x = 0 to xSize - 2
  87.         for y = 0 to ySize - 2
  88.         
  89.             ' Draw the textured grid square.
  90.             ' We will use the quad, which draws a solid filled 4 sided object.
  91.             ' For each vertex we must ALSO specify texture coordinates.
  92.             ' These are 2D coordinates, where (0, 0) = the bottom left
  93.             ' and (1, 1) = the top right of the texture.
  94.             ' Note that we can go outside the 0-1 range, in which case the texture
  95.             ' repeats itself.
  96.             
  97.             ' Calculate the texture coordinates
  98.             tx# = x * texScale#
  99.             ty# = y * texScale#
  100.  
  101.             ' Draw a grid square.
  102.             glBegin (GL_QUADS)
  103.                 glTexCoord2f (tx#            , ty#)
  104.                 glVertex3f (x  , h#(x  )(y  ), y  )
  105.                 
  106.                 glTexCoord2f (tx# + texScale#, ty#)
  107.                 glVertex3f (x+1, h#(x+1)(y  ), y  )
  108.  
  109.                 glTexCoord2f (tx# + texScale#, ty# + texScale#)
  110.                 glVertex3f (x+1, h#(x+1)(y+1), y+1)
  111.  
  112.                 glTexCoord2f (tx#            , ty# + texScale#)
  113.                 glVertex3f (x  , h#(x  )(y+1), y+1)
  114.             glEnd ()
  115.         next
  116.     next
  117.     
  118.     ' Show the result
  119.     SwapBuffers ()
  120.     
  121.     ' Animation
  122.     while SyncTimer (20)
  123.         angle# = angle# + 0.8
  124.     wend
  125. wend